Under construction

Still being built. Things may change

On the Bus

Time limit1sMemory limit128 MB

Summary
Track passengers boarding and alighting over a series of stops, never exceeding the bus capacity, and report the final count.
Level

Easy3 of 10

Topics
Simulation, Implementation
Solved
No attempts yet

Problem

A bus leaves the depot with 4 passengers aboard. At the first stop, 3 more passengers board. At the next stop, 1 passenger gets off and 2 board. How many passengers are on the bus now?

Each scenario in this problem follows the same idea: track the passengers as the bus moves through its stops and report how many are aboard once the scenario ends.

Input

The input contains several scenarios.

Each scenario is given as follows:

  • The first line contains a route number (up to 5 letters or digits, no spaces) and ZZ, the capacity of the bus (the maximum number of passengers, 10≤Z≤10010 \le Z \le 100), separated by a space.
  • The second line contains PP, the number of passengers already on the bus (0≤P≤Z0 \le P \le Z).
  • The third line contains SS, the number of stops to process (1≤S≤1001 \le S \le 100).
  • Each of the next SS lines contains two integers separated by a space: the number of passengers getting off at that stop, followed by the number of passengers waiting to board.

At each stop the alighting passengers get off first. If the number of passengers waiting to board is greater than the number of free seats, the passengers who cannot be seated are left behind (standing passengers are not allowed).

The input ends with a line containing exactly # 0. This line is not a scenario and must not be processed.

Output

For each scenario, print one line: the route number exactly as it appears in the input, a single space, and the number of passengers aboard the bus at the end of the scenario.

Examples4

  1. Example 1

    Input
    36A 26
    4
    2
    0 3
    1 2
    # 0
    
    Expected output
    36A 8
    
  2. Example 2

    Input
    1 10
    8
    1
    0 5
    # 0
    
    Expected output
    1 10
    
  3. Example 3

    Input
    AB12 50
    20
    3
    5 10
    10 0
    15 40
    # 0
    
    Expected output
    AB12 40
    
  4. Example 4

    Input
    X 20
    0
    1
    0 20
    Y99 100
    100
    1
    50 60
    # 0
    
    Expected output
    X 20
    Y99 100