#include <bits/stdc++.h>
using namespace std;
void dfs(int v, vector <vector <int> > &g, vector <bool> &u, vector <int> &p, vector <int> &dp) {
u[v] = 1;
for (int to: g[v]) {
if (u[to]) {
continue;
}
p[to] = v;
dfs(to, g, u, p, dp);
dp[v] = max(dp[v], dp[to] + 1);
}
}
void dfs2(int v, vector <vector <int> > &g, vector <bool> &u, vector <int> &p, vector <int> &dp, vector <int> &dp_up) {
u[v] = 1;
if (p[v] != -1) {
int u = p[v];
dp_up[v] = dp_up[u] + 1;
for (int to: g[u]) {
if (p[u] != to && to != v) {
dp_up[v] = max(dp_up[v], dp[to] + 2);
}
}
}
for (int to: g[v]) {
if (!u[to]) {
dfs2(to, g, u, p, dp, dp_up);
}
}
}
const int M = 1e9 + 7;
void solve() {
int n;
cin >> n;
vector <vector <int> > g(n);
for (int i = 1; i < n; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 0; i < n; ++i) {
if (g[i].size() > 3) {
cout << -1 << endl;
return;
}
}
vector <int> dp(n), p(n, -1);
vector <bool> u(n);
dfs(0, g, u, p, dp);
vector <int> dp_up(n);
u.assign(n, 0);
dfs2(0, g, u, p, dp, dp_up);
int ans_h = 1000000000, ans_v = -1;
for (int i = 0; i < n; ++i) {
if (g[i].size() > 2) {
continue;
}
int h = max(dp[i], dp_up[i]);
if (ans_h > h) {
ans_h = h;
ans_v = i;
}
}
int ans = 1;
for (int i = 0; i <= ans_h; ++i) {
ans *= 2;
ans %= M;
}
ans+= M - 1;
ans %= M;
ans += M - n;
ans %= M;
cout << ans_v + 1 << ' ' << ans << endl;
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T-- > 0) {
solve();
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2b2lkIGRmcyhpbnQgdiwgdmVjdG9yIDx2ZWN0b3IgPGludD4gPiAmZywgd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