/**
*    author: Dooloper
*    created: 15.05.2021 20:54:02
**/

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repp(i,k, n) for(int i = (int)(k); i < (int)(n); i++)
#define ALL(a)  (a).begin(),(a).end()
using ll = long long;
using P = pair<int, int>;
struct Edge{int to; ll w; Edge(int to, ll w) : to(to), w(w) {}};
using Graph = vector<vector<Edge>>;
const int di[] = {-1,0,1,0};
const int dj[] = {0,-1,0,1};

ll kaijo(int x, int k){
    int ans = 1;
    rep(i,k){
        ans *= x;
        x--;
    }
    return ans;
}

int main(){
    string s; cin >> s;
    vector<int> table(10,0);
    rep(i,10){
        if(s[i] == 'o') table[i] = 2;
        if(s[i] == '?') table[i] = 1;
        if(s[i] == 'x') table[i] = 0;
    }

    int ans = 0;

    rep(i,10){
        rep(j,10){
            rep(k,10){
                rep(l,10){
                    vector<bool> ok(10,false);
                    rep(m,10) ok[m] = false;
                    bool flag = true;
                    ok[i] = true;
                    ok[j] = true;
                    ok[k] = true;
                    ok[l] = true;
                    rep(m,10){
                        if(ok[m] == false && table[m] == 2) flag = false;
                        if(ok[m] == true && table[m] == 0) flag = false;
                    }
                    if(flag) ans ++;
                    //if(flag) cout << i << j << k << l << endl;
                }
            }
        }
    }
    cout << ans << endl;
}
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
typedef long long ll;
#define F first
#define S second
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl


int main(){
    int n,m;
    cin>>n>>m;
    vector<pair<int,int>> cnd(m); 
    rep(i,m){
        cin>>cnd.at(i).F>>cnd.at(i).S;
    }
    int k;
    cin>>k;
    vector<pair<int,int>> put(k);
    rep(i,k){
        cin>>put.at(i).F>>put.at(i).S;
    }
    int ans=0;
    rep(i,1<<k){
        vector<int> dish(n);
        rep(j,k){
            if((i>>j)%2==1){
                dish.at(put.at(j).F-1)=true;
            }
            else{
                dish.at(put.at(j).S-1)=true;
            }
        }
        int cnt=0;
        rep(i,m){
            if(dish.at(cnd.at(i).F-1)&&dish.at(cnd.at(i).S-1)==true){
                cnt++;
            }
        }
        ans=max(ans,cnt);
    }
    cout<<ans<<endl;
    return 0;
}
