#include<bits/stdc++.h>
using namespace std;
#define FOR(i,s,t) for(int i= s; i <t; i++)
using LL = long long; using ll = long long;
const int INF = 1e9;
const LL LINF = 1e18;
using pii = pair<int, int>; using PII = pii;
using PLL = pair<LL, LL>; using pll = PLL;


ll dp[220][110];
void solve() {
	ll D; cin >> D;
	ll x; cin >> x;
	vector<pll> events;
	for (int i = 0; i < x; i++) {
		string a; ll p; cin >> a >> p;
		if (a == "D") {
			events.push_back({ p,1 });
		}
	}
	ll y; cin >> y;
	for (int i = 0; i < y; i++) {
		string a; ll p; cin >> a >> p;
		if (a == "DD") {
			events.push_back({ p,2 });
		}
	}

	for (int i = 0; i < events.size(); i++) {
//		for (int j = 0; j <= 100; j++) {
			for (int k = 0; k <= 100; k++) {
				dp[i + 1][k] = max(dp[i + 1][k], dp[i][k]);
				if (k < events[i].second) continue;
				dp[i + 1][k] = max(dp[i + 1][k], dp[i][k - events[i].second] + events[i].first);
			}
//		}
	}
	
	ll ans = 0;
	for (int i = 0; i < D+1; i++) {
		ans = max(ans, (ll)dp[events.size()][i]);
	}
	cout << ans << endl;
}

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	solve();

	return 0;
}
