#include <bits/stdc++.h>
#include <fstream>
using namespace std;
#define read(type) readInt<type>() // Fast read
#define ll long long
#define nL "\n"
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define a first
#define b second
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define umap unordered_map
#define uset unordered_set
#define MOD 1000000007
#define imax INT_MAX
#define imin INT_MIN
#define exp 1e9
#define sz(x) (int((x).size()))
void solve() {
int x; cin >> x;
map<string, vector<string>> same;
for(auto i = 0; i < x; i++) {
string o, p; cin >> o >> p;
if (same.find(o) == same.end()) {
same[o] = {p};
} else {same[o].pb(p);}
if (same.find(p) == same.end()) {
same[p] = {o};
} else {same[p].pb(o);}
}
int y; cin >> y;
map<string, vector<string>> no;
for(auto i = 0; i < y; i++) {
string o, p; cin >> o >> p;
if (no.find(o) == no.end()) {
no[o] = {p};
} else {no[o].pb(p);}
if (no.find(p) == no.end()) {
no[p] = {o};
} else {no[p].pb(o);}
}
int ans = 0;
int q; cin >> q;
while(q--) {
string z, v, b; cin >> z >> v >> b;
vector<string> ppl = {z, v, b};
for(auto i = 0; i < 3; i++) {
if (same.find(ppl[i]) != same.end()) {
for(auto j = 0; j < 3; j++) {
if (i == j) {continue;}
if (find(all(same[ppl[i]]), ppl[j]) == same[ppl[i]].end()) {
ans++;
cout << "Found " << ppl[i] << " and " << ppl[j] << endl;
remove(all(same[ppl[i]]), ppl[j]);
remove(all(same[ppl[j]]), ppl[i]);
}
}
}
}
// for(auto i = 0; i < 3; i++) {
// if (no.find(ppl[i]) != no.end()) {
// for(auto j = 0; j < 3; j++) {
// if (i==j) {continue;}
// if (find(all(no[ppl[i]]), ppl[j]) != no[ppl[i]].end()) {
// ans++;
// remove(all(no[ppl[i]]), ppl[j]);
// remove(all(no[ppl[j]]), ppl[i]);
// }
// }
// }
// }
}
cout << ans << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}