Dice

No attempts yetTime limit1sMemory limit128 MB

Problem

A classic children's board game uses a board with a trail of squares and a set of colored pieces. Each player is given one piece, and every piece starts just before the first square of the trail.

The game is played in rounds. In each round the players roll a pair of dice in a fixed order (player 1 first, then player 2, and so on) and move their piece forward by the total of the two dice.

Most squares are ordinary, but some are traps. If a piece ends its move exactly on a trap square, that player must miss the next round: they do not roll, and their piece stays where it is for one round.

There are exactly three trap squares on the trail.

A player wins the moment their piece passes the end of the trail, which lies just after the last square. For example, on a board with squares numbered 1 to 48, a piece on square 41 needs a dice total of at least 8 to move past square 48 and win. There is never a tie.

Given the number of players, the length of the trail, the positions of the three traps, and the sequence of dice rolls, determine which player wins.

Input

The input contains several test cases. The first line of a test case has two integers $P$ and $S$: the number of players and the number of squares in the trail ($1 \le P \le 10$, $3 \le S \le 10000$). The second line has three distinct integers $T_1$, $T_2$, $T_3$, the positions of the traps ($1 \le T_1, T_2, T_3 \le S$). The third line has a single integer $N$, the number of dice rolls in this test. Each of the next $N$ lines has two integers $D_1$ and $D_2$ ($1 \le D_1, D_2 \le 6$), one roll of the two dice. The input ends with a line 0 0 (that is, $P = S = 0$), which is not processed.

Players are numbered $1$ to $P$ and take their turns in the order $1, 2, \dots, P$ every round. The dice rolls are listed in the exact order they are used, and each test provides exactly the rolls needed for some player to win, no more and no fewer.

Output

For each test case, print a single line containing the number of the winning player.