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>
#define rep(i,n) for(int i=0;i<n;++i)
using namespace std;
using ll=long long;

const ll inf=1LL<<62;
const int vmax=100010;
struct node{int to;ll cost;};
vector<node> graph[vmax];
ll sdist[vmax],tdist[vmax];


void dijkstra(int n,ll* dist,int s){
	fill(dist,dist+n,inf);
	using state=tuple<ll,int>;
	priority_queue<state,vector<state>,greater<state>> q;
	dist[s]=0;q.push(state(0LL,s));
	while(!q.empty()){
		ll cost,v; tie(cost,v)=q.top();q.pop();
		if(dist[v]<cost) continue;
		for(auto &e:graph[v]){
			int nv=e.to;
			ll ncost=cost+e.cost;
			if(dist[nv]>ncost){
				dist[nv]=ncost;
				q.push(state(ncost,nv));
			}
		}
	}
	return;
}

vector<ll> sd,td;

const int qn=1<<17;
vector<ll> data[1<<18];

void init(int n){
	rep(i,n){
		int sidx=lower_bound(sd.begin(),sd.end(),sdist[i])-sd.begin();
		int tidx=lower_bound(td.begin(),td.end(),tdist[i])-td.begin();
		data[sidx+qn-1].emplace_back(tidx);
		sort(data[sidx+qn-1].begin(),data[sidx+qn-1].end());
	}
	for(int i=qn-2;i>=0;--i){
		int left=2*i+1,right=2*i+2;
		copy(data[left].begin(),data[left].end(),back_inserter(data[i]));
		copy(data[right].begin(),data[right].end(),back_inserter(data[i]));
		sort(data[i].begin(),data[i].end());	
	}
	return;
}

ll query(int a,int b,int k,int l,int r,int x){
	if(b<=l||r<=a) return 0LL;
	if(a<=l&&r<=b){
		ll ret=data[k].end()-lower_bound(data[k].begin(),data[k].end(),x);
		return ret;
	}
	int m=(l+r)/2;
	return query(a,b,2*k+1,l,m,x)+query(a,b,2*k+2,m,r,x);
}

int main(void){
	int n,m;
	cin >> n >> m;
	rep(i,m){
		int a,b,c;
		cin >> a >> b >> c;
		graph[a].push_back({b,c});
		graph[b].push_back({a,c});
	}
	dijkstra(n,sdist,0);
	dijkstra(n,tdist,n-1);

	rep(i,n) sd.emplace_back(sdist[i]),td.emplace_back(tdist[i]);
	sort(sd.begin(),sd.end()),sort(td.begin(),td.end());

	sd.erase(unique(sd.begin(),sd.end()),sd.end());
	td.erase(unique(td.begin(),td.end()),td.end());

	init(n);
	int q;
	cin >> q;
	rep(loop,q){
		ll fs,fg;
		cin >> fs >> fg;
		int sidx=upper_bound(sd.begin(),sd.end(),fs)-sd.begin();
		int tidx=lower_bound(td.begin(),td.end(),fg)-td.begin();
		cout << query(0,sidx,0,0,qn,tidx) << endl;
	}
	return 0;
}