#include<iostream>
#include<vector>
#include<map>
using namespace std;
vector<pair<int,int> >data;

int check(int d,int i)
{
  if(i>=data.size()|| d<=0)
    return 0;
  int ans=check(d,i+1);  
  if(data[i].first<=d)
    ans=max(ans,data[i].second+check(d-data[i].first,i+1));
  
  return ans;
}

int main()
{
  int d;
  cin>>d;
  int x;
  cin>>x;
  for(;x--;)
    {
      string a;
      int p;
      cin>>a>>p;
      if(a=="D")
	data.push_back(make_pair(1,p));
    }
  int y;
  cin>>y;
  for(;y--;)
    {
      string a;
      int p;
      cin>>a>>p;
      if(a=="DD")
	data.push_back(make_pair(2,p));
    }
  cout<<check(d,0)<<endl; 
}