#include <bits/stdc++.h>

#define ascent(i, a, b) for(int i = a; i < b; i ++)
#define loop(_n) for(int lp; lp < _n; lp ++)

#define pic pair<int, char>
#define pist pair<int, string>

using namespace std;

int D;
vector<int> ap;
vector<int> bcq;

int search(int level, int ans, int d) {
  int point;
  int nxt_d;

  if(level >= ap.size() + bcq.size()) { return ans; }
  if(d <= 0) { return ans; }

  if(level >= ap.size() && d >= 2) {
    point = bcq[level-ap.size()];
    nxt_d = 2;
  }
  else if(level >= ap.size() && d < 2) {
    return ans;
  }
  else if(level < ap.size()) {
    point = ap[level];
    nxt_d = 1;
  }
  
  return max(search(level+1, ans+point, d-nxt_d), search(level+1, ans, d));
}

int main() {
  cin >> D;
  int x;
  cin >> x;

  ascent(i, 0, x) {
    char a;
    int  p;
    cin >> a >> p;
    if(a == 'D') ap.push_back(p);
  }

  int y;
  cin >> y;
  ascent(i, 0, y) {
    string bc;
    int q;
    cin >> bc >> q;
    if(bc == "DD") bcq.push_back(q);
  }

  cout << search(0, 0, D) << endl;
  return 0;
}