#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;

int dp[21][14];
int main() {
	int d, x; scanf("%d%d", &d, &x);
	rep(i, x) {
		string a; int p; cin >> a >> p;
		rep(j, d + 1) {
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
			if (a == "D"&&j + 1 <= d)dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + p);
		}
	}
	int y; scanf("%d", &y);
	rep(i, y) {
		string b; int p; cin >> b >> p;
		rep(j, d + 1) {
			dp[x + i + 1][j] = max(dp[x + i + 1][j], dp[x + i][j]);
			if (b == "DD"&&j + 2 <= d)dp[x + i + 1][j + 2] = max(dp[x + i + 1][j + 2], dp[x + i][j] + p);
		}
	}
	printf("%d\n", dp[x + y][d]);
}