This page is still under construction.

Parts of this page are still being built. What you see may change.

Nim/3

Time limit1sMemory limit128 MB

Summary
Three-player Nim where each player has a preferred winner; find player 1's optimal move with smallest stack then smallest count.
Level

Medium7 of 10

Topics
Game theory, Dynamic programming, Brute force, Recursion
Solved
No attempts yet

Problem

You are staying in the country of Determinisia to take part in a challenging programming contest. The Determinisians are very fond of deterministic games — games whose result can be known in advance, assuming the players play optimally. They love watching everything unfold exactly as they had foreseen.

The great teacher Oneplusoneistwo invented the game of Nim thousands of years ago, and it is still very popular in Determinisia. The rules are simple: there are three (possibly empty) stacks of matches. Players take turns; on your turn you must choose a non-empty stack and remove any positive number of matches from it. The player who empties all three stacks — that is, who takes the last match — wins.

Recently the scientist Oneplustwoisthree proposed a three-player version of Nim, called Nim/3. To keep the game deterministic, each player designates one of the other two players as a favorite. If a player cannot win himself, he plays so that his favorite wins. Every player also knows everyone else's favorite. In short, each player's preference order is: (win yourself) > (your favorite wins) > (the remaining player wins).

You are player 1. To socialize with the local students, you want to play a few games of Nim/3, but only if you can play optimally. Fortunately, you are allowed to write a computer program to help you during play. Determine player 1's optimal move: which stack to draw from, and how many matches to take.

Input

The first line contains the number of test cases TT. Each test case is given in the following format.

  • One line with six integers s1 s2 s3 f1 f2 f3s_1\ s_2\ s_3\ f_1\ f_2\ f_3. Here s1,s2,s3s_1, s_2, s_3 are the number of matches on stacks 1, 2, and 3 respectively, with 0≤sn≤200 \le s_n \le 20 and s1+s2+s3≥1s_1 + s_2 + s_3 \ge 1. f1,f2,f3f_1, f_2, f_3 are the favorites of players 1, 2, and 3 respectively.

Note that fp∈{1,2,3}∖{p}f_p \in \{1, 2, 3\} \setminus \{p\}. It is currently player 1's turn, followed by player 2, then player 3, and so on cyclically.

Output

For each test case, print player 1's move when he plays optimally, as two integers kk and nn on a single line separated by a space. Here k∈{1,2,3}k \in \{1, 2, 3\} is the stack and 0<n≤sk0 < n \le s_k is the number of matches to take from that stack. There may be several optimal moves; in that case output the one with the smallest kk, and among those the smallest nn.

Examples1

  1. Example 1

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