Wheel of Fortune

Time limit: 0.44s Memory limit: 64MB Input: Output:

Task


William is playing the Wheel of Fortune, a famous game show. The wheel is a "sliced" circle, with numbers on each slice. It can be seen as a circular array of 2N2N elements.

The rules are as follows. William can "turn" the wheel by rotating the array, i.e. moving each of its elements ahead by some positions (possibly making several complete turns). For example, if the array is 2 1 5 32 \ 1 \ 5 \ 3, a rotation of 11 would yield the following array: 3 2 1 53 \ 2 \ 1 \ 5. Rotating again by 22 would yield 1 5 3 21 \ 5 \ 3 \ 2. Rotating again by 11 would yield the original array.

In order to win the game, William needs some information about the wheel everytime he turns it. Specifically: he needs to know the min among the first NN elements and the max among the last NN elements. Initially, in the previous example, the min and max values are 11 and 55. After the first rotation they become 22 and 55, then they become 11 and 33, and finally they become 11 and 55 again when the original ordering is restored.

Note: the "turns" add up, that is, the wheel is not reset after each turn.

Input data

The first line contains a single integer NN: half of the length of the wheel. The second line contains 2N2N integers WiW_i. The third line contains a single integer QQ: the number of turns. The fourth line contains QQ space-separated integers KiK_i, the list of "turns" William will do.

Output data

You need to write QQ lines, each of them corresponding to a turn and containing two integers:

  • The min of the first half of the wheel after performing the turn;
  • The max of the second half of the wheel after performing the turn.

Constraints and clarifications

  • 1N,Q40 0001 \leq N, Q \leq 40 \ 000
  • 1Wi40 0001 \leq W_i \leq 40 \ 000 for each i=0N1i = 0 \dots N − 1
  • 1Ki40 0001 \leq K_i \leq 40 \ 000 for each i=0Q1i = 0 \dots Q − 1
# Points Constraints
1 5 Examples.
2 25 N,Q10N, Q \leq 10.
3 40 N,Q100N, Q \leq 100.
4 30 No additional constraints.

Example 1

stdin

2
2 1 5 3
3
1 2 1

stdout

2 5
1 3
1 5

Explanation

This sample case is the same example presented earlier.

Example 2

stdin

3
2 9 8 3 5 5
3
2 1 8

stdout

2 9
3 9
3 5

Explanation

The wheel 2 9 8 3 5 52 \ 9 \ 8 \ 3 \ 5 \ 5 gets turned by 22 and becomes 5 5 2 9 8 35 \ 5 \ 2 \ 9 \ 8 \ 3, then 11 and becomes 3 5 5 2 9 83 \ 5 \ 5 \ 2 \ 9 \ 8, then 88 and becomes 9 8 3 5 5 29 \ 8 \ 3 \ 5 \ 5 \ 2.

Log in or sign up to be able to send submissions!