#include<bits/stdc++.h>
using namespace std;
using ld = long double;
#define int long long
#define endl '\n'
#define all(x) x.begin(), x.end()

const int mod = 1e9 + 7;


void solve() {
    int n; cin >> n;
    array<vector<string>, 5> mpch;
    map<string, int> nonchamp;
    vector<pair<string,int>> champ;
    map<string, bool> ch;
    for(int i = 0; i < 5*n; i++) {
        string s; cin >> s;
        ch[s] = true;
    }
    int m; cin >> m;

    int noncnt = 0, cnt = 0;
    for(int i = 0; i < m; i++) {
        string s; cin >> s;
        int pos; cin >> pos;
        if(ch.find(s) != ch.end()) {
            cnt++; 
            mpch[pos-1].push_back(s);
        } else {
            noncnt++; nonchamp[s] = pos-1; 
        }
        // cout << s << ' ' << pos << '\n';
    }
    int mx=  0;
    for(int i = 0; i < 5; i++) mx = max(mx, (int)mpch[i].size());
    for(int i = 0; i < mx; i++) {
        for(int j = 0; j < 5; j++) {
            // cout << j << ' ' << i << '\n';
            if(mpch[j].size() <= i) continue;
            champ.push_back({mpch[j][i], j});
        }
    }
    vector<array<int, 5>> teams(cnt);
    vector<int> rep(cnt);
    vector<bool> isteam(cnt);

    int ind = 0;
    for(auto& [s, y] : champ) {
        teams[ind][y] = true;
        rep[ind] = y;
        ind++;
    }

    for(int i = 0; i < cnt; i++) {
        if(noncnt >= 4) {
            noncnt -= 4;
            isteam[i] = true;
        } else {
            isteam[i] = false;
            for(int j = i-1; j >= 0; j--) {
                if(isteam[j] == false) continue;
                else {
                    if(teams[j][rep[i]] == false) {
                        teams[j][rep[i]] = true;
                        noncnt++;
                        break;
                    }
                }
            }
        }
    }
    int ans = 0;
    for(int i = 0; i < champ.size(); i++) {
        if(isteam[i]) ans++;
    }
    cout << ans << '\n';

}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(nullptr);
    int tc = 1;
    // cin >> tc;
    for(int i = 1; i <= tc; i++) {
        solve();
    }
}