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:
typedef long long ll;

#include <iostream>
#include <map>
#include <algorithm>
#include <queue>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

#include <cstdio>
inline int getInt(){ int s; scanf("%d", &s); return s; }
inline ll getLL(){ ll s; scanf("%lld", &s); return s; }

#include <set>
using namespace std;

vector<pair<int, int> > g[100000];

vector<ll> dijkstra(int pos, int n){
  vector<ll> ret(n, -1);

  typedef pair<ll, int> data;
  priority_queue<data, vector<data>, greater<data> > pq;

  pq.push(data(0, pos));

  while(pq.size()){
    const data d = pq.top(); pq.pop();
    const int now = d.second;
    const ll cost = d.first;

    if(ret[now] != -1) continue;
    ret[now] = cost;

    REP(i,g[now].size()){
      const int next = g[now][i].first;
      const ll cc = cost + g[now][i].second;

      if(ret[next] == -1){
	pq.push(data(cc, next));
      }
    }
  }

  return ret;
}

const int MAX_N = 1 << 20;
int n, dat[2 * MAX_N - 1];
void init(int n_){
  n = 1;
  while(n < n_) n *= 2;
  REP(i, 2 * n - 1) dat[i] = 0;
}

void update(int k, int a){
  k += n - 1;
  dat[k] = a;

  while(k > 0){
    k = (k - 1) / 2;
    dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
  }
}

int query(int a, int b, int k, int l, int r){
  if(r <= a || b <= l) return 0;

  if(a <= l && r <= b) return dat[k];

  int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);

  return vl + vr;
}

int main(){
  const int n = getInt();
  const int m = getInt();

  init(n);

  REP(i,m){
    const int a = getInt();
    const int b = getInt();
    const int c = getInt();

    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
  }

  vector<ll> sd = dijkstra(0, n);
  vector<ll> gd = dijkstra(n - 1, n);
  vector<ll> gds = gd; sort(gds.begin(), gds.end());
  map<ll, vector<int> > gdm;

  vector<pair<ll, ll> > d(n);

  REP(i,n){
    d[i].first  = sd[i];
    d[i].second = gd[i];
  }
  sort(d.begin(), d.end());

  REP(i,n){
    gdm[d[i].second].push_back(i);
  }

  const int q = getInt();
  vector<pair<pair<ll, ll>, int> > que(q);

  REP(i,q){
    que[i].first.second = getLL();
    que[i].first.first = getLL();
    que[i].second = i;
  }

  sort(que.begin(), que.end());
  int gpos = 0;

  vector<int> ans(q);

  REP(i,q){
    const ll fs = que[i].first.second;
    const ll fg = que[i].first.first;

    while(gpos != n && fg > gds[gpos]){
      const vector<int> &v = gdm[gds[gpos]];

      REP(j,v.size()){
	update(v[j], 1);
      }

      gpos++;
    }

    const int lim = lower_bound(d.begin(), d.end(), pair<ll, ll>(fs + 1, -1)) - d.begin();

    ans[que[i].second] = lim - query(0, lim, 0, 0, ::n);
    // printf("%d %d : lim = %d, ans = %d  (gpos = %d)\n", fs, fg, lim, ans[que[i].second], gpos);
  }

  REP(i,q){
    printf("%d\n", ans[i]);
  }

  return 0;
}