After the last regional contest, Hemose and his teammates finally qualified to the ICPC World Finals, so for this great achievement and his love of trees, he gave you this problem as the name of his team "Hemose 3al shagra" (Hemose on the tree).You are given a tree of $$$n$$$ vertices where $$$n$$$ is a power of $$$2$$$. You have to give each node and edge an integer value in the range $$$[1,2n -1]$$$ (inclusive), where all the values are distinct.After giving each node and edge a value, you should select some root for the tree such that the maximum cost of any simple path starting from the root and ending at any node or edge is minimized.The cost of the path between two nodes $$$u$$$ and $$$v$$$ or any node $$$u$$$ and edge $$$e$$$ is defined as the bitwise XOR of all the node's and edge's values between them, including the endpoints (note that in a tree there is only one simple path between two nodes or between a node and an edge).

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$p$$$ ($$$1 \le p \le 17$$$), where $$$n$$$ (the number of vertices in the tree) is equal to $$$2^p$$$. Each of the next $$$n−1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$) meaning that there is an edge between the vertices $$$u$$$ and $$$v$$$ in the tree. It is guaranteed that the given graph is a tree. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3\cdot 10^5$$$.

For each test case on the first line print the chosen root. On the second line, print $$$n$$$ integers separated by spaces, where the $$$i$$$-th integer represents the chosen value for the $$$i$$$-th node. On the third line, print $$$n-1$$$ integers separated by spaces, where the $$$i$$$-th integer represents the chosen value for the $$$i$$$-th edge. The edges are numerated in the order of their appearance in the input data. If there are multiple solutions, you may output any.

NoteThe tree in the first test case with the weights of all nodes and edges is shown in the picture.  The costs of all paths are:   $$$3$$$;  $$$3\oplus 7=4$$$;  $$$3\oplus 7\oplus 6=2$$$;  $$$3\oplus 2=1$$$;  $$$3\oplus 2\oplus 1=0$$$;  $$$3\oplus 2\oplus 1\oplus 4=4$$$;  $$$3\oplus 2\oplus 1\oplus 4\oplus 5=1$$$. The maximum cost of all these paths is $$$4$$$. We can show that it is impossible to assign the values and choose the root differently to achieve a smaller maximum cost of all paths.The tree in the second test case:

package main

import (
	"bufio"
	"fmt"
	"os"
)

const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

type Edge struct {
	v int
	e int
}

type Node struct {
	edges []Edge
	w     int
}

func (n *Node) Add(v, e int) {
	n.edges = append(n.edges, Edge{v, e})
}

var dfs func(int, int, int)

