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 <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
struct Edge{
  int u, v;
  ll c;
  Edge(int u, int v, ll c) :
    u(u), v(v), c(c) {}
  bool operator < (const Edge& e) const {
    return c > e.c;
  }
};
typedef vector<Edge> Node;
typedef vector<Node> Graph;
void dijkstra(vector<ll>& dist, Graph& G, int s){
  int N = G.size();
  dist = vector<ll>(N, -1);
  priority_queue<Edge> que;
  que.push(Edge(-1, s, 0));
  dist[s] = 0;
  while(!que.empty()){
    Edge e = que.top(); que.pop();
    if(dist[e.v] < e.c) continue;
    REP(i, G[e.v].size()){
      Edge f = G[e.v][i];
      if(dist[f.v] == -1 || dist[f.v] > e.c + f.c){
        dist[f.v] = e.c + f.c;
        que.push(Edge(f.u, f.v, e.c + f.c));
      }
    }
  }
}

struct RangeTree{
  static const int ST_SIZE = (1<<17);
  vector<ll> dat[2 * ST_SIZE];
  void init(int k, int l, int r, vector<ll>& v){
    if(r - l == 1){
      dat[k].push_back(v[l]);
    }else{
      int lch = k * 2 + 1, rch = k * 2 + 2;
      init(lch, l, (l + r) / 2, v);
      init(rch, (l + r) / 2, r, v);

      dat[k].resize(r - l);
      merge(dat[lch].begin(), dat[lch].end(), dat[rch].begin(), dat[rch].end(), dat[k].begin());
    }
  }
  int query(int i, int j, ll x, int k, int l, int r){
    if(j <= l || r <= i) return 0;
    if(i <= l && r <= j){
      return upper_bound(dat[k].begin(), dat[k].end(), x) - dat[k].begin();
    }else{
      int lc = query(i, j, x, 2 * k + 1, l, (l + r) / 2);
      int rc = query(i, j, x, 2 * k + 2, (l + r) / 2, r);
      return lc + rc;
    }
  }
};



int main(){
  int N, M;
  while(cin>>N>>M && N){
    Graph G(N);
    REP(i, M){
      int a, b, c;
      cin>>a>>b>>c;
      G[a].push_back(Edge(a, b, c));
      G[b].push_back(Edge(b, a, c));
    }
    vector<ll> dist, rdist;
    dijkstra(dist, G, 0);
    dijkstra(rdist, G, N - 1);

    vector<pair<ll, ll> > ps(N);
    REP(i, N){
      ps[i] = make_pair(dist[i], -rdist[i]);
    }
    sort(ps.begin(), ps.end());
    REP(i, N) {
      dist[i] = ps[i].first;
      rdist[i] = ps[i].second;
    }
    RangeTree tree;
    tree.init(0, 0, N, rdist);

    int Q; cin>>Q;
    REP(i, Q){
      ll fs, fg;
      cin>>fs>>fg;
      int e = upper_bound(dist.begin(), dist.end(), fs) - dist.begin();
      int ans = tree.query(0, e, -fg, 0, 0, N);
      cout<<ans<<endl;
    }
  }
  return 0;
}