New Year Domino

Celebrating the new year, many people post videos of falling dominoes; Here’s a list of them: https://www.youtube.com/results?search_query=New+Years+Dominos

User ainta, who lives in a 2D world, is going to post a video as well.

There are n dominoes on a 2D Cartesian plane. i-th domino (1 ≤ i ≤ n) can be represented as a line segment which is parallel to the y-axis and whose length is l i. The lower point of the domino is on the x-axis. Let’s denote the x-coordinate of the i-th domino as p i. Dominoes are placed one after another, so p 1 < p 2 < … < p n - 1 < p n holds.

User ainta wants to take a video of falling dominoes. To make dominoes fall, he can push a single domino to the right. Then, the domino will fall down drawing a circle-shaped orbit until the line segment totally overlaps with the x-axis.

Also, if the s-th domino touches the t-th domino while falling down, the t-th domino will also fall down towards the right, following the same procedure above. Domino s touches domino t if and only if the segment representing s and t intersects.

See the picture above. If he pushes the leftmost domino to the right, it falls down, touching dominoes (A), (B) and (C). As a result, dominoes (A), (B), (C) will also fall towards the right. However, domino (D) won’t be affected by pushing the leftmost domino, but eventually it will fall because it is touched by domino (C) for the first time.

The picture above is an example of falling dominoes. Each red circle denotes a touch of two dominoes.

User ainta has q plans of posting the video. j-th of them starts with pushing the x j-th domino, and lasts until the y j-th domino falls. But sometimes, it could be impossible to achieve such plan, so he has to lengthen some dominoes. It costs one dollar to increase the length of a single domino by 1. User ainta wants to know, for each plan, the minimum cost needed to achieve it. Plans are processed independently, i. e. if domino’s length is increased in some plan, it doesn’t affect its length in other plans. Set of dominos that will fall except x j-th domino and y j-th domino doesn’t matter, but the initial push should be on domino x j.

Input

The first line contains an integer n (2 ≤ n ≤ 2 × 105)— the number of dominoes.

Next n lines describe the dominoes. The i-th line (1 ≤ i ≤ n) contains two space-separated integers p il i (1 ≤ p i, l i ≤ 109)— the x-coordinate and the length of the i-th domino. It is guaranteed that p 1 < p 2 < … < p n - 1 < p n.

The next line contains an integer q (1 ≤ q ≤ 2 × 105) — the number of plans.

Next q lines describe the plans. The j-th line (1 ≤ j ≤ q) contains two space-separated integers x jy j (1 ≤ x j < y j ≤ n). It means the j-th plan is, to push the x j-th domino, and shoot a video until the y j-th domino falls.

Output

For each plan, print a line containing the minimum cost needed to achieve it. If no cost is needed, print 0.

Examples

input

6
1 5
3 3
4 4
9 2
10 1
12 1
4
1 2
2 4
2 5
2 6

output

0
1
1
2

Note

Consider the example. The dominoes are set like the picture below.

Let’s take a look at the 4th plan. To make the 6th domino fall by pushing the 2nd domino, the length of the 3rd domino (whose x-coordinate is 4) should be increased by 1, and the 5th domino (whose x-coordinate is 9) should be increased by 1 (other option is to increase 4th domino instead of 5th also by 1). Then, the dominoes will fall like in the picture below. Each cross denotes a touch between two dominoes.

Solution:

#include <cstring>
#include <vector>
#include  <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

using namespace std;

const int N = 800010;

int mx[N], rg[N];

void build(int x, int l, int r) {
if (l == r) {
mx[x] = l;
} else {
int y = (l + r) >> 1;
build(x + x, l, y);
build(x + x + 1, y + 1, r);
mx[x] = (rg[mx[x + x]] > rg[mx[x + x + 1]] ? mx[x + x] : mx[x + x + 1]);
}
}

int find_max(int x, int l, int r, int ll, int rr) {
if (rr < l || r < ll) {
    return -1;
  }
  if (ll <= l && r <= rr) {
    return mx[x];
  }
  int y = (l + r) >> 1;
int a = find_max(x + x, l, y, ll, rr);
int b = find_max(x + x + 1, y + 1, r, ll, rr);
if (a == -1) return b;
if (b == -1) return a;
return (rg[a] > rg[b] ? a : b);
}

int x[N], len[N];
int nx[N], cost[N];

const int LOG = 20;

int to[N][LOG], pay[N][LOG];

int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
    scanf("%d %d", x + i, len + i);
    rg[i] = x[i] + len[i];
  }
  build(1, 0, n - 1);
  for (int i = 0; i < n - 1; i++) {
    int to = lower_bound(x, x + n, x[i] + len[i] + 1) - x - 1;
    if (to == i) {
      nx[i] = i + 1;
      cost[i] = x[i + 1] - x[i] - len[i];
    } else {
      nx[i] = find_max(1, 0, n - 1, i + 1, to);
      cost[i] = 0;
      if (rg[nx[i]] < rg[i] && to < n - 1) {
        nx[i] = to + 1;
        cost[i] = x[nx[i]] - x[i] - len[i];
      }
    }
  }
  nx[n - 1] = n - 1;
  cost[n - 1] = 0;
  for (int i = 0; i < n; i++) {
    to[i][0] = nx[i];
    pay[i][0] = cost[i];
  }
  for (int j = 1; j < LOG; j++) {
    for (int i = 0; i < n; i++) {
      to[i][j] = to[to[i][j - 1]][j - 1];
      pay[i][j] = pay[to[i][j - 1]][j - 1] + pay[i][j - 1];
    }
  }
  int tt;
  scanf("%d", &tt);
  while (tt--) {
    int st, fin;
    scanf("%d %d", &st, &fin);
    st--; fin--;
    int ans = 0;
    for (int j = LOG - 1; j >= 0; j--) {
if (to[st][j] < fin) {
        ans += pay[st][j];
        st = to[st][j];
      }
    }
    if (x[fin] > rg[st]) {
ans += pay[st][0];
}
printf("%d\n", ans);
}
return 0;
}