// ~icebear love attt~
#include<bits/stdc++.h>
using namespace std;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FORR(i, a, b) for(int i = (a); i >= (b); i--)
#define REP(i, n) for(int i = 0; i < (n); i++)
#define RED(i, n) for(int i = (n) - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define MASK(i) (1LL << (i))
#define task "fakerfmvp"
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 2e5 + 5;
int numNode, numUndir, numDir;
vector<ii> G[N];
vector<int> revG[N];
int numState;
ii edge[3 * N];
void init() {
cin >> numNode >> numUndir >> numDir;
numState = 2 * numUndir + numDir;
FOR(i, 1, numNode) {
G[i].clear();
revG[i].clear();
}
FOR(i, 1, numUndir) {
int u, v;
cin >> u >> v;
G[u].pb(mp(v, i + numDir));
G[v].pb(mp(u, i + numDir));
edge[i + numDir] = mp(u, v);
edge[i + numUndir + numDir] = mp(v, u);
}
FOR(i, 1, numDir) {
int u, v;
cin >> u >> v;
edge[i] = mp(u, v);
G[u].pb(mp(v, i));
revG[v].pb(u);
}
}
namespace Subtask123 {
bool check() {
return max({numNode, numDir, numUndir}) <= 1000;
}
int dist[N], countState[N];
vector<int> adj[N];
int fordBellman() {
queue<int> q;
FOR(i, 1, numState) {
countState[i] = 0;
dist[i] = -1;
q.push(i);
}
while(!q.empty()) {
int state = q.front(); q.pop();
if (++countState[state] > numState) return -1;
for(int nxt : adj[state])
if (minimize(dist[nxt], dist[state] - 1))
q.push(nxt);
}
return -(*min_element(dist + 1, dist + numState + 1));
}
void solve() {
FOR(i, 1, numState) adj[i].clear();
FOR(i, 1, numState) FOR(j, 1, numState) if (edge[i].se == edge[j].fi) {
if (i > numDir && j > numDir && abs(i - j) == numUndir) continue;
adj[i].pb(j);
}
cout << fordBellman() << '\n';
}
}
namespace Subtask4 {
bool check() {
return numUndir == 0;
}
bool vis[N], haveCycle;
void checkCycle(int u) {
if (haveCycle) return;
vis[u] = true;
for(ii e : G[u]) {
int v = e.fi;
if (vis[v]) {
haveCycle = true;
return;
}
checkCycle(v);
}
}
int dist[N];
int revDFS(int u) {
if (dist[u]) return dist[u];
for(int v : revG[u])
dist[u] = max(dist[u], revDFS(v) + 1);
return dist[u];
}
void solve() {
FOR(i, 1, numNode) vis[i] = false;
haveCycle = false;
FOR(i, 1, numNode) if (!vis[i]) {
checkCycle(i);
}
if (haveCycle) {
cout << "-1\n";
return;
}
FOR(i, 1, numNode) dist[i] = 0;
int ans = 0;
FOR(i, 1, numNode) maximize(ans, revDFS(i));
cout << ans << '\n';
}
}
namespace Subtask5 {
bool check() {
return numDir == 0;
}
int lab[N];
int root(int v) {
return (lab[v] < 0 ? v : lab[v] = root(lab[v]));
}
bool unite(int u, int v) {
u = root(u); v = root(v);
if (u == v) return false;
if (lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
bool vis[N];
int top, diameter;
void dfs(int u, int par, int dist = 0) {
if (dist > diameter) top = u;
vis[u] = true;
for(ii e : G[u]) {
int v = e.fi;
if (v == par) continue;
dfs(v, u, dist + 1);
}
}
void solve() {
FOR(i, 1, numNode) lab[i] = -1;
bool haveCycle = false;
FOR(i, 1, numUndir)
if (!unite(edge[i].fi, edge[i].se)) haveCycle = true;
if (haveCycle) {
cout << "-1\n";
return;
}
FOR(i, 1, numNode) vis[i] = false;
int ans = 0;
FOR(i, 1, numNode) if (!vis[i]) {
diameter = 0;
dfs(1, -1);
dfs(top, -1);
maximize(ans, diameter);
}
cout << ans << '\n';
}
}
void process() {
if (Subtask123 :: check()) Subtask123 :: solve();
else if (Subtask4 :: check()) Subtask4 :: solve();
else if (Subtask5 :: check()) Subtask5 :: solve();
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
cin >> tc;
while(tc--) {
init();
process();
}
return 0;
}
Ly8gfmljZWJlYXIgbG92ZSBhdHR0fgojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdGVtcGxhdGU8Y2xhc3MgVD4KICAgIGJvb2wgbWluaW1pemUoVCAmYSwgY29uc3QgVCAmYikgewogICAgICAgIGlmIChhID4gYikgcmV0dXJuIGEgPSBiLCB0cnVlOwogICAgICAgIHJldHVybiBmYWxzZTsKICAgIH0KCnRlbXBsYXRlPGNsYXNzIFQ+CiAgICBib29sIG1heGltaXplKFQgJmEsIGNvbnN0IFQgJmIpIHsKICAgICAgICBpZiAoYSA8IGIpIHJldHVybiBhID0gYiwgdHJ1ZTsKICAgICAgICByZXR1cm4gZmFsc2U7CiAgICB9Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBwYWlyPGludCwgaW50PiBpaTsKdHlwZWRlZiBwYWlyPGludCwgaWk+IGlpaTsKCiNkZWZpbmUgRk9SKGksIGEsIGIpIGZvcihpbnQgaSA9IChhKTsgaSA8PSAoYik7IGkrKykKI2RlZmluZSBGT1JSKGksIGEsIGIpIGZvcihpbnQgaSA9IChhKTsgaSA+PSAoYik7IGktLSkKI2RlZmluZSBSRVAoaSwgbikgZm9yKGludCBpID0gMDsgaSA8IChuKTsgaSsrKQojZGVmaW5lIFJFRChpLCBuKSBmb3IoaW50IGkgPSAobikgLSAxOyBpID49IDA7IGktLSkKI2RlZmluZSBhbGwodikgdi5iZWdpbigpLCB2LmVuZCgpCiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSBNQVNLKGkpICgxTEwgPDwgKGkpKQojZGVmaW5lIHRhc2sgImZha2VyZm12cCIKCmNvbnN0IGludCBNT0QgPSAxZTkgKyA3Owpjb25zdCBpbnQgaW5mID0gMWU5ICsgMjcwOTIwMDg7CmNvbnN0IGxsIElORiA9IDFlMTggKyAyNzA5MjAwODsKY29uc3QgaW50IE4gPSAyZTUgKyA1OwppbnQgbnVtTm9kZSwgbnVtVW5kaXIsIG51bURpcjsKdmVjdG9yPGlpPiBHW05dOwp2ZWN0b3I8aW50PiByZXZHW05dOwppbnQgbnVtU3RhdGU7CmlpIGVkZ2VbMyAqIE5dOwoKdm9pZCBpbml0KCkgewogICAgY2luID4+IG51bU5vZGUgPj4gbnVtVW5kaXIgPj4gbnVtRGlyOwoKICAgIG51bVN0YXRlID0gMiAqIG51bVVuZGlyICsgbnVtRGlyOwogICAgRk9SKGksIDEsIG51bU5vZGUpIHsKICAgICAgICBHW2ldLmNsZWFyKCk7CiAgICAgICAgcmV2R1tpXS5jbGVhcigpOwogICAgfQoKICAgIEZPUihpLCAxLCBudW1VbmRpcikgewogICAgICAgIGludCB1LCB2OwogICAgICAgIGNpbiA+PiB1ID4+IHY7CiAgICAgICAgR1t1XS5wYihtcCh2LCBpICsgbnVtRGlyKSk7CiAgICAgICAgR1t2XS5wYihtcCh1LCBpICsgbnVtRGlyKSk7CiAgICAgICAgZWRnZVtpICsgbnVtRGlyXSA9IG1wKHUsIHYpOwogICAgICAgIGVkZ2VbaSArIG51bVVuZGlyICsgbnVtRGlyXSA9IG1wKHYsIHUpOwogICAgfQogICAgRk9SKGksIDEsIG51bURpcikgewogICAgICAgIGludCB1LCB2OwogICAgICAgIGNpbiA+PiB1ID4+IHY7CiAgICAgICAgZWRnZVtpXSA9IG1wKHUsIHYpOwogICAgICAgIEdbdV0ucGIobXAodiwgaSkpOwogICAgICAgIHJldkdbdl0ucGIodSk7CiAgICB9Cn0KCm5hbWVzcGFjZSBTdWJ0YXNrMTIzIHsKICAgIGJvb2wgY2hlY2soKSB7CiAgICAgICAgcmV0dXJuIG1heCh7bnVtTm9kZSwgbnVtRGlyLCBudW1VbmRpcn0pIDw9IDEwMDA7CiAgICB9CgogICAgaW50IGRpc3RbTl0sIGNvdW50U3RhdGVbTl07CiAgICB2ZWN0b3I8aW50PiBhZGpbTl07CgogICAgaW50IGZvcmRCZWxsbWFuKCkgewogICAgICAgIHF1ZXVlPGludD4gcTsKICAgICAgICBGT1IoaSwgMSwgbnVtU3RhdGUpIHsKICAgICAgICAgICAgY291bnRTdGF0ZVtpXSA9IDA7CiAgICAgICAgICAgIGRpc3RbaV0gPSAtMTsKICAgICAgICAgICAgcS5wdXNoKGkpOwogICAgICAgIH0KCiAgICAgICAgd2hpbGUoIXEuZW1wdHkoKSkgewogICAgICAgICAgICBpbnQgc3RhdGUgPSBxLmZyb250KCk7IHEucG9wKCk7CiAgICAgICAgICAgIGlmICgrK2NvdW50U3RhdGVbc3RhdGVdID4gbnVtU3RhdGUpIHJldHVybiAtMTsKICAgICAgICAgICAgZm9yKGludCBueHQgOiBhZGpbc3RhdGVdKQogICAgICAgICAgICAgICAgaWYgKG1pbmltaXplKGRpc3Rbbnh0XSwgZGlzdFtzdGF0ZV0gLSAxKSkKICAgICAgICAgICAgICAgICAgICBxLnB1c2gobnh0KTsKICAgICAgICB9CgogICAgICAgIHJldHVybiAtKCptaW5fZWxlbWVudChkaXN0ICsgMSwgZGlzdCArIG51bVN0YXRlICsgMSkpOwogICAgfQoKICAgIHZvaWQgc29sdmUoKSB7CiAgICAgICAgRk9SKGksIDEsIG51bVN0YXRlKSBhZGpbaV0uY2xlYXIoKTsKICAgICAgICBGT1IoaSwgMSwgbnVtU3RhdGUpIEZPUihqLCAxLCBudW1TdGF0ZSkgaWYgKGVkZ2VbaV0uc2UgPT0gZWRnZVtqXS5maSkgewogICAgICAgICAgICBpZiAoaSA+IG51bURpciAmJiBqID4gbnVtRGlyICYmIGFicyhpIC0gaikgPT0gbnVtVW5kaXIpIGNvbnRpbnVlOwogICAgICAgICAgICBhZGpbaV0ucGIoaik7CiAgICAgICAgfQoKICAgICAgICBjb3V0IDw8IGZvcmRCZWxsbWFuKCkgPDwgJ1xuJzsKICAgIH0KfQoKbmFtZXNwYWNlIFN1YnRhc2s0IHsKICAgIGJvb2wgY2hlY2soKSB7CiAgICAgICAgcmV0dXJuIG51bVVuZGlyID09IDA7CiAgICB9CgogICAgYm9vbCB2aXNbTl0sIGhhdmVDeWNsZTsKICAgIHZvaWQgY2hlY2tDeWNsZShpbnQgdSkgewogICAgICAgIGlmIChoYXZlQ3ljbGUpIHJldHVybjsKICAgICAgICB2aXNbdV0gPSB0cnVlOwogICAgICAgIGZvcihpaSBlIDogR1t1XSkgewogICAgICAgICAgICBpbnQgdiA9IGUuZmk7CiAgICAgICAgICAgIGlmICh2aXNbdl0pIHsKICAgICAgICAgICAgICAgIGhhdmVDeWNsZSA9IHRydWU7CiAgICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY2hlY2tDeWNsZSh2KTsKICAgICAgICB9CiAgICB9CgogICAgaW50IGRpc3RbTl07CiAgICBpbnQgcmV2REZTKGludCB1KSB7CiAgICAgICAgaWYgKGRpc3RbdV0pIHJldHVybiBkaXN0W3VdOwogICAgICAgIGZvcihpbnQgdiA6IHJldkdbdV0pCiAgICAgICAgICAgIGRpc3RbdV0gPSBtYXgoZGlzdFt1XSwgcmV2REZTKHYpICsgMSk7CiAgICAgICAgcmV0dXJuIGRpc3RbdV07CiAgICB9CgogICAgdm9pZCBzb2x2ZSgpIHsKICAgICAgICBGT1IoaSwgMSwgbnVtTm9kZSkgdmlzW2ldID0gZmFsc2U7CiAgICAgICAgaGF2ZUN5Y2xlID0gZmFsc2U7CiAgICAgICAgRk9SKGksIDEsIG51bU5vZGUpIGlmICghdmlzW2ldKSB7CiAgICAgICAgICAgIGNoZWNrQ3ljbGUoaSk7CiAgICAgICAgfQoKICAgICAgICBpZiAoaGF2ZUN5Y2xlKSB7CiAgICAgICAgICAgIGNvdXQgPDwgIi0xXG4iOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQoKICAgICAgICBGT1IoaSwgMSwgbnVtTm9kZSkgZGlzdFtpXSA9IDA7CiAgICAgICAgaW50IGFucyA9IDA7CiAgICAgICAgRk9SKGksIDEsIG51bU5vZGUpIG1heGltaXplKGFucywgcmV2REZTKGkpKTsKICAgICAgICBjb3V0IDw8IGFucyA8PCAnXG4nOwogICAgfQp9CgpuYW1lc3BhY2UgU3VidGFzazUgewogICAgYm9vbCBjaGVjaygpIHsKICAgICAgICByZXR1cm4gbnVtRGlyID09IDA7CiAgICB9CgogICAgaW50IGxhYltOXTsKICAgIGludCByb290KGludCB2KSB7CiAgICAgICAgcmV0dXJuIChsYWJbdl0gPCAwID8gdiA6IGxhYlt2XSA9IHJvb3QobGFiW3ZdKSk7CiAgICB9CgogICAgYm9vbCB1bml0ZShpbnQgdSwgaW50IHYpIHsKICAgICAgICB1ID0gcm9vdCh1KTsgdiA9IHJvb3Qodik7CiAgICAgICAgaWYgKHUgPT0gdikgcmV0dXJuIGZhbHNlOwogICAgICAgIGlmIChsYWJbdV0gPiBsYWJbdl0pIHN3YXAodSwgdik7CiAgICAgICAgbGFiW3VdICs9IGxhYlt2XTsKICAgICAgICBsYWJbdl0gPSB1OwogICAgICAgIHJldHVybiB0cnVlOwogICAgfQoKICAgIGJvb2wgdmlzW05dOwogICAgaW50IHRvcCwgZGlhbWV0ZXI7CiAgICB2b2lkIGRmcyhpbnQgdSwgaW50IHBhciwgaW50IGRpc3QgPSAwKSB7CiAgICAgICAgaWYgKGRpc3QgPiBkaWFtZXRlcikgdG9wID0gdTsKICAgICAgICB2aXNbdV0gPSB0cnVlOwogICAgICAgIGZvcihpaSBlIDogR1t1XSkgewogICAgICAgICAgICBpbnQgdiA9IGUuZmk7CiAgICAgICAgICAgIGlmICh2ID09IHBhcikgY29udGludWU7CiAgICAgICAgICAgIGRmcyh2LCB1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c2siLmlucCIsICJyIikpIHsKICAgICAgICBmcmVvcGVuKHRhc2siLmlucCIsICJyIiwgc3RkaW4pOwogICAgICAgIGZyZW9wZW4odGFzayIub3V0IiwgInciLCBzdGRvdXQpOwogICAgfQogICAgaW50IHRjID0gMTsKICAgIGNpbiA+PiB0YzsKICAgIHdoaWxlKHRjLS0pIHsKICAgICAgICBpbml0KCk7CiAgICAgICAgcHJvY2VzcygpOwogICAgfQogICAgcmV0dXJuIDA7Cn0=