Toy store

시간 제한2초메모리 제한1024 MB

요약
고객이 어떤 종류를 샀는지 알 수 없는 상황에서, 매 분마다 구매 가능한 장난감 종류의 가능 상한과 확실 하한을 계산한다.
난이도

보통10점 중 7점

유형
시뮬레이션, 그리디, 구현, 수학
정답자
아직 제출이 없습니다

문제

Taja often went by the toy store and looked at electoronic table near the store window which displayed two integers. Store window shows several kinds of toys, but numbers on the table may be out of sync with actual number of different kinds. It turned out that, not every toy from the store window can be bought, because they do not remove toys as soon as possible, but only after some time past the purchase. For different kinds of toys this time can be different.

There are nn kinds of toys. For each kind of toy it is known that initial amount of toys is c_ic\_i and time is t_it\_i minutes after the purchase, after which the toy is removed from the window. Each minute the following happens:

  • removal of toys from the store window, that were purchased corresponding amount of minutes ago;
  • the electronic table is updated;
  • new customer comes and necessarily buys some toy, that is remaining in stock.

Taja has been always interested the meaning of the numbers on the electronic table and recently she found it out. Both numbers show how many kinds of toys can be bought in the store, but first one shows number of kinds, possibly in stock up to the current moment, and second one is number of kinds, that are in stock up to the current moment for sure. Also Taja is interested how much is this table informative for customers. That's why she needs a program, that will model behaviour of the customers and update the table.

Your task is: for each minute calculate electronic table numbers.

입력

First line of the input contains single integer nn (1≤n≤1051 \leq n \leq 10^5) --- number of kinds of the toys.

Each of the following nn lines contains two integers c_ic\_i and t_it\_i (1≤c_i≤1051 \leq c\_i \leq 10^5, 1≤t_i≤1001 \leq t\_i \leq 100) --- number of toys of iith kind and time, after which the toy will be removed out of the store window after the purchase correspondingly.

Next string contains single integer kk (1≤k≤1051 \leq k \leq 10^5) --- number of customers.

Each fo the following kk lines contains integer q_iq\_i and q_iq\_i integers p_1,p_2,...,p_q_ip\_1, p\_2, ..., p\_{q\_i} --- number of toys, that were removed at iith minute and numbers of these toys.

출력

Output should contain kk lines, each of which contains two integers a_ia\_i and b_ib\_i --- numbers on the electronic table at moment of the beginning of iith minute correspondingly.

힌트

In the above example, the store window contains one toy of the first kind, two toys of the second kind and three toys of the third kind, which were removed after 22, 11 and 33 minutes correspondingly after the purchase. Numbers on the table shoud change in the following order:

  • 3/33/3: there were no customers before the first one, he can buy any toy.
  • 3/23/2: first customer could possibly buy the toy of first kind, thus there's no confidence, that second customer can buy it.
  • 3/23/2: since the toy of neither the first kind nor second kind has been removed from the store window, then it means that first customer purchased the toy of the third kind. What was purchased by the second customer is impossible to decide yet.
  • 2/22/2: no toys of the first kind anymore.
  • 2/22/2: no toy has been removed from the store window, which means that previous customer bought a toy of the third kind.
  • 1/11/1: There's only one toy of the third kind remaining.

예제1

  1. 예제 1

    입력
    3
    1 2
    2 1
    3 3
    6
    0
    0
    0
    3 1 2 3
    0
    1 2
    
    예상 출력
    3 3
    3 2
    3 2
    2 2
    2 2
    1 1