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<stdio.h>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
int v[110000];
long long ijk[110000];
long long ijk2[110000];
int ans[110000];
vector<pair<int,int> > g[110000];
long long z[110000];
int bit[110000];
int n;
pair<long long,long long>pt[110000];
pair<long long,pair<int,long long > >ev[110000];
void add(int a,int b){
	for(;a<110000;a|=a+1)bit[a]+=b;
}
int sum(int a,int b){
	if(a>b)return 0;
	if(a)return sum(0,b)-sum(0,a-1);
	int ret=0;
	for(;b>=0;b=(b&(b+1))-1)ret+=bit[b];
	return ret;
}
int main(){
	int a,b;
	scanf("%d%d",&a,&b);
	n=a;
	for(int i=0;i<b;i++){
		int p,q,r;scanf("%d%d%d",&p,&q,&r);
		g[p].push_back(make_pair(q,r));
		g[q].push_back(make_pair(p,r));
	}
	for(int i=0;i<a;i++)ijk[i]=99999999999999LL;
	ijk[0]=0;
	priority_queue<pair<long long,int> > Q;
	Q.push(make_pair(0,0));
	while(Q.size()){
		long long cost=-Q.top().first;
		int at=Q.top().second;
		Q.pop();
		if(v[at])continue;
		v[at]=1;
		for(int i=0;i<g[at].size();i++){
			if(!v[g[at][i].first]&&ijk[g[at][i].first]>cost+g[at][i].second){
				ijk[g[at][i].first]=cost+g[at][i].second;
				Q.push(make_pair(-cost-g[at][i].second,g[at][i].first));
			}
		}
	}
	for(int i=0;i<a;i++)v[i]=0;
	for(int i=0;i<a;i++)ijk2[i]=999999999999999LL;
	ijk2[a-1]=0;
	Q.push(make_pair(0,a-1));
	while(Q.size()){
		long long cost=-Q.top().first;
		int at=Q.top().second;
		Q.pop();
		if(v[at])continue;
		v[at]=1;
		for(int i=0;i<g[at].size();i++){
			if(!v[g[at][i].first]&&ijk2[g[at][i].first]>cost+g[at][i].second){
				ijk2[g[at][i].first]=cost+g[at][i].second;
				Q.push(make_pair(-cost-g[at][i].second,g[at][i].first));
			}
		}
	}
	for(int i=0;i<a;i++){
		if(ijk[i]>10000000000LL||ijk2[i]>10000000000LL){
			while(1);
		}
	}
	for(int i=0;i<a;i++)z[i]=ijk2[i];
	std::sort(z,z+a);
	int c;scanf("%d",&c);
	for(int i=0;i<c;i++){
		long long p,q;scanf("%lld%lld",&p,&q);
		ev[i]=make_pair(p,make_pair(i,q));
	}
	std::sort(ev,ev+c);
	for(int i=0;i<a;i++)pt[i]=make_pair(ijk[i],ijk2[i]);
	std::sort(pt,pt+a);
	int at=0;
	for(int i=0;i<c;i++){
		while(at<a&&pt[at].first<=ev[i].first){
			add(lower_bound(z,z+a,pt[at].second)-z,1);
			at++;
		}
		if(ev[i].second.second>z[a-1])continue;
		ans[ev[i].second.first]=sum(lower_bound(z,z+a,ev[i].second.second)-z,a-1);
	}
	for(int i=0;i<c;i++)printf("%d\n",ans[i]);
}