#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <sstream>
using namespace __gnu_pbds;
using namespace std;
void Henry() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
void filIO() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int cnt = 0;
void DFS(int i, int p, vector<vector<int>> &adj, vector<int>&vis, vector<vector<int>> &ans) {
vis[i] = 1;
if (p) {
if (cnt) {
ans[i][p] = 2;
ans[p][i] = 2;
}
else {
ans[i][p] = 3;
ans[p][i] = 3;
}
cnt = ~cnt;
}
for (auto j : adj[i]) {
if (!vis[j]) {
DFS(j, i, adj, vis, ans);
}
}
}
void solve() {
int n;cin >> n;
vector<vector<int>> adj;
vector<int>vis;
vector<vector<int>> ans;
adj.resize(n + 1, {});
vis.resize(n + 1, 0);
ans.resize(n + 1, vector<int>(n + 1, 0));
vector<pair<int, int>> edges;
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
edges.push_back({u, v});
}
int st = 0;
for (int i = 1; i <= n; i++) {
if (adj[i].size() > 2) {
cout << "-1\n";
return;
}
if (adj[i].size() == 1) {
st = i;
}
}
cnt = 0;
DFS(st, 0, adj, vis, ans);
for (auto edge : edges) {
cout << ans[edge.first][edge.second] << ' ';
}
cout << '\n';
}
signed main() {
Henry();
filIO();
int q = 1;
cin >> q;
while (q--) {
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KI2luY2x1ZGUgPHNzdHJlYW0+Cgp1c2luZyBuYW1lc3BhY2UgX19nbnVfcGJkczsKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgSGVucnkoKSB7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50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