1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int m_arr[2005];

vector<int> canon(const vector<int>& seq) {
    vector<int> res;
    res.reserve(seq.size());
    int nxt = 1;
    for(int x : seq) {
        if(m_arr[x] == 0) m_arr[x] = nxt++;
        res.push_back(m_arr[x]);
    }
    for(int x : seq) m_arr[x] = 0;
    return res;
}

bool cmp(const vector<int>& A, const vector<int>& B) {
    int maxA = 0; for(int x : A) if(x > maxA) maxA = x;
    int maxB = 0; for(int x : B) if(x > maxB) maxB = x;
    
    vector<int> AB; AB.reserve(A.size() + B.size());
    for(int x : A) AB.push_back(x);
    for(int x : B) AB.push_back(x == 0 ? 0 : x + maxA);
    
    vector<int> BA; BA.reserve(B.size() + A.size());
    for(int x : B) BA.push_back(x);
    for(int x : A) BA.push_back(x == 0 ? 0 : x + maxB);
    
    vector<int> cAB = canon(AB);
    vector<int> cBA = canon(BA);
    
    return cAB < cBA;
}

struct Edge {
    int to;
    int id;
};

vector<Edge> adj[1005];
bool visited[2005];
vector<int> memo[2005];

vector<int> computeS(int u, int p_edge_id) {
    if(visited[p_edge_id]) return memo[p_edge_id];
    
    vector<vector<int>> branches;
    for(const auto& e : adj[u]) {
        if(e.id != p_edge_id) {
            vector<int> sv = computeS(e.to, e.id ^ 1);
            sv.push_back(0);
            branches.push_back(sv);
        }
    }
    
    if(branches.empty()) {
        visited[p_edge_id] = true;
        return memo[p_edge_id] = {};
    }
    
    sort(branches.begin(), branches.end(), cmp);
    
    vector<int> concat;
    int max_val = 0;
    for(const auto& b : branches) {
        int local_max = 0;
        for(int x : b) {
            if(x == 0) concat.push_back(0);
            else {
                concat.push_back(x + max_val);
                if(x > local_max) local_max = x;
            }
        }
        max_val += local_max;
    }
    
    vector<int> res = canon(concat);
    visited[p_edge_id] = true;
    return memo[p_edge_id] = res;
}

struct InputEdge {
    int u, v;
};

void solve() {
    int n;
    cin >> n;
    
    for(int i = 1; i <= n; ++i) {
        adj[i].clear();
    }
    
    vector<InputEdge> edges;
    int edge_cnt = 0;
    for(int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        edges.push_back({u, v});
        adj[u].push_back({v, edge_cnt});
        adj[v].push_back({u, edge_cnt + 1});
        edge_cnt += 2;
    }
    
    for(int i = 0; i < edge_cnt; ++i) {
        visited[i] = false;
        memo[i].clear();
    }
    
    vector<int> best_code;
    for(int i = 0; i < n - 1; ++i) {
        int u = edges[i].u;
        int v = edges[i].v;
        int id_uv = 2 * i;     
        int id_vu = 2 * i + 1; 
        
        vector<int> su = computeS(u, id_uv);
        vector<int> sv = computeS(v, id_vu);
        
        int maxU = 0; for(int x : su) if(x > maxU) maxU = x;
        int maxV = 0; for(int x : sv) if(x > maxV) maxV = x;
        
        vector<int> UV; UV.reserve(su.size() + sv.size());
        for(int x : su) UV.push_back(x);
        for(int x : sv) UV.push_back(x + maxU);
        
        vector<int> VU; VU.reserve(su.size() + sv.size());
        for(int x : sv) VU.push_back(x);
        for(int x : su) VU.push_back(x + maxV);
        
        vector<int> cUV = canon(UV);
        vector<int> cVU = canon(VU);
        
        if(best_code.empty() || cUV < best_code) best_code = cUV;
        if(cVU < best_code) best_code = cVU;
    }
    
    for(size_t i = 0; i < best_code.size(); ++i) {
        cout << best_code[i] << (i + 1 == best_code.size() ? "" : " ");
    }
    cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    if(cin >> t) {
        while(t--) {
            solve();
        }
    }
    return 0;
}