//#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 a, b, c, u, f, back, mine;
};
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 = {a,b,c,u,0,SZ(g[b]), SZ(g[a])};
Edge e2 = {b,a,-c,0,0,SZ(g[a]), SZ(g[b])};
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;
set<pair<int,int> > Set;
FOR(i,0,n)
Set.insert(MP(d[i],i));
while(!Set.empty()) {
PII r = *Set.begin();
Set.erase(Set.begin());
int v = r.Y;
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] ) {
Set.erase (make_pair (d[to], to));
d[to] = d[v] + g[v][i].c + phi[v] - phi[to];
from[to] = v;
from_edge[to] = i;
Set.insert (make_pair (d[to], to));
}
}
}
}
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+PSAoYik7IC0taSkKI2RlZmluZSBDTEVBUihhLCBiKSAgICAgICAgICAgICBtZW1zZXQoYSwgYiwgc2l6ZW9mKGEpKQojZGVmaW5lIFNaKGE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