func main() {
	myio := NewIO(os.Stdin, os.Stdout)
	defer myio.Flush()
	t := myio.ReadInt()
	sol := func() {
		n := myio.ReadInt()
		N := 1 << n
		e1 := make([]int, 1<<n)
		e2 := make([]int, 1<<n)
		w := make([]int, 1<<n)

		nodes := make([]Node, N+1)

		for i := 0; i < N-1; i++ {
			e1[i] = myio.ReadInt()
			e2[i] = myio.ReadInt()
			nodes[e1[i]].Add(e2[i], i)
			nodes[e2[i]].Add(e1[i], i)
		}

		nodes[1].w = N
		idx := 1
		dfs = func(v, p, xor int) {
			for _, eg := range nodes[v].edges {
				if eg.v == p {
					continue
				}
				if xor&N > 0 {
					w[eg.e] = idx + N
					nodes[eg.v].w = idx
				} else {
					w[eg.e] = idx
					nodes[eg.v].w = idx + N
				}
				idx++
				dfs(eg.v, v, xor^N)
			}
		}
		dfs(1, 0, N)
		myio.Println("1")
		for i := 1; i <= N; i++ {
			myio.Print(nodes[i].w, " ")
		}
		myio.Println()
		for i := 0; i < N-1; i++ {
			myio.Print(w[i], " ")
		}
		myio.Println()
	}
	for ; t > 0; t-- {
		sol()
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Edge struct {
	v int
	e int
}

type Node struct {
	edges []Edge
	w     int
}

func (n *Node) Add(v, e int) {
	n.edges = append(n.edges, Edge{v, e})
}

var dfs func(int, int, int)

func main() {
	const eof = 0
	_r := os.Stdin
	// in.Split(bufio.ScanWords)
	// in.Buffer(nil, 1e9)
	_i, _n, buf := 0, 0, make([]byte, 1<<12) // 4KB
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	rc := func() byte {
		if _i == _n {
			_n, _ = _r.Read(buf)
			if _n == 0 { // EOF
				return eof
			}
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}

	nextInt := func() (x int) {
		b := rc()
		for ; '0' > b || b > '9'; b = rc() {
			if b == eof {
				return
			}
		}
		for ; '0' <= b && b <= '9'; b = rc() {
			x = x*10 + int(b&15)
		}
		return x
	}

	t := nextInt()
	sol := func() {
		n := nextInt()
		N := 1 << n
		e1 := make([]int, 1<<n)
		e2 := make([]int, 1<<n)
		w := make([]int, 1<<n)

		nodes := make([]Node, N+1)

		for i := 0; i < N-1; i++ {
			e1[i] = nextInt()
			e2[i] = nextInt()
			nodes[e1[i]].Add(e2[i], i)
			nodes[e2[i]].Add(e1[i], i)
		}

		nodes[1].w = N
		idx := 1
		dfs = func(v, p, xor int) {
			for _, eg := range nodes[v].edges {
				if eg.v == p {
					continue
				}
				if xor&N > 0 {
					w[eg.e] = idx + N
					nodes[eg.v].w = idx
				} else {
					w[eg.e] = idx
					nodes[eg.v].w = idx + N
				}
				idx++
				dfs(eg.v, v, xor^N)
			}
		}
		dfs(1, 0, N)
		fmt.Fprintln(out, "1")
		for i := 1; i <= N; i++ {
			fmt.Fprint(out, nodes[i].w, " ")
		}
		fmt.Fprintln(out)
		for i := 0; i < N-1; i++ {
			fmt.Fprint(out, w[i], " ")
		}
		fmt.Fprintln(out)
	}
	for ; t > 0; t-- {
		sol()
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

type Edge struct {
	v int
	e int
}

type Node struct {
	edges []Edge
	w     int
}

func (n *Node) Add(v, e int) {
	n.edges = append(n.edges, Edge{v, e})
}

var dfs func(int, int, int)

func main() {
	myio := NewIO(os.Stdin, os.Stdout)
	defer myio.Flush()
	t := myio.ReadInt()
	sol := func() {
		n := myio.ReadInt()
		N := 1 << n
		e1 := make([]int, 1<<n)
		e2 := make([]int, 1<<n)
		w := make([]int, 1<<n)

		nodes := make([]Node, N+1)

		for i := 0; i < N-1; i++ {
			e1[i] = myio.ReadInt()
			e2[i] = myio.ReadInt()
			nodes[e1[i]].Add(e2[i], i)
			nodes[e2[i]].Add(e1[i], i)
		}

		nodes[1].w = N
		idx := 1
		dfs = func(v, p, xor int) {
			for _, eg := range nodes[v].edges {
				if eg.v == p {
					continue
				}
				if xor&N > 0 {
					w[eg.e] = idx + N
					nodes[eg.v].w = idx
				} else {
					w[eg.e] = idx
					nodes[eg.v].w = idx + N
				}
				idx++
				dfs(eg.v, v, xor^N)
			}
		}
		dfs(1, 0, N)
		myio.Println("1")
		for i := 1; i <= N; i++ {
			myio.Print(nodes[i].w, " ")
		}
		myio.Println()
		for i := 0; i < N-1; i++ {
			myio.Print(w[i], " ")
		}
		myio.Println()
	}
	for ; t > 0; t-- {
		sol()
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Edge struct {
	v int
	e int
}

type Node struct {
	edges []Edge
	w     int
}

func (n *Node) Add(v, e int) {
	n.edges = append(n.edges, Edge{v, e})
}

var dfs func(int, int, int)

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	in.Buffer(nil, 1e9)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	nextInt := func() int {
		in.Scan()
		vv, _ := strconv.Atoi(in.Text())
		return vv
	}

	t := nextInt()
	sol := func() {
		n := nextInt()
		N := 1 << n
		e1 := make([]int, 1<<n)
		e2 := make([]int, 1<<n)
		w := make([]int, 1<<n)

		nodes := make([]Node, N+1)

		for i := 0; i < N-1; i++ {
			e1[i] = nextInt()
			e2[i] = nextInt()
			nodes[e1[i]].Add(e2[i], i)
			nodes[e2[i]].Add(e1[i], i)
		}

		nodes[1].w = N
		idx := 1
		dfs = func(v, p, xor int) {
			for _, eg := range nodes[v].edges {
				if eg.v == p {
					continue
				}
				if xor&N > 0 {
					w[eg.e] = idx + N
					nodes[eg.v].w = idx
				} else {
					w[eg.e] = idx
					nodes[eg.v].w = idx + N
				}
				idx++
				dfs(eg.v, v, xor^N)
			}
		}
		dfs(1, 0, N)
		fmt.Fprintln(out, "1")
		for i := 1; i <= N; i++ {
			fmt.Fprint(out, nodes[i].w, " ")
		}
		fmt.Fprintln(out)
		for i := 0; i < N-1; i++ {
			fmt.Fprint(out, w[i], " ")
		}
		fmt.Fprintln(out)
	}
	for ; t > 0; t-- {
		sol()
	}
}

package main

import (
	"fmt"
	"os"
)

type Edge struct {
	v int
	e int
}

type Node struct {
	edges []Edge
	w     int
}

func (n *Node) Add(v, e int) {
	n.edges = append(n.edges, Edge{v, e})
}

var dfs func(int, int, int)

func main() {
	const eof = 0
	_r := os.Stdin
	// in.Split(bufio.ScanWords)
	// in.Buffer(nil, 1e9)
	_i, _n, buf := 0, 0, make([]byte, 1<<12) // 4KB

	rc := func() byte {
		if _i == _n {
			_n, _ = _r.Read(buf)
			if _n == 0 { // EOF
				return eof
			}
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}

	nextInt := func() (x int) {
		b := rc()
		for ; '0' > b || b > '9'; b = rc() {
			if b == eof {
				return
			}
		}
		for ; '0' <= b && b <= '9'; b = rc() {
			x = x*10 + int(b&15)
		}
		return x
	}

	t := nextInt()
	sol := func() {
		n := nextInt()
		N := 1 << n
		e1 := make([]int, 1<<n)
		e2 := make([]int, 1<<n)
		w := make([]int, 1<<n)

		nodes := make([]Node, N+1)

		for i := 0; i < N-1; i++ {
			e1[i] = nextInt()
			e2[i] = nextInt()
			nodes[e1[i]].Add(e2[i], i)
			nodes[e2[i]].Add(e1[i], i)
		}

		nodes[1].w = N
		idx := 1
		dfs = func(v, p, xor int) {
			for _, eg := range nodes[v].edges {
				if eg.v == p {
					continue
				}
				if xor&N > 0 {
					w[eg.e] = idx + N
					nodes[eg.v].w = idx
				} else {
					w[eg.e] = idx
					nodes[eg.v].w = idx + N
				}
				idx++
				dfs(eg.v, v, xor^N)
			}
		}
		dfs(1, 0, N)
		fmt.Println("1")
		for i := 1; i <= N; i++ {
			fmt.Print(nodes[i].w, " ")
		}
		fmt.Println()
		for i := 0; i < N-1; i++ {
			fmt.Print(w[i], " ")
		}
		fmt.Println()
	}
	for ; t > 0; t-- {
		sol()
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type Edge struct {
	v int
	e int
}

type Node struct {
	edges []Edge
	w     int
}

func (n *Node) Add(v, e int) {
	n.edges = append(n.edges, Edge{v, e})
}

var dfs func(int, int, int)

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	in.Buffer(nil, 1e9)

	nextInt := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b&15)
		}
		return x
	}

	t := nextInt()
	sol := func() {
		n := nextInt()
		N := 1 << n
		e1 := make([]int, 1<<n)
		e2 := make([]int, 1<<n)
		w := make([]int, 1<<n)

		nodes := make([]Node, N+1)

		for i := 0; i < N-1; i++ {
			e1[i] = nextInt()
			e2[i] = nextInt()
			nodes[e1[i]].Add(e2[i], i)
			nodes[e2[i]].Add(e1[i], i)
		}

		nodes[1].w = N
		idx := 1
		dfs = func(v, p, xor int) {
			for _, eg := range nodes[v].edges {
				if eg.v == p {
					continue
				}
				if xor&N > 0 {
					w[eg.e] = idx + N
					nodes[eg.v].w = idx
				} else {
					w[eg.e] = idx
					nodes[eg.v].w = idx + N
				}
				idx++
				dfs(eg.v, v, xor^N)
			}
		}
		dfs(1, 0, N)
		fmt.Println("1")
		for i := 1; i <= N; i++ {
			fmt.Print(nodes[i].w, " ")
		}
		fmt.Println()
		for i := 0; i < N-1; i++ {
			fmt.Print(w[i], " ")
		}
		fmt.Println()
	}
	for ; t > 0; t-- {
		sol()
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Edge struct {
	v int
	e int
}

type Node struct {
	edges []Edge
	w     int
}

func (n *Node) Add(v, e int) {
	n.edges = append(n.edges, Edge{v, e})
}

var dfs func(int, int, int)

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	in.Buffer(nil, 1e9)

	nextInt := func() int {
		in.Scan()
		vv, _ := strconv.Atoi(in.Text())
		return vv
	}

	t := nextInt()
	sol := func() {
		n := nextInt()
		N := 1 << n
		e1 := make([]int, 1<<n)
		e2 := make([]int, 1<<n)
		w := make([]int, 1<<n)

		nodes := make([]Node, N+1)

		for i := 0; i < N-1; i++ {
			e1[i] = nextInt()
			e2[i] = nextInt()
			nodes[e1[i]].Add(e2[i], i)
			nodes[e2[i]].Add(e1[i], i)
		}

		nodes[1].w = N
		idx := 1
		dfs = func(v, p, xor int) {
			for _, eg := range nodes[v].edges {
				if eg.v == p {
					continue
				}
				if xor&N > 0 {
					w[eg.e] = idx + N
					nodes[eg.v].w = idx
				} else {
					w[eg.e] = idx
					nodes[eg.v].w = idx + N
				}
				idx++
				dfs(eg.v, v, xor^N)
			}
		}
		dfs(1, 0, N)
		fmt.Println("1")
		for i := 1; i <= N; i++ {
			fmt.Print(nodes[i].w, " ")
		}
		fmt.Println()
		for i := 0; i < N-1; i++ {
			fmt.Print(w[i], " ")
		}
		fmt.Println()
	}
	for ; t > 0; t-- {
		sol()
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func ia(m int) []int { return make([]int,m) }
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
type edge struct {n2,eidx int}
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		P := gi(); N := 1<<P; U,V := fill2(N-1)
		for i:=0;i<N-1;i++ { U[i]--; V[i]-- }
		gr := make([][]edge,N)
		for i:=0;i<N-1;i++ { u,v := U[i],V[i]; gr[u] = append(gr[u],edge{v,i}); gr[v] = append(gr[v],edge{u,i}) }
		nans := make([]int,N)
		eans := make([]int,N-1)
		nans[0] = 1 << P
		adder := 1; big := 1 << P
		var dfs func(n,p,v int)
		dfs = func(n,p,v int) {
			for _,e := range gr[n] {
				if e.n2 != p {
					if v ^ adder < big {
						eans[e.eidx] = adder; nans[e.n2] = adder | big
					} else {
						eans[e.eidx] = adder | big; nans[e.n2] = adder
					}
					adder++
					dfs(e.n2,n,v ^ big)
				}
			}
		}
		dfs(0,-1,big)
		fmt.Fprintln(wrtr,1)
		fmt.Fprintln(wrtr,vecintstring(nans))
		fmt.Fprintln(wrtr,vecintstring(eans))
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Edge struct {
	v int
	e int
}

type Node struct {
	edges []Edge
	w     int
}

func (n *Node) Add(v, e int) {
	n.edges = append(n.edges, Edge{v, e})
}

var dfs func(int, int, int)

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	in.Buffer(nil, 1e9)

	nextInt := func() int {
		in.Scan()
		vv, _ := strconv.Atoi(in.Text())
		return vv
	}

	t := nextInt()
	sol := func() {
		n := nextInt()
		N := 1 << n
		e1 := make([]int, 1<<n)
		e2 := make([]int, 1<<n)
		w := make([]int, 1<<n)

		nodes := make([]Node, N+1)

		for i := 0; i < N-1; i++ {
			e1[i] = nextInt()
			e2[i] = nextInt()
			nodes[e1[i]].Add(e2[i], i)
			nodes[e2[i]].Add(e1[i], i)
		}

		nodes[1].w = N
		idx := 1
		dfs = func(v, p, xor int) {
			for _, eg := range nodes[v].edges {
				if eg.v == p {
					continue
				}
				if p&N > 0 {
					w[eg.e] = idx + N
					nodes[eg.v].w = idx
				} else {
					w[eg.e] = idx
					nodes[eg.v].w = idx + N
				}
				idx++
				dfs(eg.v, v, xor^N)
			}
		}
		dfs(1, N, N)
		fmt.Println("1")
		for i := 1; i <= N; i++ {
			fmt.Print(nodes[i].w, " ")
		}
		fmt.Println()
		for i := 0; i < N-1; i++ {
			fmt.Print(w[i], " ")
		}
		fmt.Println()
	}
	for ; t > 0; t-- {
		sol()
	}
}