Under construction

Still being built. Things may change

Game

Time limit8sMemory limit512 MB

Summary
Players alternately remove an end element from a sequence, and the mover who reaches a terminal sequence wins. Decide whether the first player, the second, or neither wins.
Level

Hard9 of 10

Topics
Game theory, Dynamic programming, String matching
Solved
No attempts yet

Problem

Let us consider the following two-player game. Initially, there is some sequence of numbers. Then players in turns remove either the first or the last element of the sequence. If after some move, the sequence becomes one of several terminal sequences, then the game ends and the player who made the last move wins. If no player can make a move (because the sequence has length 00), then no player wins. Determine which player has a winning strategy in this game.

Input

The first line of input contains the number of test cases zz. The descriptions of the test cases follow.

The first line of each test case contains an integer nn (1≤n≤1061 \le n \le 10^6) followed by nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤1090 \le a_i \le 10^9), the initial sequence itself. The second line contains one integer kk, the number of terminal sequences. The next kk lines contain descriptions of terminal sequences. Each of these lines contains an integer mm (m≥1m \ge 1), the length of the sequence, followed by mm elements of the sequence: w1,w2,…,wmw_1, w_2, \ldots, w_m (0≤wi≤1090 \le w_i \le 10^9). The total length of terminal sequences does not exceed 3⋅1063 \cdot 10^6.

Consecutive test cases will be separated by a single blank line.

Output

For each test case, print "FIRST" if the player who makes the first move wins, "SECOND" if the other player wins, or "DRAW" if no player wins.

Examples1

  1. Example 1

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