Under construction

Still being built. Things may change

Treasure Hunt Game

Time limit4sMemory limit1024 MB

Summary
For each start vertex, decide whether Alice can force the token to hit treasure vertices infinitely often in a two-player token game on an undirected graph.
Level

Medium7 of 10

Topics
Graph, Game theory, BFS
Solved
No attempts yet

Problem

The graph has NN vertices numbered 1 to NN and MM undirected edges. Every vertex is connected to at least one edge. Some vertices belong to Alice, and the rest belong to Bob. Some vertices contain a treasure.

Alice and Bob place a token on a vertex of the graph and play a game. At the start of each turn, if the token is on one of Alice's vertices, Alice must choose one edge adjacent to the current vertex and move the token to its other endpoint. Bob does the same on his vertices. The token cannot stay where it is.

When the token visits a vertex with a treasure, the player gets one treasure. If that vertex was visited before, the player gets another treasure each time the token returns to it.

Alice wants the token to visit treasure vertices infinitely often, and Bob wants to prevent this. Precisely, play the game for 1010010^{100} turns. If the token is on a treasure vertex in at least 109010^{90} of those turns, Alice wins. Otherwise, Bob wins.

Assuming both players always play optimally, determine who wins for each starting vertex.

Input

The first line contains the number of test cases TT. (1≤T≤501 \le T \le 50)

The first line of each test case contains integers NN and MM. (1≤N≤100 0001 \le N \le 100\,000, 1≤M≤200 0001 \le M \le 200\,000)

The next line contains a string of length NN. Each character is A or B. If the ii-th character is A, vertex ii belongs to Alice. If it is B, vertex ii belongs to Bob.

The next line contains a string of length NN. Each character is T or .. If the ii-th character is T, vertex ii has a treasure. If it is ., vertex ii has no treasure.

The next MM lines each contain two integers uiu_i and viv_i, separated by a space, the endpoints of the ii-th edge. (1≤ui,vi≤N1 \le u_i, v_i \le N, ui≠viu_i \neq v_i) No two edges connect the same pair of vertices.

In all test cases, the sum of NN and the sum of MM each do not exceed 2,000,000.

Output

For each test case, print a line "Case #CC", where CC is the test case number. On the next line, print a string of length NN made of A or B. The ii-th character is A if Alice wins when the game starts with the token on vertex ii, and B if Bob wins.

Examples1

  1. Example 1

    Input
    2
    5 5
    BBAAB
    ...TT
    1 2
    1 5
    2 3
    5 3
    3 4
    5 3
    BBAAB
    ...TT
    1 2
    1 4
    3 5
    
    Expected output
    Case #1
    BBAAB
    Case #2
    BBABA