#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = int(3e5)+7;
struct node{
int n , sz[maxN];
ll dp[maxN] , cost = 0;
int ver = -1;
vector<int> g[maxN];
vector<ii> E;
void init(int _n){
n = _n;
cost = 0;
ver = -1;
for (int i = 1 ; i <= n ; i++){
g[i].clear();
dp[i] = 0;
}
E.clear();
}
void add_edge(int u , int v){
g[u].push_back(v);
g[v].push_back(u);
E.push_back({u , v});
}
void dfs_init(int u , int par){
sz[u] = 1;
dp[u] = 0;
for (int v : g[u]){
if (v != par){
dfs_init(v , u);
sz[u] += sz[v];
dp[u] += dp[v] + 1ll * sz[v];
}
}
}
void change(int u , int v){
sz[u] -= sz[v];
dp[u] -= dp[v] + 1ll * sz[v];
sz[v] += sz[u];
dp[v] += dp[u] + 1ll * sz[u];
}
void dfs_reroot(int u , int par){
cost += dp[u];
if (ver == -1 || dp[ver] < dp[u]) ver = u;
for (int v : g[u]){
if (v != par){
change(u , v);
dfs_reroot(v , u);
change(v , u);
}
}
}
} f[3];
node add(node X , node Y){
int n = X.n + Y.n;
node ans; ans.init(n);
for (auto it : X.E){
ans.add_edge(it.fi , it.se);
}
for (auto it : Y.E){
ans.add_edge(it.fi + X.n , it.se + X.n);
}
ans.add_edge(X.ver , Y.ver + X.n);
ans.dfs_init(1 , 0);
ans.dfs_reroot(1 , 0);
return ans;
}
void solve(){
for (int i = 0 ; i < 3 ; i++){
int n; cin >> n;
f[i].init(n);
for (int j = 1 ; j < n ; j++){
int u , v;
cin >> u >> v;
f[i].add_edge(u , v);
}
f[i].dfs_init(1 , 0);
f[i].dfs_reroot(1 , 0);
}
node A = add(add(f[0] , f[1]) , f[2]);
node B = add(add(f[1] , f[2]) , f[0]);
node C = add(add(f[2] , f[0]) , f[1]);
cout << max(A.cost , max(B.cost , C.cost)) / 2 << "\n";
}
#define name "A"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; cin >> t;
solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBhbGwodikgdi5iZWdpbigpICwgdi5lbmQoKQojZGVmaW5lIHN6KHYpIGludCh2LnNpemUoKSkKI2RlZmluZSB1bnEodikgc29ydChhbGwodikpOyB2LnJlc2l6ZSh1bmlxdWUoYWxsKHYpKSAtIHYuYmVnaW4oKSk7CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBwYWlyPGludCAsIGludD4gaWk7CnR5cGVkZWYgcGFpcjxsb25nIGxvbmcgLCBpbnQ+IGxsaTsKCmNvbnN0IGludCBtYXhOID0gaW50KDNlNSkrNzsKCnN0cnVjdCBub2RlewogICAgaW50IG4gLCBzelttYXhOXTsKICAgIGxsIGRwW21heE5dICwgY29zdCA9IDA7CiAgICBpbnQgdmVyID0gLTE7CiAgICB2ZWN0b3I8aW50PiBnW21heE5dOwogICAgdmVjdG9yPGlpPiBFOwoKICAgIHZvaWQgaW5pdChpbnQgX24pewogICAgICAgIG4gPSBfbjsKICAgICAgICBjb3N0ID0gMDsKICAgICAgICB2ZXIgPSAtMTsKICAgICAgICBmb3IgKGludCBpID0gMSA7IGkgPD0gbiA7IGkrKyl7CiAgICAgICAgICAgIGdbaV0uY2xlYXIoKTsKICAgICAgICAgICAgZHBbaV0gPSAwOwogICAgICAgIH0KICAgICAgICBFLmNsZWFyKCk7CiAgICB9CgogICAgdm9pZCBhZGRfZWRnZShpbnQgdSAsIGludCB2KXsKICAgICAgICBnW3VdLnB1c2hfYmFjayh2KTsKICAgICAgICBnW3ZdLnB1c2hfYmFjayh1KTsKICAgICAgICBFLnB1c2hfYmFjayh7dSAsIHZ9KTsKICAgIH0KCiAgICB2b2lkIGRmc19pbml0KGludCB1ICwgaW50IHBhcil7CiAgICAgICAgc3pbdV0gPSAxOwogICAgICAgIGRwW3VdID0gMDsKICAgICAgICBmb3IgKGludCB2IDogZ1t1XSl7CiAgICAgICAgICAgIGlmICh2ICE9IHBhcil7CiAgICAgICAgICAgICAgICBkZnNfaW5pdCh2ICwgdSk7CiAgICAgICAgICAgICAgICBzelt1XSArPSBzelt2XTsKICAgICAgICAgICAgICAgIGRwW3VdICs9IGRwW3ZdICsgMWxsICogc3pbdl07CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgdm9pZCBjaGFuZ2UoaW50IHUgLCBpbnQgdil7CiAgICAgICAgc3pbdV0gLT0gc3pbdl07CiAgICAgICAgZHBbdV0gLT0gZHBbdl0gKyAxbGwgKiBzelt2XTsKICAgICAgICBzelt2XSArPSBzelt1XTsKICAgICAgICBkcFt2XSArPSBkcFt1XSArIDFsbCAqIHN6W3VdOwogICAgfQoKICAgIHZvaWQgZGZzX3Jlcm9vdChpbnQgdSAsIGludCBwYXIpewogICAgICAgIGNvc3QgKz0gZHBbdV07CiAgICAgICAgaWYgKHZlciA9PSAtMSB8fCBkcFt2ZXJdIDwgZHBbdV0pIHZlciA9IHU7CiAgICAgICAgZm9yIChpbnQgdiA6IGdbdV0pewogICAgICAgICAgICBpZiAodiAhPSBwYXIpewogICAgICAgICAgICAgICAgY2hhbmdlKHUgLCB2KTsKICAgICAgICAgICAgICAgIGRmc19yZXJvb3QodiAsIHUpOwogICAgICAgICAgICAgICAgY2hhbmdlKHYgLCB1KTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfSBmWzNdOwoKbm9kZSBhZGQobm9kZSBYICwgbm9kZSBZKXsKICAgIGludCBuID0gWC5uICsgWS5uOwogICAgbm9kZSBhbnM7IGFucy5pbml0KG4pOwogICAgZm9yIChhdXRvIGl0IDogWC5FKXsKICAgICAgICBhbnMuYWRkX2VkZ2UoaXQuZmkgLCBpdC5zZSk7CiAgICB9CiAgICBmb3IgKGF1dG8gaXQgOiBZLkUpewogICAgICAgIGFucy5hZGRfZWRnZShpdC5maSArIFgubiAsIGl0LnNlICsgWC5uKTsKICAgIH0KICAgIGFucy5hZGRfZWRnZShYLnZlciAsIFkudmVyICsgWC5uKTsKICAgIGFucy5kZnNfaW5pdCgxICwgMCk7CiAgICBhbnMuZGZzX3Jlcm9vdCgxICwgMCk7CiAgICByZXR1cm4gYW5zOwp9Cgp2b2lkIHNvbHZlKCl7CiAgICBmb3IgKGludCBpID0gMCA7IGkgPCAzIDsgaSsrKXsKICAgICAgICBpbnQgbjsgY2luID4+IG47CiAgICAgICAgZltpXS5pbml0KG4pOwogICAgICAgIGZvciAoaW50IGogPSAxIDsgaiA8IG4gOyBqKyspewogICAgICAgICAgICBpbnQgdSAsIHY7CiAgICAgICAgICAgIGNpbiA+PiB1ID4+IHY7CiAgICAgICAgICAgIGZbaV0uYWRkX2VkZ2UodSAsIHYpOwogICAgICAgIH0KICAgICAgICBmW2ldLmRmc19pbml0KDEgLCAwKTsKICAgICAgICBmW2ldLmRmc19yZXJvb3QoMSAsIDApOwogICAgfQogICAgbm9kZSBBID0gYWRkKGFkZChmWzBdICwgZlsxXSkgLCBmWzJdKTsKICAgIG5vZGUgQiA9IGFkZChhZGQoZlsxXSAsIGZbMl0pICwgZlswXSk7CiAgICBub2RlIEMgPSBhZGQoYWRkKGZbMl0gLCBmWzBdKSAsIGZbMV0pOwogICAgY291dCA8PCBtYXgoQS5jb3N0ICwgbWF4KEIuY29zdCAsIEMuY29zdCkpIC8gMiA8PCAiXG4iOwp9CgojZGVmaW5lIG5hbWUgIkEiCgppbnQgbWFpbigpewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsgY291dC50aWUoMCk7CiAgICBpZiAoZm9wZW4obmFtZSIuSU5QIiAsICJyIikpewogICAgICAgIGZyZW9wZW4obmFtZSIuSU5QIiAsICJyIiAsIHN0ZGluKTsKICAgICAgICBmcmVvcGVuKG5hbWUiLk9VVCIgLCAidyIgLCBzdGRvdXQpOwogICAgfQogICAgaW50IHQgPSAxOyBjaW4gPj4gdDsKICAgIHNvbHZlKCk7CiAgICByZXR1cm4gMDsKfQoKCg==