Circle and Marble

No attempts yetTime limit1sMemory limit256 MB

Problem

Adi and Putri have been together for years, and Adi is ready to propose. Putri does not want to make it easy. She asks Adi to play a game with her, and she will marry him if he wins.

Putri drew one circle and put some marbles inside it. Then she drew a second circle, put some marbles inside it, and drew one arrow from the first circle to the new one. Then she drew a third circle, put some marbles inside it, and drew one arrow from one of the circles already on the paper to the new one. She repeated this until she had drawn NN circles. Some circles may hold no marbles. No two circles overlap and no circle sits inside another.

The two players move alternately. On your turn you pick one circle, take exactly one marble out of it, and move that marble to one of the circles that the arrows leaving the chosen circle point to. Whoever cannot move loses.

A circle with no outgoing arrow cannot be picked. Moving the marble to another circle is mandatory, so a circle the marble cannot leave is not a legal choice. A circle holding no marble cannot be picked either. The same rules bind both players.

If both players play optimally, the starting configuration alone decides the winner. Putri lets Adi choose who moves first. Given the starting configuration, decide whether Adi should move first or second in order to win. Putri plays optimally, so she wins whenever she gets the chance.

Input

The first line contains the number of test cases TT (1T1001 \le T \le 100).

Each test case begins with a line containing NN, the number of circles Putri drew (3N200003 \le N \le 20000). The circles are numbered 11 to NN in the order she drew them.

The second line contains NN integers M1,M2,,MNM_1, M_2, \ldots, M_N, where MiM_i is the number of marbles in circle ii (0Mi10000000 \le M_i \le 1000000).

The third line contains NN integers P1,P2,,PNP_1, P_2, \ldots, P_N, where PiP_i is the circle that the arrow pointing at circle ii starts from (1Pi<iN1 \le P_i < i \le N). Circle 11 was drawn first, so no arrow points at it and P1P_1 is always 00.

Output

For each test case, print one line in the form Case #X: Y. XX is the test case number starting from 11, followed by a single space. YY is first if Adi should take the first move to win, or second if he should move second.

Hint

These notations make the walkthrough shorter.

  • move(a, b) means taking one marble out of circle aa and moving it to circle bb.
  • m1,m2,m3,\langle m_1, m_2, m_3, \ldots \rangle lists the number of marbles in circle 11, circle 22, circle 33, and so on.

In the first case of the example input, the first player plays move(2, 3) and reaches 1,0,3\langle 1, 0, 3 \rangle. The second player has nothing but move(1, 2), reaching 0,1,3\langle 0, 1, 3 \rangle. The first player plays move(2, 3) again, reaches 0,0,4\langle 0, 0, 4 \rangle, and the second player cannot move.

In the second case, the first player loses whatever he plays. He has two moves.

  • move(1, 2) reaches 0,3,2\langle 0, 3, 2 \rangle. The two players then take turns moving marbles from circle 22 to circle 33, and the second player wins.
  • move(2, 3) reaches 1,1,3\langle 1, 1, 3 \rangle. The second player answers with move(2, 3) to reach 1,0,4\langle 1, 0, 4 \rangle. The first player has nothing but move(1, 2), and the second player ends the game with move(2, 3).

So Adi should be the second player in this configuration.

In the third case, the first player plays move(1, 2) and reaches 0,2,2,3\langle 0, 2, 2, 3 \rangle. The second player has nothing but move(3, 4), reaching 0,2,1,4\langle 0, 2, 1, 4 \rangle. The first player moves the last marble of circle 33 to circle 44, and the second player cannot move.