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<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<map>
//#include<>
using namespace std;

typedef long long ll;

typedef pair<ll,ll> pll;
typedef pair<ll,ll> Node;

#define rep(i,j) for(i=0;i<j;i++)
#define rev_rep(i,j) for(i=j;i>=0;i--)

ll INF=1LL<<62LL;
class Dijkstra
{
public:
  typedef struct Edge{ll to,cost;}Edge;
  vector< vector<Edge> > Graph;//接続リスト

  Dijkstra(ll n)
  {
    vector<Edge> temp;
    Graph.resize(n,temp);
  }

  void add_arc(ll x,ll y,ll c)
  {
    Edge e={y,c};
    Graph[x].push_back(e);
  }

  vector<ll> get_distance(ll s)
  {
    priority_queue< Node ,vector<Node>,greater<Node> > que;//(sからの距離,インデックス)
    vector<ll>ans;
    ll i;
    ans.resize(Graph.size(),INF);
    ans[s]=0;
    que.push(Node(0,s));
    while(!que.empty()){
      Node now=que.top();que.pop();
      ll now_i=now.second;
      //nowへの最小距離でないならスキップ
      if(ans[now_i]<now.first)continue;
      //nowからの辺全てをみる
      rep(i,Graph[now_i].size()){
	Edge e=Graph[now_i][i];
	//nowから行った方が近かったら更新
	if(ans[e.to]>ans[now_i]+e.cost){
	  ans[e.to]=ans[now_i]+e.cost;
	  que.push(Node(ans[e.to],e.to));
	}
      }
    }
    return ans;
  }

};

class BIT
{
public:
  vector<ll> bit;

  BIT(ll n)
  {
    bit.resize(n+1,0);
  }
  ll sum(ll i)
  {
    ll s=0;
    while(i>0){
      s+=bit[i];
      i-=i& (-i);//i& -i はiの最小1bit  <- 2^kを引く
    }
    return s;
  }

  void add(ll i,ll x){
    while(i<=bit.size()){
      bit[i]+=x;
      i+=i& (-i);
    }
  }
};

typedef struct Query{
  ll fs,fe;ll id;
  Query(ll fs_,ll fe_,ll id_):fs(fs_),fe(fe_),id(id_){};
  bool operator<(const Query& right)const{
    return fs!=right.fs?fs<right.fs:fe>right.fe; 
  }
}Query;

int main()
{
  ll n,m,i;
  cin>>n>>m;
  Dijkstra G(n);
  ll q_num;
  vector<Query> query;
  //input
  {
    rep(i,m){
      ll a,b,c;
      cin>>a>>b>>c;
      G.add_arc(a,b,c);
      G.add_arc(b,a,c);
    }
    cin>>q_num;
    rep(i,q_num){
      ll fs,fe;
      cin>>fs>>fe;
      query.push_back(Query(fs,fe,i));
    }
  }
  
  vector<ll> d_s=G.get_distance(0),d_e=G.get_distance(n-1);

  //d_eを座標圧縮
  map<ll,ll> zip;
  {
    vector<ll>temp;
    temp.resize(d_e.size(),0);
    
    copy(d_e.begin(),d_e.end(),temp.begin());
    rep(i,query.size())
      temp.push_back(query[i].fe);


    sort(temp.begin(),temp.end(),greater<ll>() );
    temp.erase(unique(temp.begin(),temp.end()),temp.end());

    rep(i,temp.size())
      zip[temp[i]]=i+1;
    rep(i,d_e.size())
      d_e[i]=zip[d_e[i]];
  }

  vector<pll> distance;
  rep(i,n)
    distance.push_back(pll(d_s[i],d_e[i]));
  sort(distance.begin(),distance.end());

  BIT bit(zip.size());
  vector< pll >::iterator itr=distance.begin();
  vector<ll> ans;
  ans.resize(query.size(),0);
  
  sort(query.begin(),query.end());
  rep(i,query.size())
    {
      while(itr!=distance.end() && itr->first<=query[i].fs){
	bit.add(itr++->second,1);
      }
      ans[query[i].id]=bit.sum(zip[query[i].fe]);
    }

  rep(i,ans.size())
    cout<<ans[i]<<endl;
  
  return 0;
}