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 <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cassert>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
#include <iomanip>
#include <fstream>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
const ll INF = 1LL<<61;
typedef pair<int,int> pii;

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

vector<pii> g[100000];
int n,m;
ll dist[100000];
int idmap[100000];

typedef pair<ll,int> pli;
vector<pli> v;

void dijkstra_etc() {
  priority_queue<pli> Q;
  Q.push(pli(0,n-1));
  REP(i,n)dist[i]=INF;
  dist[n-1] = 0;
  while(!Q.empty()) {
    pli p = Q.top();  Q.pop();
    ll d = -p.first;
    if (dist[p.second] < d) continue;
    FOR(it, g[p.second]) {
      ll dd = d + it->second;
      if (dist[it->first] > dd) {
        dist[it->first] = dd;
        Q.push(pli(-dd,it->first));
      }
    }
  }
  v.clear();
  REP(i,n) v.push_back(pli(dist[i], i));
  sort(ALL(v));
  REP(i,n) {
    idmap[v[i].second] = i;
  }
}

const int MAX_N = 1<<17;        // 131072

struct SegSum {
  int bit[MAX_N], n;
  SegSum(int n) : n(n) {
    memset(bit, 0, sizeof(bit));
  }
  int sum(int i) {              // [1,i]の和
    int s = 0;
    while(i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  int sum2(int a, int b) {      // [a,b]の和
    return sum(b)-sum(a-1);
  }
  void add(int i, int x) {      // i(>=1)にx足す
    while(i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }
  void addSeg(int a, int b, int x) {   // [a,b]をx個加える
    add(a, x);
    add(b+1, -x);
  }
};

ll dis2[100000];
int ans[100000];

int main() {
  cin>>n>>m;
  REP(i,m) {
    int a, b, c;
    cin >> a >> b >> c;
    g[a].push_back(pii(b,c));
    g[b].push_back(pii(a,c));
  }
  dijkstra_etc();
  int qn;
  cin >> qn;
  vector<Query> qs;
  REP(i,qn) {
    Query tmp;
    cin >> tmp.fs >> tmp.fg;
    tmp.id = i;
    qs.push_back(tmp);
  }
  sort(ALL(qs));
  SegSum segsum(n);
  priority_queue<pli> Q;
  REP(i,n) dis2[i]=INF;
  Q.push(pli(0,0));
  dis2[0] = 0;
  FOR(q, qs) {
    // cout << q->fs << " " << q->fg << " " << q->id << endl;
    while(!Q.empty()) {
      pli p = Q.top();
      ll d = -p.first;
      if (d > q->fs) break;
      Q.pop();
      if (dis2[p.second]<d) continue;
      segsum.add(idmap[p.second]+1, 1);
      // cout << "a" << p.second << " " << idmap[p.second] << endl;
      FOR(it, g[p.second]) {
        ll dd = d + it->second;
        if (dis2[it->first] > dd) {
          dis2[it->first] = dd;
          Q.push(pli(-dd, it->first));
        }
      }
    }
    int up = lower_bound(ALL(v), pli(q->fg, 0)) - v.begin();
    // FOR(it, v) cout << it->first << " "; cout << endl;
    // FOR(it, v) cout << it->second << " "; cout << endl;
    // cout << "up = " << up << endl;
    ans[q->id] = segsum.sum2(up+1,n);
  }
  REP(i,qn) {
    cout << ans[i] << endl;
  }  
}