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 <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int N = 100010;
const int INF = 1LL<<55;
const int mod = (1e9)+7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
typedef pair<int,int> P;
typedef long long ll;
ostream& operator<<(ostream& o,P p){
  return o<<"("<<p.first<<","<<p.second<<")";
}
istream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}
template<class T> istream& operator>>(istream& i,vector<T> &a){for(auto &t:a)cin>>t;return i;}
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
template<class T> void prArr(T a,string s=" "){int i=0;for(auto t:a)cout<<(i++?s:"")<<t;cout<<endl;}

class RSQ{
public:
  typedef long long ll;
  ll n;
  vector<ll> dat,td;
  
  RSQ(){n = -1;}
  RSQ(int n_){
    n=1;
    while(n<n_)n*=2; //要素数nを2のべき乗に
    dat.resize(2*n-1,0),td.resize(2*n-1,0);
  }
  
  //[a,b)の区間にxを加算する,query(a,b,x);
  ll add(int a,int b,ll x,int k=0,int l=0,int r=-1){
    if(r==-1) r=n, assert(a < n && b <= n);
    if(r<=a||b<=l)return 0;
    if(a<=l&&r<=b){
      dat[k]+=(r-l)*x; 
      td[k]+=x;
      return dat[k];
    }

    dat[k]+=(min(r,b)-max(l,a))*x;
    ll kl=k*2+1,kr=k*2+2,t=td[k]*(r-l)/2;
    dat[kl]+=t, dat[kr]+=t;
    td[kl]+=td[k], td[kr]+=td[k];
    td[k]=0;

    ll vl=add(a,b,x,k*2+1,l,(l+r)/2);
    ll vr=add(a,b,x,k*2+2,(l+r)/2,r);
    return vl+vr;
  }
  
  //[a,b)の総和を得る
  ll sum(int a,int b){return add(a,b,0);}
};


vector<vector<P> > G;

vector<int> dijkstra(int start){
  int n = G.size();
  vector<int> D(n,INF);
  priority_queue<P,vector<P>,greater<P> > Q;
  Q.push(P(0,start));
  D[start] = 0;
  
  while(!Q.empty()){
    auto t = Q.top(); Q.pop();
    int cost,pos; tie(cost,pos) = t;

    if(D[pos] < cost) continue;
    
    for(auto p:G[pos]){
      int to = p.first;
      int ncost = cost + p.second;
      if(D[to] <= ncost) continue;
      D[to] = ncost;
      Q.push(P(ncost,to));
    }
    
  }
  return D;
}

typedef pair<P,int> PP;
signed main(){
  int n,m;

  cin>>n>>m;

  G.resize(n);
  for(int i=0;i<m;i++){
    int a,b,c;
    cin>>a>>b>>c;
    G[a].push_back(P(b,c));
    G[b].push_back(P(a,c));
  }


  auto DA = dijkstra(0);
  auto DB = dijkstra(n-1);

  priority_queue<P,vector<P>,greater<P> > D;
  for(int i=0;i<n;i++) D.push(P(DA[i],DB[i]));
  
  vector<int> X;
  for(int i=0;i<n;i++){
    int a = DB[i];
    X.push_back(a);
    X.push_back(a+1);
    X.push_back(a-1);
  }

  for(int i=0;i<n;i++){
    int a = DA[i];
    X.push_back(a);
    X.push_back(a+1);
    X.push_back(a-1);
  }
  
  
  auto get=[&](int x){
    return lower_bound(X.begin(),X.end(),x) - X.begin();
  };
  
  
  int q;
  cin>>q;
  vector<PP> A(q);
  for(int i=0;i<q;i++){
    int fs,fg;
    cin>>fs>>fg;
    A[i] = PP(P(fs,fg),i);
  }

  for(int i=0;i<q;i++){
    int a = A[i].first.first;
    X.push_back(a);
    X.push_back(a+1);
    X.push_back(a-1);
  }
  
  for(int i=0;i<q;i++){
    int a = A[i].first.second;
    X.push_back(a);
    X.push_back(a+1);
    X.push_back(a-1);
  }

  sort(X.begin(),X.end());
  X.erase(unique(X.begin(),X.end()),X.end());


  sort(A.begin(),A.end());


  RSQ rsq(X.size()+100);

  auto add=[&](int fs){
    while(!D.empty() && D.top().first <= fs){
      int fg = D.top().second; D.pop();
      int idx = get(fg);
      rsq.add(idx, idx+1,1);
    }
  };

  auto calc=[&](int fg){
    int idx = get(fg);
    int res = rsq.sum(idx, rsq.n);
    return res;
  };


  vector<int> Ans(q);
  for(int i=0;i<q;i++){
    int fs = A[i].first.first;
    int fg = A[i].first.second;
    int idx = A[i].second;
    
    add(fs);
    int ans = calc(fg);
    Ans[idx] = ans;
  }

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