This page is still under construction.

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

S-Nim

Time limit1sMemory limit128 MB

Summary
Given a move set S, decide for each position whether the S-Nim game is a win or a loss by computing Grundy numbers and XORing them over the heaps.
Level

Medium7 of 10

Topics
Game theory, Dynamic programming, Math, Bit manipulation
Solved
No attempts yet

Problem

Arthur and his sister Caroll have been playing a game called Nim for some time now. Nim is played as follows:

  • The starting position has a number of heaps, all containing some (not necessarily equal) number of beads.
  • The players take turns choosing a heap and removing a positive number of beads from it.
  • The first player who cannot make a move loses.

Arthur and Caroll enjoyed this simple game until they discovered an easy way to always find the best move:

  • XOR the number of beads in every heap of the current position. (For example, with heaps 2,4,72, 4, 7 the XOR-sum is 2⊕4⊕7=12 \oplus 4 \oplus 7 = 1.)
  • If the XOR-sum is 00, too bad, you will lose.
  • Otherwise, move so that the XOR-sum becomes 00. This is always possible.

It is easy to convince yourself that this works, using these facts:

  • The player who takes the last bead wins.
  • After the winning player's last move the XOR-sum is 00.
  • The XOR-sum changes after every move.

So if you make sure the XOR-sum is always 00 after your move, your opponent can never win, and therefore you win.

Once both players know how to play perfectly, the game is no longer fun. Fortunately, Arthur and Caroll soon invented a similar game, S-Nim, that seemed to fix this. Each player may now only remove a number of beads that belongs to some predefined set SS; for example, if S={2,5}S = \{2, 5\} then each player may only remove 22 or 55 beads at a time. Now it is not always possible to make the XOR-sum 00, so the strategy above is useless. Or is it?

Your job is to write a program that decides whether a given S-Nim position is a losing or a winning position. A position is a winning position if there is at least one move to a losing position. A position is a losing position if there is no move to a losing position. In particular, a position with no legal moves is a losing position.

Input

The input consists of a number of test cases.

Each test case is given as follows. The first line contains an integer kk (0<k≤1000 < k \le 100), the size of SS, followed by kk integers sis_i (0<si≤100000 < s_i \le 10000) that make up SS. The second line contains an integer mm (0<m≤1000 < m \le 100), the number of positions to evaluate. Each of the next mm lines contains an integer ll (0<l≤1000 < l \le 100), the number of heaps, followed by ll integers hih_i (0≤hi≤100000 \le h_i \le 10000), the number of beads in each heap.

The last test case is followed by a line containing a single 00.

Output

For each position, output the following.

  • Print W if the position is a winning position, or L if it is a losing position.
  • After all positions of a test case have been processed, print a newline.

In other words, the results for the positions of one test case are concatenated on a single line, and a line break is printed at the end of each test case.

Examples1

  1. Example 1

    Input
    2 2 5
    3
    2 5 12
    3 2 4 7
    4 2 3 7 12
    5 1 2 3 4 5
    3
    2 5 12
    3 2 4 7
    4 2 3 7 12
    0
    
    Expected output
    LWW
    WWL