Problem: You are involved in the development of a certain game. The game is for players to explore randomly generated dungeons.

There are n rooms in the dungeon generated by this game, and they are numbered from 0 to n-1. The rooms are connected by a passage. There are m passages connecting rooms. The passage can go in either direction. In addition, a distance is set between the rooms. In the generated dungeon, it is possible to go from one room to all other rooms via several passages. Then, when the player plays the game, the 0th room is selected as the starting point and the n-1th room is selected as the goal point.

I want to set up a treasure chest in some room with items that will help users explore the dungeon. At that time, it is meaningless if it is too far from the starting point or too close to the goal point. Therefore, we would like to make a room that takes at least fg as a candidate for installing a treasure chest when we reach within the distance fs from the start and take the shortest path to reach the goal.

Given the generated dungeon and q. For q queries, count the number of candidate locations for treasure chests.



Input

The input is given in the following format.


n m
a1 b1 c1
..
..
..
am bm cm
q
fs1 fg1
..
..
..
fsq fgq


ai bi ci means that the distance of the passage connecting the rooms ai and bi is ci.

Input meets the following constraints
2 ≤ n ≤ 100,000
0 ≤ ai, bi <n
0 ≤ ci ≤ 100,000
n-1 ≤ m ≤ 100,000
1 ≤ q ≤ 100,000
0 ≤ fsi, fgi ≤ 260

Output

Print the answer value on one line for each query

Example

Input

4 4
0 1 3
1 3 4
0 2 2
2 3 1
4
0 0
2 2
2 1
3 4


Output

1
1
2
1

Solution (in CPP):

Solution:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

typedef long long ll;

const int MAX_N = 100000;
const int MAX_M = 100000;
const int MAX_Q = 100000;
const int MAX_BITS = 1 << 17;
const ll INF = 1LL << 62;

/* typedef */

typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef vector<pil> vpil;

struct Stat {
  ll d;
  int i;
  Stat() {}
  Stat(ll _d, int _i) { d = _d, i = _i; }
  bool operator<(const Stat& s) const { return d > s.d; }
};

struct Query {
  ll fs, fg;
  int i;
  bool operator<(const Query& q) const { return fs < q.fs; }
};

struct BIT {
  int n;
  int bits[MAX_BITS];

  BIT() {}
  BIT(int _n) { init(_n); }
  
  void init(int _n) { n = _n; }

  int sum(int x) {
    int s = 0;
    while (x > 0) {
      s += bits[x];
      x -= (x & -x);
    }
    return s;
  }

  void add(int x, int v) {
    while (x <= n) {
      bits[x] += v;
      x += (x & -x);
    }
  }

  void print(int m) {
    for (int i = 1; i <= m; i++) cout << sum(i) << ' ';
    cout << endl;
  }
};

/* global variables */

int n, m, q;
vpil nbrs[MAX_N];
ll sdists[MAX_N], gdists[MAX_N];
pll sgdists[MAX_N];
Query qrs[MAX_Q];
BIT bit;
int ans[MAX_Q];

/* subroutines */

void calc_dists(int st, ll *dists) {
  for (int i = 0; i < n; i++) dists[i] = INF;
  dists[st] = 0;

  priority_queue<Stat> q;
  q.push(Stat(0, st));

  while (! q.empty()) {
    Stat u = q.top();
    q.pop();
    if (dists[u.i] != u.d) continue;

    vpil& nbru = nbrs[u.i];
    
    for (vpil::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
      int vi = vit->first;
      ll vd = u.d + vit->second;

      if (dists[vi] > vd) {
	dists[vi] = vd;
	q.push(Stat(vd, vi));
      }
    }
  }
}

/* main */

int main() {
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    nbrs[a].push_back(pil(b, c));
    nbrs[b].push_back(pil(a, c));
  }

  calc_dists(0, sdists);
  calc_dists(n - 1, gdists);

  for (int i = 0; i < n; i++)
    sgdists[i].first = sdists[i], sgdists[i].second = gdists[i];

  sort(sgdists, sgdists + n);

  sort(gdists, gdists + n);
  int gn = unique(gdists, gdists + n) - gdists;
  
  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> qrs[i].fs >> qrs[i].fg;
    qrs[i].i = i;
  }
  sort(qrs, qrs + q);

  bit.init(gn);
  int sgi = 0;
  
  for (int qi = 0; qi < q; qi++) {
    //printf("qrs[%d]=(%lld,%lld)\n", qrs[qi].i, qrs[qi].fs, qrs[qi].fg);
    while (sgi < n && sgdists[sgi].first <= qrs[qi].fs) {
      int gi = lower_bound(gdists, gdists + gn, sgdists[sgi].second) - gdists;
      //printf("  gi=%d\n", gi);
      bit.add(gn - gi, 1);
      sgi++;
      //bit.print(gn);
    }

    int fgi = lower_bound(gdists, gdists + gn, qrs[qi].fg) - gdists;
    //printf("  fgi=%d\n", fgi);
    ans[qrs[qi].i] = bit.sum(gn - fgi);
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
  
  return 0;
}