Under construction

Still being built. Things may change

Dice

Interview

Time limit1sMemory limit128 MB

Summary
Simulate a board game where players advance by dice totals, skip a turn when landing on one of three traps, and the first past the last square wins.
Level

Medium4 of 10

Topics
Simulation, Implementation, Array
Solved
No attempts yet

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 PP and SS: the number of players and the number of squares in the trail (1≤P≤101 \le P \le 10, 3≤S≤100003 \le S \le 10000). The second line has three distinct integers T1T_1, T2T_2, T3T_3, the positions of the traps (1≤T1,T2,T3≤S1 \le T_1, T_2, T_3 \le S). The third line has a single integer NN, the number of dice rolls in this test. Each of the next NN lines has two integers D1D_1 and D2D_2 (1≤D1,D2≤61 \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=0P = S = 0), which is not processed.

Players are numbered 11 to PP and take their turns in the order 1,2,…,P1, 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.

Examples2

  1. Example 1

    Input
    2 10
    2 4 8
    4
    1 1
    3 4
    1 2
    6 5
    3 7
    4 5 7
    7
    1 2
    2 2
    2 1
    1 1
    1 2
    1 1
    1 1
    0 0
    
    Expected output
    1
    3
    
  2. Example 2

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