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<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<functional>
#include<map>

using namespace std;
typedef long long ll;
typedef pair<ll,int> P;
typedef pair<ll,ll> P2;
typedef pair<P2,int> PP;
ll INF=1000000000000000000ll;

struct edge{
	int t;
	ll c;
};

int n,m,q;
vector<edge> G[100001];
P2 room[100001];
PP query[100001];
ll sd[100001],gd[100001];
ll ggd[100001];
int ans[400000];
int bit[400000];

void add(int k,int a){
	k++;
	while(k<=200000){
		bit[k]+=a;
		k+=k&-k;
	}
}

int sum(int k){
	int res=0;
	while(k>0){
		res+=bit[k];
		k-= k& -k;
	}
	return res;
}

void dijk(){
	for(int i=0;i<n;i++)sd[i]=INF;;
	priority_queue<P,vector<P>,greater<P> > que;
	que.push(P(0,0));
	while(que.size()){
		P p=que.top();
		que.pop();
		int v=p.second;
		ll c=p.first;
		if(sd[v]<c)continue;
		sd[v]=c;
		for(int i=0;i<G[v].size();i++){
			edge e=G[v][i];
			if(sd[e.t]>c+e.c){
				sd[e.t]=c+e.c;
				que.push(P(c+e.c,e.t));
			}
		}
	} 
}

void dijk2(){
	for(int i=0;i<n;i++)gd[i]=INF;
	priority_queue<P,vector<P>,greater<P> > que;
	que.push(P(0,n-1));
	while(que.size()){
		P p=que.top();
		que.pop();
		int v=p.second;
		ll c=p.first;
		if(gd[v]<c)continue;
		gd[v]=c;
		for(int i=0;i<G[v].size();i++){
			edge e=G[v][i];
			if(gd[e.t]>c+e.c){
				gd[e.t]=c+e.c;
				que.push(P(c+e.c,e.t));
			}
		}
	} 
}


int main(void){
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++){
		int f,t;
		ll c;
		scanf("%d%d%lld",&f,&t,&c);
		edge e;
		e.c=c;
		e.t=t;
		G[f].push_back(e);
		e.t=f;
		G[t].push_back(e);
	}
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		ll s,g;
		scanf("%lld%lld",&s,&g);
		query[i]=PP(P2(s,g),i);
	}
	dijk();
	dijk2();
	for(int i=0;i<n;i++)room[i]=P2(sd[i],gd[i]),ggd[i]=gd[i];
	sort(ggd,ggd+n);
	int gsize=unique(ggd,ggd+n)-ggd;
	for(int i=0;i<n;i++)room[i].second=lower_bound(ggd,ggd+gsize,room[i].second)-ggd;
	sort(query,query+q);
	sort(room,room+n);
	int j=0;
	for(int i=0;i<q;i++){
		while(j<n && room[j].first<=query[i].first.first){
			add(room[j].second,1);
			++j;
		}
		int p=lower_bound(ggd,ggd+gsize,query[i].first.second)-ggd;
		ans[query[i].second]=j-sum(p);
	}
	for(int i=0;i<q;i++)printf("%d\n",ans[i]);
	return 0;
}