#include<bits/stdc++.h>
#define ll long long
using namespace std;

ll MX=100000000000000000LL;

ll ck(vector<ll> a, vector<ll> b)
{
    ll mn=MX;
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    for(int i=0;i<a.size();i++)
    {
        auto it=upper_bound(b.begin(), b.end(), a[i]);
        if(it!=b.end())
        {
            mn=min(mn, abs(*it-a[i]));
        }
        if(it!=b.begin())
        {
            it=prev(it);
            mn=min(mn, abs(*it-a[i]));
        }
    }
    return mn;

}

ll ck1(vector<ll> a, vector<ll> b, vector<ll> c)
{
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    sort(c.begin(), c.end());
    vector<ll> dl(a.size()), dr(a.size());
    for(int i=0;i<a.size();i++)
    {
        ll x=MX;
        auto it=upper_bound(b.begin(), b.end(), a[i]);
        if(it!=b.end())
        {
            x=min(x, abs(*it-a[i]));
        }
        if(it!=b.begin())
        {
            it=prev(it);
            x=min(x, abs(*it-a[i]));
        }
        if(i>0) dl[i]=min(x, dl[i-1]);
        else dl[i]=x;
    }
    for(int i=a.size()-1;i>=0;i--)
    {
        ll x=MX;
        auto it=upper_bound(b.begin(), b.end(), a[i]);
        if(it!=b.end())
        {
            x=min(x, abs(*it-a[i]));
        }
        if(it!=b.begin())
        {
            it=prev(it);
            x=min(x, abs(*it-a[i]));
        }
        if(i!=a.size()-1) dr[i]=min(x, dr[i+1]);
        else dr[i]=x;
    }
    
    ll mn=MX;
    for(int i=0;i<a.size();i++)
    {
        ll x=MX;
        auto it=upper_bound(c.begin(), c.end(), a[i]);
        if(it!=c.end())
        {
            x=min(x, abs(*it-a[i]));
        }
        if(it!=c.begin())
        {
            it=prev(it);
            x=min(x, abs(*it-a[i]));
        }
        ll y=MX;
        if(i>0) y=min(y, dl[i-1]);
        if(i+1<a.size()) y=min(y, dr[i+1]);
        mn=min(mn, x+y);
    }
    return mn;
}

int main()
{
    ll n;
    cin>>n;
    map<char, vector<ll> > mp;
    for(int i=1;i<=2*n;i++)
    {
        ll x;
        char c;
        cin>>x>>c;
        mp[c].push_back(x);
    }
    if(mp['B'].size()%2==0 and mp['R'].size()%2==0 and mp['G'].size()%2==0)
    {
        cout<<0<<endl;
        return 0;
    }
    if(mp['B'].size()%2==0)
    {
        ll x=ck(mp['R'], mp['G']);
        if(mp['B'].size()>=2)
            x=min(x, ck1(mp['B'], mp['R'], mp['G']));
        cout<<x<<endl;
        return 0;
    }

    if(mp['R'].size()%2==0)
    {
        ll x=ck(mp['B'], mp['G']);
        if(mp['R'].size()>=2)
        {
            x=min(x, ck1( mp['R'], mp['B'], mp['G'] ));
        }
        cout<<x<<endl;
        return 0;
    }

    if(mp['G'].size()%2==0)
    {
        ll x=ck(mp['R'], mp['B']);
        if(mp['G'].size()>=2)
        {
            x=min(x, ck1(mp['G'], mp['R'], mp['B']));
        }
        cout<<x<<endl;
        return 0;
    }

    return 0;
}
#include <stdio.h>

typedef long long ll;
int num[105][105], max = -1, vis[20], k, n, m;
struct node {
	int x1, x2;
} a[20], b[105];

void dfs(int now) {
	if (now >= k) {
		int sum = 0;
		//for (int i = 0; i < 17; i ++) printf("%d ", vis[i]);printf("\n");
		for (int i = 0; i < m; i ++) 
			if (vis[b[i].x1] && vis[b[i].x2]) 
				//printf("sum : %d + num[%d][%d] (%d)\n", sum, b[i].x1, b[i].x2, num[b[i].x1][b[i].x2]);
				sum ++;
		
		if (sum > max) max = sum;
		//printf("%d..sum : %d\n", now, sum);
		return;
	}
	int px1 = a[now].x1, px2 = a[now].x2;

	vis[px1] ++;
	//printf("%d pick : %d\n", now, px1);
	dfs(now + 1);
	vis[px1] --;



	vis[px2] ++;
	//printf("%d pick : %d\n", now, px2);
	dfs(now + 1);
	vis[px2] --;


}
int main() {
	int cnt = 0;
	scanf("%d %d", &n, &m);
	while (cnt < m) {
		scanf("%d %d", &b[cnt].x1, &b[cnt].x2);
		//num[b[cnt].x1][b[cnt].x2] ++;
		cnt ++;
	}
	int ind = 0;
	scanf("%d", &k);
	while (ind < k) {
		scanf("%d %d", &a[ind].x1, &a[ind].x2);
		ind ++;
	}
	dfs(0);
	//dfs(1);
	printf("%d\n", max);
	return 0;
}
