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"
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define vi vector<int>
#define pb push_back
#define INF (1LL<<59)
#define int ll

//verified AOJ GRL_1
#define MAX_V 100000
struct edge{int to,cost;};
vector<edge> G[MAX_V];

void dijkstra(int s, vector<ll> &d){
	priority_queue< pii,vector<pii>,greater<pii> > que;
	//    rep( i,d.size() )d[i]=INF;
	d[s] = 0;
	que.push( pii(0,s) );
	
	while( que.size() ){
		pii p=que.top();
		que.pop();
		
		int v=p.second;
		if(d[v]<p.first)continue;
		
		rep(i,G[v].size()){
			edge e=G[v][i];
			if(d[e.to]>d[v]+e.cost){
				d[e.to]=d[v]+e.cost;
				que.push(pii(d[e.to],e.to));
			}
		}
	}
}


template<class T>
class segtree{
private:
	vector<T> dat;
	int _size;  //the number of leaves
	T _init;
	
public:
	segtree(int __size=0 , T init=T() ):_size(__size) , _init(init) , dat(4*__size,init){
		if(__size==0)return ;
		int x=1;
		while(x<_size)x*=2;
		_size = x;
	}
	
	
	void init(int m,int x){
		int i = m+_size;
		dat[i].pb(x);
	}
	
	void refresh(){
		for(int i=_size-1;i>0;i--){
			T tmp = dat[2*i];
			tmp.insert( tmp.end(),all(dat[2*i+1]) );
			dat[i] = tmp;
		}
	}
 
	
	int find(int s,int t,int p,int num=1,int a=0,int b=-1){
		if(b==-1)b=_size-1; //I couldn't "int b=_size".
		if(b<s||t<a)return 0;
		if(s<=a&&b<=t)return dat[num].size()-(lower_bound(all(dat[num]),p)-dat[num].begin());
		int  ret,res;
		ret = find(s,t,p,num*2,a,(a+b)/2);
		res = find(s,t,p,num*2+1,(a+b)/2+1,b);
		
		return ret+res;
	}
	
	void sortAll(){
		rep(i,_size*2-1){
			sort(all(dat[i]));
		}
	}
};


signed main(){
	set<ll> valid_number;
	int n,m;
	cin>>n>>m;
	rep(i,m) {
		int a,b,c;
		scanf("%lld%lld%lld",&a,&b,&c);
		
		G[a].pb(edge{b,c});
		G[b].pb(edge{a,c});
	}
	
	vector<ll> ds(n,INF), dg(n,INF);
	dijkstra(0,ds);
	dijkstra(n-1,dg);
	
	rep(i,ds.size()) valid_number.insert(ds[i]);
	
	
	int q;
	cin>>q;
	vector<pair<ll,ll>> query;
	rep(i,q){
		ll a,b;
		scanf("%lld%lld",&a,&b);
		valid_number.insert(a);
		query.pb(pair<ll,ll>(a,b));
	}
	
	
	map<ll,int> conv;
	int c=0;
	for(auto & num:valid_number){
		conv[num] = c++;
	}
	
	
	segtree<vi> st( valid_number.size() );
	rep(i,n){
		st.init(conv[ds[i]],dg[i]);
	}
	st.refresh();
	st.sortAll();
	
	rep(i,q){
		printf( "%lld\n",st.find(0,conv[query[i].first],query[i].second));
	}
}