#include <iostream>
#include <map>
using namespace std;
const int N = 110;
char a[N],b[N];
int f[N][N];
int g[5][5]={//题意打标
{5,-1,-2 ,-1,-3},
{-1,5,-3 ,-2,-4},
{-2,-3,5 ,-2,-2},
{-1,-2, -2,5,-1},
{-3,-4, -2,-1,0}
};
map<char,int>m;
int main()
{
m['A']=0,m['C']=1,m['G']=2,m['T']=3,m['-']=4;
int n,q;
cin>>n>>a+1;
cin>>q>>b+1;
for(int i=1;i<=n;i++) f[i][0]=f[i-1][0]+g[m[a[i]]][4];
for(int i=1;i<=q;i++) f[0][i]=f[0][i-1]+g[4][m[b[i]]];
f[0][0]=0;//初始化
//DP
for(int i=1;i<=n;i++)
for(int j=1;j<=q;j++)
{
f[i][j]=max(f[i-1][j]+g[m[a[i]]][4],f[i][j-1]+g[4][m[b[j]]]);
f[i][j]=max(f[i][j],f[i-1][j-1]+g[m[a[i]]][m[b[j]]]);
}
cout<<f[n][q];
return 0;
}