// ~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 haveCycle;
int color[N];
void checkCycle(int u) {
if (haveCycle) return;
color[u] = 1;
for(ii e : G[u]) {
int v = e.fi;
if (color[v] == 1) {
haveCycle = true;
return;
}
if (!color[v])
checkCycle(v);
}
color[u] = 2;
}
int dist[N];
int revDFS(int u) {
if (dist[u] != -1) return dist[u];
dist[u] = 0;
for(int v : revG[u]) maximize(dist[u], revDFS(v) + 1);
return dist[u];
}
void solve() {
FOR(i, 1, numNode) color[i] = 0;
haveCycle = false;
FOR(i, 1, numNode) if (!color[i]) {
checkCycle(i);
}
if (haveCycle) {
cout << "-1\n";
return;
}
FOR(i, 1, numNode) dist[i] = -1;
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) diameter = dist, 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+IG51bURpciAmJiBqID4gbnVtRGlyICYmIGFicyhpIC0gaikgPT0gbnVtVW5kaXIpIGNvbnRpbnVlOwogICAgICAgICAgICBhZGpbaV0ucGIoaik7CiAgICAgICAgfQoKICAgICAgICBjb3V0IDw8IGZvcmRCZWxsbWFuKCkgPDwgJ1xuJzsKICAgIH0KfQoKbmFtZXNwYWNlIFN1YnRhc2s0IHsKICAgIGJvb2wgY2hlY2soKSB7CiAgICAgICAgcmV0dXJuIG51bVVuZGlyID09IDA7CiAgICB9CgogICAgYm9vbCBoYXZlQ3ljbGU7CiAgICBpbnQgY29sb3JbTl07CgogICAgdm9pZCBjaGVja0N5Y2xlKGludCB1KSB7CiAgICAgICAgaWYgKGhhdmVDeWNsZSkgcmV0dXJuOwogICAgICAgIGNvbG9yW3VdID0gMTsKICAgICAgICBmb3IoaWkgZSA6IEdbdV0pIHsKICAgICAgICAgICAgaW50IHYgPSBlLmZpOwogICAgICAgICAgICBpZiAoY29sb3Jbdl0gPT0gMSkgewogICAgICAgICAgICAgICAgaGF2ZUN5Y2xlID0gdHJ1ZTsKICAgICAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoIWNvbG9yW3ZdKQogICAgICAgICAgICAgICAgY2hlY2tDeWNsZSh2KTsKICAgICAgICB9CiAgICAgICAgY29sb3JbdV0gPSAyOwogICAgfQoKICAgIGludCBkaXN0W05dOwogICAgaW50IHJldkRGUyhpbnQgdSkgewogICAgICAgIGlmIChkaXN0W3VdICE9IC0xKSByZXR1cm4gZGlzdFt1XTsKICAgICAgICBkaXN0W3VdID0gMDsKICAgICAgICBmb3IoaW50IHYgOiByZXZHW3VdKSBtYXhpbWl6ZShkaXN0W3VdLCByZXZERlModikgKyAxKTsKICAgICAgICByZXR1cm4gZGlzdFt1XTsKICAgIH0KCiAgICB2b2lkIHNvbHZlKCkgewogICAgICAgIEZPUihpLCAxLCBudW1Ob2RlKSBjb2xvcltpXSA9IDA7CiAgICAgICAgaGF2ZUN5Y2xlID0gZmFsc2U7CiAgICAgICAgRk9SKGksIDEsIG51bU5vZGUpIGlmICghY29sb3JbaV0pIHsKICAgICAgICAgICAgY2hlY2tDeWNsZShpKTsKICAgICAgICB9CgogICAgICAgIGlmIChoYXZlQ3ljbGUpIHsKICAgICAgICAgICAgY291dCA8PCAiLTFcbiI7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CgogICAgICAgIEZPUihpLCAxLCBudW1Ob2RlKSBkaXN0W2ldID0gLTE7CiAgICAgICAgaW50IGFucyA9IDA7CiAgICAgICAgRk9SKGksIDEsIG51bU5vZGUpIG1heGltaXplKGFucywgcmV2REZTKGkpKTsKICAgICAgICBjb3V0IDw8IGFucyA8PCAnXG4nOwogICAgfQp9CgpuYW1lc3BhY2UgU3VidGFzazUgewogICAgYm9vbCBjaGVjaygpIHsKICAgICAgICByZXR1cm4gbnVtRGlyID09IDA7CiAgICB9CgogICAgaW50IGxhYltOXTsKICAgIGludCByb290KGludCB2KSB7CiAgICAgICAgcmV0dXJuIChsYWJbdl0gPCAwID8gdiA6IGxhYlt2XSA9IHJvb3QobGFiW3ZdKSk7CiAgICB9CgogICAgYm9vbCB1bml0ZShpbnQgdSwgaW50IHYpIHsKICAgICAgICB1ID0gcm9vdCh1KTsgdiA9IHJvb3Qodik7CiAgICAgICAgaWYgKHUgPT0gdikgcmV0dXJuIGZhbHNlOwogICAgICAgIGlmIChsYWJbdV0gPiBsYWJbdl0pIHN3YXAodSwgdik7CiAgICAgICAgbGFiW3VdICs9IGxhYlt2XTsKICAgICAgICBsYWJbdl0gPSB1OwogICAgICAgIHJldHVybiB0cnVlOwogICAgfQoKICAgIGJvb2wgdmlzW05dOwogICAgaW50IHRvcCwgZGlhbWV0ZXI7CiAgICB2b2lkIGRmcyhpbnQgdSwgaW50IHBhciwgaW50IGRpc3QgPSAwKSB7CiAgICAgICAgaWYgKGRpc3QgPiBkaWFtZXRlcikgZGlhbWV0ZXIgPSBkaXN0LCB0b3AgPSB1OwogICAgICAgIHZpc1t1XSA9IHRydWU7CiAgICAgICAgZm9yKGlpIGUgOiBHW3VdKSB7CiAgICAgICAgICAgIGludCB2ID0gZS5maTsKICAgICAgICAgICAgaWYgKHYgPT0gcGFyKSBjb250aW51ZTsKICAgICAgICAgICAgZGZzKHYsIHUsIGRpc3QgKyAxKTsKICAgICAgICB9CiAgICB9CgogICAgdm9pZCBzb2x2ZSgpIHsKICAgICAgICBGT1IoaSwgMSwgbnVtTm9kZSkgbGFiW2ldID0gLTE7CiAgICAgICAgYm9vbCBoYXZlQ3ljbGUgPSBmYWxzZTsKICAgICAgICBGT1IoaSwgMSwgbnVtVW5kaXIpCiAgICAgICAgICAgIGlmICghdW5pdGUoZWRnZVtpXS5maSwgZWRnZVtpXS5zZSkpIGhhdmVDeWNsZSA9IHRydWU7CgogICAgICAgIGlmIChoYXZlQ3ljbGUpIHsKICAgICAgICAgICAgY291dCA8PCAiLTFcbiI7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CgogICAgICAgIEZPUihpLCAxLCBudW1Ob2RlKSB2aXNbaV0gPSBmYWxzZTsKICAgICAgICBpbnQgYW5zID0gMDsKICAgICAgICBGT1IoaSwgMSwgbnVtTm9kZSkgaWYgKCF2aXNbaV0pIHsKICAgICAgICAgICAgZGlhbWV0ZXIgPSAwOwogICAgICAgICAgICBkZnMoMSwgLTEpOwogICAgICAgICAgICBkZnModG9wLCAtMSk7CiAgICAgICAgICAgIG1heGltaXplKGFucywgZGlhbWV0ZXIpOwogICAgICAgIH0KICAgICAgICBjb3V0IDw8IGFucyA8PCAnXG4nOwogICAgfQp9Cgp2b2lkIHByb2Nlc3MoKSB7CiAgICBpZiAoU3VidGFzazEyMyA6OiBjaGVjaygpKSBTdWJ0YXNrMTIzIDo6IHNvbHZlKCk7CiAgICBlbHNlIGlmIChTdWJ0YXNrNCA6OiBjaGVjaygpKSBTdWJ0YXNrNCA6OiBzb2x2ZSgpOwogICAgZWxzZSBpZiAoU3VidGFzazUgOjogY2hlY2soKSkgU3VidGFzazUgOjogc29sdmUoKTsKfQoKaW50IG1haW4oKSB7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOwogICAgY2luLnRpZSgwKTsgY291dC50aWUoMCk7CiAgICBpZiAoZm9wZW4odGFzayIuaW5wIiwgInIiKSkgewogICAgICAgIGZyZW9wZW4odGFzayIuaW5wIiwgInIiLCBzdGRpbik7CiAgICAgICAgZnJlb3Blbih0YXNrIi5vdXQiLCAidyIsIHN0ZG91dCk7CiAgICB9CiAgICBpbnQgdGMgPSAxOwogICAgY2luID4+IHRjOwogICAgd2hpbGUodGMtLSkgewogICAgICAgIGluaXQoKTsKICAgICAgICBwcm9jZXNzKCk7CiAgICB9CiAgICByZXR1cm4gMDsKfQo=