//#pragma comment(linker,"/STACK:16777216") /*16Mb*/
//#pragma comment(linker,"/STACK:33554432") /*32Mb*/
#define _CRT_SECURE_NO_DEPRECATE
#include<sstream>
#include<iostream>
#include<numeric>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<memory>
#include<memory.h>
#include<string>
#include<vector>
#include<cctype>
#include<list>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<complex>
#include<set>
#include<algorithm>
using namespace std;
typedef unsigned long long ui64;
typedef long long i64;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef map<string,int> MSI;
typedef set<int> SI;
typedef set<string> SS;
typedef complex<double> CD;
typedef vector< CD > VCD;
typedef map<int,int> MII;
typedef pair<double,double> PDD;
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); --i)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define SZ(a) int((a).size())
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#ifdef _DEBUG
#define eprintf(...) fprintf (stderr, __VA_ARGS__)
#else
#define eprintf(...) assert (true)
#endif
const double PI = acos(-1.0);
typedef int FLOW;
typedef int COST;
const COST INF = 1000000000;
const FLOW FLOW_INF = 1000000000;
const int MAXN = 10000;
struct Edge {
int b, c, u, f, back;
};
class MCMF {
private:
int s, t, n;
vector<Edge> g[MAXN];
int *from, *from_edge, *d, *phi, *state, *q;
bool *used;
public:
MCMF(int _s, int _t, int _n) {
s = _s;
t = _t;
n = _n;
from = new int[n];
from_edge = new int[n];
d = new int[n];
state = new int[n];
q = new int[n];
phi = new int[n];
FOR(i,0,n)
phi[i] = 0;
used = new bool[n];
}
void addEdge(int a, int b, int c, int u) {
Edge e1 = {b,c,u,0,SZ(g[b])};
Edge e2 = {a,-c,0,0,SZ(g[a])};
g[a].PB(e1);
g[b].PB(e2);
}
void levit() {
int qh, qt;
qh = 0, qt = 0;
FOR(i,0,n) state[i] = 2, d[i] = INF;
memset(from,-1,sizeof(from));
state[s] = 1;
q[qh++] = s;
d[s] = 0;
while(qh!=qt) {
int v = q[qt++];
qt %= n;
state[v] = 0;
FOR(i,0,SZ(g[v])) if(g[v][i].f < g[v][i].u && d[g[v][i].b] > d[v] + g[v][i].c) {
int to = g[v][i].b;
d[to] = d[v] + g[v][i].c;
from[to] = v;
from_edge[to] = i;
if(state[to]==1)
continue;
if(state[to]==0) {
qt--;if(qt==-1) qt = n-1;q[qt] = to;
state[to] = 1;
}
else {
state[to] = 1;
q[qh++] = to;
qh %= n;
}
}
}
}
void dijkstra() {
memset(used,true,sizeof(used));
used[s] = false;
FOR(i,0,n)
d[i] = INF;
d[s] = 0;
while(true) {
int v = -1;
for(int i=0;i<n;i++) {
if(!used[i] && (v==-1 || d[v] > d[i]))
v = i;
}
if(v==-1)
break;
used[v] = true;
FOR(i,0,SZ(g[v])) if(g[v][i].f < g[v][i].u) {
int to = g[v][i].b;
if( d[to] > d[v] + g[v][i].c + phi[v] - phi[to] ) {
d[to] = d[v] + g[v][i].c + phi[v] - phi[to];
from[to] = v;
from_edge[to] = i;
used[to] = false;
}
}
}
}
pair<FLOW,COST> minCostMaxFlow() {
FLOW flow = 0;
COST cost = 0;
while(true) {
//levit();
dijkstra();
for (int i = 0; i < n; i++)
phi[i] += d[i];
if(d[t]==INF)
break;
int it = t;
FLOW addflow = FLOW_INF;
while(it!=s) {
addflow = min(addflow, g[from[it]][from_edge[it]].u - g[from[it]][from_edge[it]].f);
it = from[it];
}
it = t;
while(it!=s) {
g[from[it]][from_edge[it]].f += addflow;
g[it][g[from[it]][from_edge[it]].back].f -= addflow;
cost += g[from[it]][from_edge[it]].c * addflow;
it = from[it];
}
flow += addflow;
}
return MP(flow, cost);
}
};
int main() {
int n, c1, c2;
int test;
scanf("%d",&test);
while(test--) {
scanf("%d",&n);
VI cnt(n,0);
FOR(i,0,n) {
int tt;
scanf("%d",&tt);
tt--;
cnt[tt]++;
}
c1 = n;
c2 = c1+1;
MCMF t(c1,c2,n+2);
FOR(i,0,n) {
t.addEdge(c1,i,0,cnt[i]);
t.addEdge(i,c2,0,1);
}
int e;
scanf("%d",&e);
FOR(i,0,e) {
int a, b;
scanf("%d%d",&a,&b);
a--;b--;
t.addEdge(a,b,1,1000);
t.addEdge(b,a,1,1000);
}
pair<FLOW,COST> res = t.minCostMaxFlow();
printf("%d\n",res.Y);
}
return 0;
}
Ly8jcHJhZ21hIGNvbW1lbnQobGlua2VyLCIvU1RBQ0s6MTY3NzcyMTYiKSAvKjE2TWIqLwovLyNwcmFnbWEgY29tbWVudChsaW5rZXIsIi9TVEFDSzozMzU1NDQzMiIpIC8qMzJNYiovCiNkZWZpbmUgX0NSVF9TRUNVUkVfTk9fREVQUkVDQVRFCiNpbmNsdWRlPHNzdHJlYW0+CiNpbmNsdWRlPGlvc3RyZWFtPgojaW5jbHVkZTxudW1lcmljPgojaW5jbHVkZTxzc3RyZWFtPgojaW5jbHVkZTxjc3RkaW8+CiNpbmNsdWRlPGNzdGRsaWI+CiNpbmNsdWRlPGNtYXRoPgojaW5jbHVkZTxtZW1vcnk+CiNpbmNsdWRlPG1lbW9yeS5oPgojaW5jbHVkZTxzdHJpbmc+CiNpbmNsdWRlPHZlY3Rvcj4KI2luY2x1ZGU8Y2N0eXBlPgojaW5jbHVkZTxsaXN0PgojaW5jbHVkZTxxdWV1ZT4KI2luY2x1ZGU8ZGVxdWU+CiNpbmNsdWRlPHN0YWNrPgojaW5jbHVkZTxtYXA+CiNpbmNsdWRlPGNvbXBsZXg+CiNpbmNsdWRlPHNldD4KI2luY2x1ZGU8YWxnb3JpdGhtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnR5cGVkZWYgdW5zaWduZWQgbG9uZyBsb25nICAgICAgdWk2NDsKdHlwZWRlZiBsb25nIGxvbmcgICAgICAgICAgICAgICBpNjQ7CnR5cGVkZWYgICAgdmVjdG9yPGludD4gICAgICAgICAgICAgVkk7CnR5cGVkZWYJdmVjdG9yPGJvb2w+ICAgICAgICAgICAgVkI7CnR5cGVkZWYJdmVjdG9yPFZJPiAgICAgICAgICAgICAgVlZJOwp0eXBlZGVmCXZlY3RvcjxzdHJpbmc+ICAgICAgICAgIFZTOwp0eXBlZGVmCXBhaXI8aW50LGludD4gICAgICAgICAgIFBJSTsKdHlwZWRlZiBtYXA8c3RyaW5nLGludD4gICAgICAgICBNU0k7CnR5cGVkZWYgc2V0PGludD4gICAgICAgICAgICAgICAgU0k7CnR5cGVkZWYgc2V0PHN0cmluZz4gICAgICAgICAgICAgU1M7CnR5cGVkZWYgY29tcGxleDxkb3VibGU+ICAgICAgICAgQ0Q7CnR5cGVkZWYgdmVjdG9yPCBDRCA+ICAgICAgICAgICAgVkNEOwp0eXBlZGVmIG1hcDxpbnQsaW50PiAgICAgICAgICAgIE1JSTsKdHlwZWRlZglwYWlyPGRvdWJsZSxkb3VibGU+ICAgICBQREQ7CgojZGVmaW5lIFBCICAgICAgICAgICAgICAgICAgICAgIHB1c2hfYmFjawojZGVmaW5lIE1QICAgICAgICAgICAgICAgICAgICAgIG1ha2VfcGFpcgojZGVmaW5lIFggICAgICAgICAgICAgICAgICAgICAgIGZpcnN0CiNkZWZpbmUgWSAgICAgICAgICAgICAgICAgICAgICAgc2Vjb25kCiNkZWZpbmUgRk9SKGksIGEsIGIpICAgICAgICAgICAgZm9yKGludCBpID0gKGEpOyBpIDwgKGIpOyArK2kpCiNkZWZpbmUgUkZPUihpLCBhLCBiKSAgICAgICAgICAgZm9yKGludCBpID0gKGEpIC0gMTsgaSA+PSAoYik7IC0taSkKI2RlZmluZSBDTEVBUihhLCBiKSAgICAgICAgICAgICBtZW1zZXQoYSwgYiwgc2l6ZW9mKGEpKQojZGVmaW5lIFNaKGEpICAgICAgICAgICAgICAgICAgIGludCgoYSkuc2l6ZSgpKQojZGVmaW5lIEFMTChhKSAgIC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