Under construction

Still being built. Things may change

Sorting Game

Time limit1sMemory limit128 MB

Summary
Apply K sets of operations, each sorting a prefix of length A ascending then a prefix of length B descending, and print the final sequence.
Level

Hard8 of 10

Topics
Sorting, Implementation, Simulation, Greedy
Solved
No attempts yet

Problem

In this game you repeatedly sort a prefix of a sequence in ascending order, then sort a prefix again in descending order. One ascending sort followed by one descending sort is one set.

For example, take the sequence [4, 1, 2, 3] and a set that sorts elements 1 through 3 in ascending order and then elements 1 through 2 in descending order. The ascending sort gives [1, 2, 4, 3], and the descending sort that follows gives [2, 1, 4, 3]. That is the sequence after the set.

You are given a sequence and KK sets. Write a program that prints the sequence after all KK sets have been carried out in the given order.

Input

The first line contains the length of the sequence, NN. (1≤N≤100,0001 \le N \le 100{,}000)

The second line contains the NN elements of the sequence, separated by spaces. Each element is an integer between −10,000-10{,}000 and 10,00010{,}000, inclusive.

The third line contains the number of sets, KK. (1≤K≤100,0001 \le K \le 100{,}000)

Each of the next KK lines contains two integers AA and BB describing one set, separated by a space. (1≤A≤N1 \le A \le N, 1≤B≤N1 \le B \le N) The set sorts elements 1 through AA in ascending order, then sorts elements 1 through BB in descending order. Carry out the sets in the order given in the input.

Output

Print the NN elements of the sequence after all KK sets, separated by spaces, on one line.

Examples4

  1. Example 1

    Input
    4
    4 1 2 3
    1
    3 2
    
    Expected output
    2 1 4 3
    
  2. Example 2

    Input
    1
    7
    1
    1 1
    
    Expected output
    7
    
  3. Example 3

    Input
    6
    -3 5 -3 10000 -10000 0
    1
    6 6
    
    Expected output
    10000 5 0 -3 -3 -10000
    
  4. Example 4

    Input
    5
    2 9 4 4 1
    1
    5 1
    
    Expected output
    1 2 4 4 9