Under construction

Still being built. Things may change

Dining Hall Support Dispatch

Time limit1sMemory limit1024 MB

Summary
Given a day d and a soldier number x, find how many times the dispatch program picks soldier x through day d.
Level

Medium7 of 10

Topics
Simulation, Greedy, Math
Solved
No attempts yet

Problem

Soldiers from units near the military dining hall go to the hall to help with its work. This duty is called dining support. The dining support officer decides who goes on dining support and when.

At first, the officer sent three soldiers chosen at random to the hall each day. Tired of picking them by hand every day, the officer built a dispatch program to make the selection simpler and more systematic.

The dispatch program runs every morning and picks soldiers in order of how few dining support duties they have had on that day. Specifically, it selects the three soldiers with the smallest sum of two counts: the number of times they were picked at random before the program started, and the number of times the program picked them from its first day up to the day before the current dispatch. If two soldiers have the same count, the one with the smaller service number is picked first.

Now the soldiers know the dispatch is not random, and they want to know the results in advance. They have started calling the officer to ask how many times the program will pick them through day dd of operation. Given the number of days dd and a soldier's service number, find how many times the program picks that soldier.

Input

The first line contains the number of soldiers NN. (3≤N≤10 000)(3 \le N \le 10\,000)

The second line contains a1,a2,⋯ ,aNa_1, a_2, \cdots, a_N, the number of times each soldier from service number 11 to NN was picked at random before the program started, separated by spaces. (1≤ai≤30)(1 \le a_i \le 30)

The third line contains the number of calls QQ. (1≤Q≤100 000)(1 \le Q \le 100\,000)

The next QQ lines each contain a day did_i and a service number xix_i, separated by a space. (1≤di≤109;1≤xi≤N)(1 \le d_i \le 10^9; 1 \le x_i \le N)

Output

For each call, in the order the calls were received, print one integer on its own line: the number of times the program picks that soldier through day dd.

Examples2

  1. Example 1

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

    Input
    5
    1 4 10 10 10
    4
    2 1
    3 4
    1 3
    4 3
    
    Expected output
    2
    1
    1
    2