Under construction

Still being built. Things may change

Cracking Tree Codes

Interview

Time limit1sMemory limit128 MB

Summary
Given a tree and its leaf-stripping code with some entries erased, the program replays the encoding to recover the missing numbers.
Level

Easy3 of 10

Topics
Simulation, Tree, Heap
Solved
No attempts yet

Problem

Let V={1,2,…,n}V = \{1, 2, \dots, n\} and E⊂{{u,v}∣1≤u,v≤n}E \subset \{\{u, v\} \mid 1 \le u, v \le n\}. A tree T=(V,E)T = (V, E) is a graph that is connected and has exactly n−1n - 1 edges. The figure below shows a tree T1T_1.

In T1T_1 we have V={1,2,3,4,5,6,7}V = \{1, 2, 3, 4, 5, 6, 7\} and E={{4,6},{2,6},{6,5},{3,5},{5,1},{1,7}}E = \{\{4, 6\}, \{2, 6\}, \{6, 5\}, \{3, 5\}, \{5, 1\}, \{1, 7\}\}.

Professor Minton found a way to encrypt a tree. The code of a tree is a sequence of n−2n - 2 numbers from VV, built by repeating these three steps n−2n - 2 times.

  1. In the part of the tree that is left, take the leaf (a vertex of degree 11) with the smallest number.
  2. Append the number of the only vertex adjacent to that leaf to the sequence.
  3. Delete that leaf from the tree.

After n−2n - 2 rounds two vertices remain. The code of T1T_1 is ⟨6,5,6,5,1⟩\langle 6, 5, 6, 5, 1 \rangle.

A code sometimes arrives with a few of its numbers erased. Every erased position is written as the letter x. Given a tree and a code with erased numbers, write a program that recovers the erased numbers.

Input

The first line contains the number of test cases tt. (1≤t≤101 \le t \le 10)

Each test case takes three lines. The first line contains the number of vertices nn. (2≤n≤100002 \le n \le 10000) The second line contains 2n−22n - 2 numbers separated by blanks that describe the tree. Reading two numbers at a time, the first pair is the first edge, the second pair is the second edge, and so on. The third line contains the code with erased numbers as n−2n - 2 tokens. Each token is either a vertex number or the letter x for an erased position. When n=2n = 2 the third line is empty.

The numbers that were not erased agree with the code of the given tree. The sum of nn over all test cases is at most 3000030000.

Output

For each test case print one line holding the erased numbers in the order they appear in the input, separated by single blanks. If nothing was erased, print an empty line.

Examples2

  1. Example 1

    Input
    1
    7
    4 6 2 6 6 5 3 5 5 1 1 7
    6 x x 5 1
    
    Expected output
    5 6
    
  2. Example 2

    Input
    1
    5
    3 1 3 2 3 4 3 5
    x x x
    
    Expected output
    3 3 3