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>
using namespace std;
using Int = long long;

template <typename T>
vector<T> dijkstra(Int s,vector<vector<pair<Int, T> > > & G,T INF){
  using P = pair<T, Int>;
  Int n=G.size();
  vector<T> d(n,INF);
  vector<Int> b(n,-1);
  priority_queue<P,vector<P>,greater<P> > q;
  d[s]=0;
  q.emplace(d[s],s);
  while(!q.empty()){
    P p=q.top();q.pop();
    Int v=p.second;
    if(d[v]<p.first) continue;
    for(auto& e:G[v]){
      Int u=e.first;
      T c=e.second;
      if(d[u]>d[v]+c){
	d[u]=d[v]+c;
	b[u]=v;
	q.emplace(d[u],u);
      }
    }
  }
  return d;
}

using P = pair<Int, Int>;
struct SegmentTree{
  Int n;
  vector<P> v;
  vector<vector<Int> > dat;
  SegmentTree(){};
  SegmentTree(Int n,vector<P> v):v(v){init(n);build();};
  
  void init(Int n_){
    n=1;
    while(n<n_) n*=2;
    dat.clear();
    dat.resize(2*n-1);
  }
  
  void build(){
    for(Int i=0;i<(Int)v.size();i++)
      dat[v[i].first+(n-1)].emplace_back(v[i].second);
    
    for(Int i=0;i<n;i++)
      sort(dat[i+n-1].begin(),dat[i+n-1].end());
    
    for(Int i=n-2;i>=0;i--){
      merge(dat[i*2+1].begin(),dat[i*2+1].end(),
	    dat[i*2+2].begin(),dat[i*2+2].end(),
	    back_inserter(dat[i]));
    }
  }
  // [a,b) * [c,d)
  inline Int query(Int a,Int b,Int c,Int d){
    Int res=0;
    auto calc=[a,b,c,d](vector<Int> &x){
      auto latte=lower_bound(x.begin(),x.end(),d);
      auto malta=lower_bound(x.begin(),x.end(),c);
      return Int(latte-malta);
    };
    for(Int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) res+=calc(dat[(l++)-1]);
      if(r&1) res+=calc(dat[(--r)-1]);
    }
    return res;
  }
  
};

signed main(){  
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n, m;
  Int INF = 1LL << 61;
  cin >> n >> m;

  using P = pair<Int, Int>;
  vector<vector<P> > G(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));
  }

  vector<Int> ds = dijkstra(0, G, INF);
  vector<Int> df = dijkstra(n-1, G, INF);

  vector<Int> fs, fg;
  Int q;
  cin >> q;
  for ( Int i = 0; i < q; i++ ) {
    Int a, b;
    cin >> a >> b;
    //cout << a << " " << b << endl;     
    fs.push_back(a);
    fg.push_back(b);
  }
  

  map<Int, Int> ms,mf;
  {
    vector<Int> d = ds;
    for ( Int i = 0; i < q; i++ ) d.push_back(fs[i]);
    d.push_back(INF);
    sort(d.begin(),d.end());
    d.erase(unique(d.begin(),d.end()),d.end());
    //for ( int i = 0; i < (Int)d.size(); i++ ) cout << d[i] << " ";
    //cout << endl;
    for(Int i=0;i<(Int)d.size();i++) ms[d[i]]=i;
  }
  {
    vector<Int> d = df;
    for ( Int i = 0; i < q; i++ ) d.push_back(fg[i]);
    d.push_back(INF);
    sort(d.begin(),d.end());
    d.erase(unique(d.begin(),d.end()),d.end());
    for(Int i=0;i<(Int)d.size();i++) mf[d[i]]=i;
  }
  
  
  vector<P> vp;
  for ( Int i = 0; i < n; i++ ) {
    vp.push_back(P(ms[ds[i]], mf[df[i]]));
  }
      
  SegmentTree t(n+q+1000, vp);

  for ( Int i = 0; i < q; i++ ) {
    cout << t.query(0, ms[fs[i]]+1, mf[fg[i]], mf[1LL << 61]) << endl;
    //cout << fs[i] << " " << fg[i] << endl;
    //cout << ms[fs[i]]+1 << " " << mf[fg[i]] << endl;
  }
  
  return 0;
}