#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;

#define rep(i, n) for(int i=0; i<n; ++i)
#define max(a, b) (a>b?a:b)
#define min(a, b) (a>b?b:a)

int main() {
  int d, x, y;
  vector<int> P, Q;
  cin >> d;
  cin >> x;
  rep(i, x) {
    char a; int p;
    cin >> a >> p;
    if(a=='D') P.push_back(p);
  }
  sort(P.begin(), P.end(), greater<int>());

  cin >> y;
  rep(i, y) {
    char b, c; int q;
    cin >> b >> c >> q;
    if(b=='D' && c=='D') Q.push_back(q);
  }
  sort(Q.begin(), Q.end(), greater<int>());

  int c = min(d, (int)P.size())+1;
  int ans = 0, pnt;
  rep(i, c) {
    pnt = 0;
    rep(j, i) pnt += P[j];
    rep(j, min((d-i)/2, Q.size())) pnt += Q[j];
    ans = max(ans, pnt);
  }
  cout << ans << endl;
  return 0;
}