#ARC161C. [ARC161C] Dyed by Majority (Odd Tree)

[ARC161C] Dyed by Majority (Odd Tree)

Score : 500500 points

Problem Statement

You are given a tree with NN vertices. The vertices are labeled from 11 to NN, and the ii-th edge connects vertex AiA_i and vertex BiB_i. Moreover, for every vertex, the number of incident edges is odd.

You will color each vertex of the given tree either black ( B ) or white ( W ). Here, the string obtained by arranging the colors ( B or W ) of the vertices in the order of vertex labels is called a color sequence.

You are given a color sequence SS. Determine whether the color sequence SS can result from performing the following operation once when all vertices are colored, and if it can, find one possible color sequence before the operation.

Operation: For each vertex k=1,2,,Nk = 1, 2, \dots, N, let CkC_k be the color that occupies the majority (more than half) of the colors of the vertices connected to kk by an edge. For every vertex kk, change its color to CkC_k simultaneously.

There are TT test cases to solve.

Constraints

  • T1T \geq 1
  • N2N \geq 2
  • The sum of NN over the test cases in each input file is at most 2×1052 \times 10^5.
  • 1Ai<BiN  (1iN1)1 \leq A_i < B_i \leq N \ \ (1 \leq i \leq N - 1)
  • The given edges (Ai,Bi) (1iN1)(A_i, B_i) \ (1 \leq i \leq N - 1) form a tree.
  • Each vertex k (1kN)k \ (1 \leq k \leq N) appears an odd number of times in total as Ai,Bi (1iN1)A_i, B_i \ (1 \leq i \leq N - 1).
  • SS is a string of length NN consisting of B and W.

Input

The input is given from Standard Input in the following format:

TT

case1\mathrm{case}_1

case2\mathrm{case}_2

\vdots

caseT\mathrm{case}_T

Each test case casei (1iT)\mathrm{case}_i \ (1 \leq i \leq T) is in the following format:

NN

A1A_1 B1B_1

A2A_2 B2B_2

\vdots

AN1A_{N-1} BN1B_{N-1}

SS

Output

Print TT lines. For the ii-th line, if the color sequence SS can result from performing the operation in the ii-th test case, print one possible color sequence before the operation; otherwise, print -1. If there are multiple possible color sequences before the operation, any of them will be considered correct.

2
4
1 2
1 3
1 4
BWWW
4
1 2
1 3
1 4
BBWW
WBBW
-1

For the first test case, suppose the color sequence before the operation was WBBW. In this case,

  • for vertex 11, the colors of the connected vertices 2,3,42, 3, 4 are B, B, W, respectively, with C1=C_1 = {}B occupying the majority;
  • for vertex 22, the color of the connected vertex 11 is W, with C2=C_2 = {}W occupying the majority;
  • for vertex 33, the color of the connected vertex 11 is W, with C3=C_3 = {}W occupying the majority;
  • for vertex 44, the color of the connected vertex 11 is W, with C4=C_4 = {}W occupying the majority.

Therefore, the color sequence after the operation is BWWW, satisfying the condition. Similarly, if the color sequence before the operation was WBBB, WBWB, or WWBB, the color sequence after the operation would be BWWW, and any of these are considered correct.

For the second test case, the color sequence BBWW cannot result from the operation on the given tree.