This page is still under construction.

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

Pole Position

Interview

Time limit1sMemory limit128 MB

Summary
Given the current race order of N cars with each car's position change from the start, reconstruct the starting grid or report that no valid grid exists.
Level

Medium5 of 10

Topics
Array, Sorting, Implementation, Hash map
Solved
No attempts yet

Problem

In car races, a tall pole always stands next to the finish line of the track.

Before the race starts, the pole displays the starting grid: the number of the car in first place on the grid is shown at the top of the pole, the number of the car in second place is shown just below it, and so on.

During the race, the pole displays the current standings: the car currently leading the race is shown at the top, the car in second place just below it, and so on.

Besides each car's current position, the pole also shows, next to each car number, an integer telling how many positions the car has gained or lost compared to the starting grid.

  • A positive value vv next to a car number means the car has gained vv positions relative to the starting grid.
  • A negative value vv means the car has lost ∣v∣|v| positions relative to the starting grid.
  • A 00 means the car is in exactly the same position it started in.

We are in the middle of the Swedish Grand Prix, the final race of the World Championship. The race director, Dr. Shoo Makra, is worried: there have been complaints that the software controlling the pole is defective and may be showing information that does not match the real order of the race.

To check the pole, Dr. Shoo Makra wants to reconstruct the starting grid from the information currently shown on the pole. If a valid starting grid can be reconstructed, he will compare it against the real starting grid. If no valid starting grid can be reconstructed, the pole software is definitely defective.

The pole lists the cars from top to bottom, so the ii-th car listed is currently in position ii. Given this list, reconstruct the starting grid, or report that it is impossible.

Can you help Dr. Shoo Makra?

Input

The input contains several test cases.

The first line of each test case contains one integer NN, the number of cars in the race (2≤N≤1032 \le N \le 10^3).

Each of the next NN lines contains two integers CC and PP separated by a single space: a car number CC (1≤C≤1041 \le C \le 10^4) and the number of positions PP that car has gained (positive) or lost (negative) relative to the starting grid (−106≤P≤106-10^6 \le P \le 10^6), as shown on the pole. The lines are given in pole order, so the ii-th of these lines describes the car currently in position ii. All cars in a race have distinct numbers.

The end of the input is indicated by a line containing a single zero.

Output

For each test case, print a single line with the reconstructed starting grid: the car numbers in starting-grid order (position 1 first), separated by single spaces.

If it is impossible to reconstruct a valid starting grid, print a single line containing only −1-1.

Examples1

  1. Example 1

    Input
    4
    1 0
    3 1
    2 -1
    4 0
    4
    22 1
    9 1
    13 0
    21 -2
    3
    19 1
    9 -345
    17 0
    7
    2 2
    8 0
    5 -2
    7 1
    1 1
    9 1
    3 -3
    0
    
    Expected output
    1 2 3 4
    -1
    -1
    5 8 2 3 7 1 9