Road Repair in Treeland

There are $n$ cities and $n-1$ two-way roads in Treeland. Each road connects a pair of different cities. From any city you can drive to any other, moving only along the roads. Cities are numbered from $1$ to $n$. Yes, of course, you recognized an undirected tree in this description.

The government plans to repair all the roads. Each road will be repaired by some private company. In total, the country has $10^6$ private companies that are numbered from $1$ to $10^6$. It is possible that some companies will not repair roads at all, and some will repair many roads.

To simplify the control over the work of private companies, the following restriction was introduced: for each city, we calculate the number of different companies that repair roads that have this city at one end. This number for each city should not exceed $2$. In other words, for each city, there should be no more than two different companies that repair roads related to this city.

The National Anti-Corruption Committee of Treeland raises concerns that all (or almost all) of the work will go to one private company. For this reason, the committee requires that roads be distributed among companies in such a way as to minimize the value of $r$. For each company, we calculate the number of roads assigned to it, the maximum among all companies is called the number $r$.

Help the government find such a way to distribute all roads among companies in the required way.

Input

The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of input cases in the input. Next, the input cases themselves are given. The first line of each test case set contains an integer $n$ ($2 \le n \le 3000$) — the number of cities in Treeland. Next, in the $n-1$ lines the roads are written: a line contains two integers $x_i$, $y_i$ ($1 \le x_i, y_i \le n$), indicating that the $i$-th road connects the cities $x_i$ and $y_i$.

It is guaranteed that the sum of all values $n$ ​​for all test cases in the input does not exceed $3000$.

Output

Print the answers for all $t$ test cases in the input. Each test case must begin with a line that contains $r$ — the minimum possible number of roads assigned to the most used company. Next, in the next line print $n-1$ the number $c_1, c_2, \dots, c_{n-1}$ ($1 \le c_i \le 10^6$), where $c_i$ indicates the company to repair the $i$-th road. If there are several optimal assignments, print any of them.

Example

input

3
3
1 2
2 3
6
1 2
1 3
1 4
1 5
1 6
7
3 1
1 4
4 6
5 1
2 4
1 7

output

1
10 20
3
1 1 1 2 2 
2
11 11 12 13 12 13

Solution:

private fun readLn() = readLine()!! // string line
private fun readInt() = readLn().toInt() // single int
private fun readLong() = readLn().toLong() // single long
private fun readDouble() = readLn().toDouble() // single double
private fun readStrings() = readLn().split(" ") // list of strings
private fun readInts() = readStrings().map { it.toInt() } // list of ints
private fun readLongs() = readStrings().map { it.toLong() } // list of longs
private fun readDoubles() = readStrings().map { it.toDouble() } // list of doubles

fun main(args: Array<String>) {
var tt = readInt()
while (tt-- > 0) {
var n = readInt()
var g = Array(n, { ArrayList<Pair<Int, Int>>() })
for (i in 0..n - 2) {
var (x, y) = readInts()
x--
y--
g[x].add(Pair(y, i))
g[y].add(Pair(x, i))
}
var low = 1
var high = n - 1
while (low < high) {
            var mid = (low + high) / 2
            var dp = IntArray(n)
            var ok = true
            var pe = IntArray(n, { -1 })
            var pv = IntArray(n, { -1 })
            fun Dfs(v: Int, pr: Int) {
                if (!ok) return
                var children = ArrayList<Int>()
var sum = 0
for (uu in g[v]) {
var u = uu.first
if (u == pr) {
continue
}
pv[u] = v
pe[u] = uu.second
Dfs(u, v)
if (!ok) return
children.add(dp[u])
sum += dp[u]
}
var knap = BooleanArray(sum + 1, {false})
knap[0] = true
for (x in children) {
for (i in sum-x downTo 0) {
if (knap[i]) {
knap[i + x] = true
}
}
}
dp[v] = -1
for (i in sum downTo 0) {
if (i <= mid && knap[i]) {
                        dp[v] = sum - i
                        break
                    }
                }
                if (v != 0) {
                    dp[v] += 1
                }
                if (dp[v] > mid) {
ok = false
}
}
Dfs(0, -1)
if (ok) {
high = mid
} else {
low = mid + 1
}
}
println(low)

var mid = low

var dp = IntArray(n)
var pe = IntArray(n, { -1 })
var pv = IntArray(n, { -1 })
var knap = BooleanArray(n + 2, {false})
fun Dfs(v: Int, pr: Int) {
var children = ArrayList<Int>()
var sum = 0
for (uu in g[v]) {
var u = uu.first
if (u == pr) {
continue
}
pv[u] = v
pe[u] = uu.second
Dfs(u, v)
children.add(dp[u])
sum += dp[u]
}
for (i in 0..sum) knap[i] = false
knap[0] = true
for (x in children) {
for (i in sum-x downTo 0) {
if (knap[i]) {
knap[i + x] = true
}
}
}
dp[v] = -1
for (i in sum downTo 0) {
if (i <= mid && knap[i]) {
                    dp[v] = sum - i
                    break
                }
            }
            if (v != 0) {
                dp[v] += 1
            }
            assert(dp[v] <= mid)
        }
        Dfs(0, -1)

        var colors = IntArray(n - 1)
        var kdp = Array(n + 2, {BooleanArray(n + 2, {false})})
        var take = BooleanArray(n, {false})

        fun Restore(v: Int, pr: Int) {
            var children = ArrayList<Int>()
var cv = ArrayList<Int>()
var sum = 0
for (uu in g[v]) {
var u = uu.first
if (u == pr) {
continue
}
pv[u] = v
pe[u] = uu.second
children.add(dp[u])
cv.add(u)
sum += dp[u]
}
var sz = children.size
for (i in 0..sz) {
for (j in 0..sum) {
kdp[i][j] = false
}
}
kdp[0][0] = true
for (xt in 0..sz - 1) {
var x = children[xt]
for (i in 0..sum) {
if (kdp[xt][i]) {
if (i + x <= sum) {
                            kdp[xt + 1][i + x] = true
                        }
                        kdp[xt + 1][i] = true
                    }
                }
            }
            var res = -1
            for (i in sum downTo 0) {
                if (i <= mid && kdp[sz][i]) {
                    res = i
                    break
                }
            }
            for (i in (sz-1) downTo 0) {
                if (!kdp[i][res]) {
                    res -= children[i]
                    take[cv[i]] = true
                }
            }
            for (i in 0..sz-1) {
                if (take[cv[i]]) {
                    colors[pe[cv[i]]] = v + 1
                } else {
                    colors[pe[cv[i]]] = if (v == 0) 787788 else colors[pe[v]]
                }
            }
            for (i in 0..sz-1) {
                Restore(cv[i], v)
            }
        }
        Restore(0, -1)

        println(colors.joinToString(" "))
    }
}