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 <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
#include <cmath>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1000000000000000000ll;

void sssp(int s, const vector< vector<pll> > &conn, vector<ll> &result){
	int n = conn.size();
	result = vector<ll>(n, INF);
	priority_queue< pll, vector<pll>, greater<pll> > pq;
	result[s] = 0;
	pq.push(pll(0, s));
	while(!pq.empty()){
		pll p = pq.top();
		pq.pop();
		ll c = p.first;
		int v = p.second;
		if(c > result[v]){ continue; }
		for(int i = 0; i < conn[v].size(); ++i){
			int nv = conn[v][i].first;
			ll nc = c + conn[v][i].second;
			if(nc < result[nv]){
				result[nv] = nc;
				pq.push(pll(nc, nv));
			}
		}
	}
}

int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	vector< vector<pll> > conn(n);
	while(m--){
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		conn[a].push_back(pll(b, c));
		conn[b].push_back(pll(a, c));
	}
	vector<ll> ds, dg, dsl, dgl;
	sssp(0, conn, ds);
	sssp(n - 1, conn, dg);
	dsl = ds;
	sort(dsl.begin(), dsl.end());
	dsl.erase(unique(dsl.begin(), dsl.end()), dsl.end());
	dgl = dg;
	sort(dgl.begin(), dgl.end());
	dgl.erase(unique(dgl.begin(), dgl.end()), dgl.end());
	int sqds = static_cast<int>(ceil(sqrt(dsl.size() + 1)));
	int sqdg = static_cast<int>(ceil(sqrt(dgl.size() + 1)));
	vector< vector<int> > large_buckets(sqds, vector<int>(sqdg));
	vector< vector< vector<pii> > >
		inner_buckets(sqds, vector< vector<pii> >(sqdg));
	for(int i = 0; i < n; ++i){
		ds[i] = lower_bound(dsl.begin(), dsl.end(), ds[i]) - dsl.begin();
		dg[i] = lower_bound(dgl.begin(), dgl.end(), dg[i]) - dgl.begin();
		int ls = ds[i] / sqds, lg = dg[i] / sqdg;
		++large_buckets[ls][lg];
		inner_buckets[ls][lg].push_back(pii(ds[i], dg[i]));
	}
	int q;
	scanf("%d", &q);
	while(q--){
		ll fs, fg;
		scanf("%lld%lld", &fs, &fg);
		int s = upper_bound(dsl.begin(), dsl.end(), fs) - dsl.begin();
		int g = lower_bound(dgl.begin(), dgl.end(), fg) - dgl.begin();
		int answer = 0;
		int libegin = 0, liend = s / sqds;
		int ljbegin = (g + sqdg - 1) / sqdg, ljend = sqdg;
		for(int i = libegin; i < liend; ++i){
			for(int j = ljbegin; j < ljend; ++j){
				answer += large_buckets[i][j];
			}
		}
		if(g % sqdg != 0){
			for(int i = libegin; i < liend; ++i){
				const vector<pii> &v = inner_buckets[i][ljbegin - 1];
				for(int j = 0; j < v.size(); ++j){
					if(v[j].first < s && v[j].second >= g){ ++answer; }
				}
			}
		}
		if(s % sqds != 0){
			for(int i = g % sqdg ? ljbegin - 1 : ljbegin; i < ljend; ++i){
				const vector<pii> &v = inner_buckets[liend][i];
				for(int j = 0; j < v.size(); ++j){
					if(v[j].first < s && v[j].second >= g){ ++answer; }
				}
			}
		}
		cout << answer << endl;
	}
	return 0;
}