#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
int main(){
  int co,n;
  cin>>co>>n;
  vector<int>in1;
  vector<int>in2;
  rep(i,n){
    string s;
    int in;
    cin>>s>>in;
    if(s=="D")in1.pb(in);
  }
  cin>>n;
  rep(i,n){
    string s;
    int in;
    cin>>s>>in;
    if(s=="DD")in2.pb(in);
  }
  sort(all(in1));
  sort(all(in2));
  int maxi=0;  
  for(int i=0;i*2<=co;i++){
    int sum=0;
    rep(j,i)if(in2.size()&&in2.size()-1-j<1e8&&(in2.size()-1-j>=0))sum+=in2[in2.size()-1-j];
    for(int j=0;j<co-i*2;j++)if(in1.size()&&in1.size()-1-j<1e8&&(in1.size()-1-j>=0))sum+=in1[in1.size()-1-j];
    maxi=max(maxi,sum);
  }
  cout<<maxi<<endl;
}