Symmetric Trees (Large)

Decide whether a color-painted tree can be drawn in the plane with a vertical line of symmetry.

Medium7TreeRecursionHash mapSortingNo attempts yetTime limit5sMemory limit512 MB

Problem

You are given a tree with NN vertices, and every vertex is painted with one color. Decide whether the tree can be drawn in the plane so that it has a line of symmetry.

Formally, a tree is line symmetric if every vertex can be assigned a location in the plane so that all four of the following hold.

  • All locations are distinct.
  • If vertex viv_i has color CC and coordinates (xi,yi)(x_i, y_i), then there is also a vertex viv_i' of color CC at (xi,yi)(-x_i, y_i). If xix_i is 0, then viv_i and viv_i' are the same vertex.
  • If edge (vi,vj)(v_i, v_j) exists, then edge (vi,vj)(v_i', v_j') also exists.
  • When each edge is drawn as the straight segment between its two end vertices, two different edges share no point. Two adjacent edges meeting at their common endpoint are allowed.

Input

The first line holds the number of test cases TT. The test cases follow.

The first line of each test case holds NN, the number of vertices in the tree.

The next NN lines each hold one uppercase letter. The ii-th of them is the color of vertex ii.

The next N1N-1 lines each hold two integers ii and jj (1i<jN1 \le i < j \le N), meaning that the tree has an edge between vertex ii and vertex jj. The given edges always form a connected tree.

Limits:

  • 1T1001 \le T \le 100
  • 2N100002 \le N \le 10000

Output

For each test case, print one line holding Case #x: y, where x is the test case number starting from 1, and y is SYMMETRIC when the tree is line symmetric by the definition above and NOT SYMMETRIC when it is not.

Hint

The first test case of the sample input can be drawn like this.

No arrangement of the second test case has a line of symmetry.

Here is one way to draw the third test case with a line of symmetry.