using static System.Math;
using System.Collections.Generic;
using System.Linq;
using System;

public class hello
{
    public static void Main()
    {
        var d = int.Parse(Console.ReadLine().Trim());
        var x = int.Parse(Console.ReadLine().Trim());

        var a1 = new List<int>();
        for (int i = 0; i < x; i++)
        {
            string[] line = Console.ReadLine().Trim().Split(' ');
            if (line[0] == "D") a1.Add(int.Parse(line[1]));
        }
        a1.Sort();
        a1.Reverse();
        var y = int.Parse(Console.ReadLine().Trim());
        if (y == 0 | d == 1) { Console.WriteLine(dd0(a1,d)); goto exit; }
        var a2 = new List<int>();
        for (int i = 0; i < y; i++)
        {
            string[] line = Console.ReadLine().Trim().Split(' ');
            if (line[0] == "DD") a2.Add(int.Parse(line[1]));
        }
        a2.Sort();
        a2.Reverse();
        if (a1.Count() == 0) { Console.WriteLine(d0(a2, d)); goto exit; }
        if (a2.Count() == 0) { Console.WriteLine(dd0(a1, d)); goto exit; }
        if (x == 0) { Console.WriteLine(d0(a2, d)); goto exit; }
        var ans = getAns(a1, a2, d);
        Console.WriteLine(ans);
        exit:;
    }
    static int getAns (List<int> a1, List<int> a2 , int d)
    {
        var a1c = a1.Count();   var a2c = a2.Count();
        if (d >= a1c + a2c * 2) return a1.Sum() + a2.Sum();
        var b1 = new int[a1c ];
        var b2 = new int[a2c ];
        b1[0] = a1[0];   b2[0] = a2[0];
        for (int i = 1; i < a1c; i++)
            b1[i] = b1[i - 1] + a1[i];
        for (int i = 1; i < a2c; i++)
            b2[i] = b2[i - 1] + a2[i];
        var ans1 = getC1(b1, b2, d);
        var ans2 = getC2(b1, b2, d);
        return Max(ans1, ans2);
    }
    static int getC2(int[] a, int[] b, int d)
    {
        var aL = a.Length;
        var bL = b.Length;
        var ans = 0;
        for (int i = 0; i < bL; i++)
        {
            var t = b[i];
            ans = Max(ans, t);
            var rd = d - 2 * (i + 1);
            if (rd == 0) break;
            else if (rd == 1) { t += a[0]; ans = Max(ans, t); break; }
            else
            {
                var w = Min(aL, rd);
                t += a[w - 1];
                ans = Max(ans, t);
            }
        }
        return ans;
    }
    static int getC1 (int[] a  , int[] b , int d)
    {
        var aL = a.Length;
        var bL = b.Length;
        var ans = 0;
        for (int i = 0; i < aL; i++)
        {
            var t = a[i];
            ans = Max(ans, t);
            var rd = d - i - 1;
            if (rd == 0) break;
            if (rd/2 >= 1)
            {
                var w = Min(rd / 2, bL);
                t += b[w - 1];
            }
            ans = Max(ans, t);
        }
        return ans;
    }
    static int d0(List<int> a, int d)
    {
        if (d >= a.Count() * 2) return a.Sum();
        var ans = 0;
        for (int i = 0; i < a.Count(); i++)
            if (d >= 2) { ans += a[i]; d-= 2; }
            else break;
        return ans;
    }
    static int dd0 (List<int> a , int d)
    {
        if (d >= a.Count()) return a.Sum();
        var ans = 0;
        for (int i = 0; i < a.Count(); i++)
            if (d >= 1) { ans += a[i]; d--; }
            else break;
        return ans;
    }
}



Measured memory_bytes: 20368