s - >往M个设备流1, 然后每个设备往对应插口号流1,然后插口号之间的K个转换边流无穷大(代表该装备能被利用无穷遍),然后插排号流1到对应的N个插排,然后每个插排流1到汇点。
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <list>
#include <cstdlib>
#include <queue>
#include <stack>
#include <cmath>
#define ALL(a) a.begin(), a.end()
#define clr(a, x) memset(a, x, sizeof a)
#define fst first
#define snd second
#define pb push_back
#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rep1(i,x,y) for(int i=x;i<=y;i++)
#define rep(i,n) for(int i=0;i<(int)n;i++)
using namespace std;
const double eps = 1e-10;
typedef long long LL;
typedef long long ll;
typedef pair<int, int> pii;
const int oo =0x3f3f3f3f;
const int maxn = 401;
const int maxm = 500;
map<string,int> M;
int cnt_,rec[maxn] , plu[maxn];
int ID(string s){
if(M.count(s)) return M[s];
return M[s] = cnt_++;
}
struct Edge
{
int u, v, cap, flow;
Edge(int u, int v, int c, int f):u(u), v(v), cap(c), flow(f) {}
};
struct Dinic
{
int n, m, s, t, d[maxn], cur[maxn];
bool vis[maxn];
vector<Edge> edge;
vector<int> G[maxn];
void init(int n)
{
this->n=n;
for(int i=0; i<=n; i++)
G[i].clear();
edge.clear();
}
int AddEdge(int u, int v, int c)
{
edge.push_back(Edge(u, v, c, 0));
edge.push_back(Edge(v, u, 0, 0));
m=edge.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
return m-2;
}
bool bfs()
{
clr(vis, 0);
queue<int> q;
q.push(s);
d[s]=0, vis[s]=1;
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=0; i<G[x].size(); i++)
{
Edge& e=edge[G[x][i]];
if(!vis[e.v] && e.cap>e.flow)
{
vis[e.v]=1;
d[e.v]=d[x]+1;
q.push(e.v);
}
}
}
return vis[t];
}
int dfs(int x, int a)
{
if(x==t || a==0)return a;
int flow=0, f;
for(int& i=cur[x]; i<G[x].size(); i++)
{
Edge& e=edge[G[x][i]];
if(d[x]+1==d[e.v] && (f=dfs(e.v, min(a, e.cap-e.flow)))>0)
{
e.flow+=f;
edge[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0)break;
}
}
return flow;
}
int MaxFlow(int s, int t)
{
this->s=s;
this->t=t;
int flow=0;
while(bfs())
{
clr(cur, 0);
flow+=dfs(s, oo);
}
return flow;
}
} net;
int S,T,n,m,k,f[maxn],t[maxn];
int main()
{
//ios::sync_with_stdio(false);
int Ta;
scanf("%d",&Ta);
while(Ta--){
scanf("%d",&n);
cnt_ = 1 , M.clear();
string str , src;
char s[50];
clr(rec,0) , clr(plu,0);
rep1(i,1,n){
scanf("%s",s); src = s;
rec[ID(src)]++;
}
scanf("%d",&m);
char s1[50],s2[50];
rep1(i,1,m){
scanf("%s %s",s1,s2);
src = s2;
plu[ID(src)]++;
}
scanf("%d",&k);
rep1(i,1,k){
scanf("%s %s",s1,s2);
str = s1 , src = s2;
f[i] = ID(str),t[i]=ID(src);
}
S = 0 , T = cnt_;
net.init(T);
rep1(i,1,cnt_-1) if(plu[i]){
net.AddEdge(S,i,plu[i]);
}
rep1(i,1,cnt_-1) if(rec[i]){
net.AddEdge(i,T,rec[i]);
}
rep1(i,1,k) {
if(f[i] != t[i])
net.AddEdge(f[i],t[i],oo);
}
printf("%d\n",m-net.MaxFlow(S,T));
if(Ta) printf("\n");
}
return 0;
}