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 <vector>
#include <queue>
#include <utility>
#include <algorithm>
#define llint long long
#define inf (1LL<<62)

using namespace std;
typedef pair<llint, llint> P;

struct edge{
	llint to, cost;
	edge(){}
	edge(llint a, llint b){
		to = a, cost = b;
	}
};

llint n, m, q;
vector<edge> G[100005];
llint distS[100005], distG[100005];
vector<llint> dvec;
vector<llint> compS, compG;
vector<llint> point[100005];
pair<P, llint> query[100005];
vector<P> qpoint[100005];
llint ans[100005];

llint bit[100005];
llint sum(llint i)
{
	i++;
	llint ret = 0;
	while(i > 0){
		ret += bit[i];
		i -= i & (-i);
	}
	return ret;
}
void add(llint i, llint x)
{
	i++;
	while(i <= n){
		bit[i] += x;
		i += i & (-i);
	}
}

llint get(llint x)
{
	return upper_bound(dvec.begin(), dvec.end(), x) - dvec.begin();
}

void dijkstra(llint S, llint dist[])
{
	for(int i = 0; i < n; i++) dist[i] = inf;
	dist[S] = 0;
	priority_queue< P, vector<P>, greater<P> > Q;
	Q.push(make_pair(0, S));
	
	llint d, v;
	while(Q.size()){
		d = Q.top().first;
		v = Q.top().second;
		Q.pop();
		if(dist[v] < d) continue;
		for(int i = 0; i < G[v].size(); i++){
			if(dist[G[v][i].to] > d + G[v][i].cost){
				dist[G[v][i].to] = d + G[v][i].cost;
				Q.push(make_pair(dist[G[v][i].to], G[v][i].to));
			}
		}
	}
}

int main(void)
{
	cin >> n >> m;
	llint a, b, c;
	for(int i = 0; i < m; i++){
		cin >> a >> b >> c;
		G[a].push_back(edge(b, c));
		G[b].push_back(edge(a, c));
	}
	
	dijkstra(0, distS);
	dijkstra(n-1, distG);
	
	for(int i = 0; i < n; i++) dvec.push_back(distS[i]);
	sort(dvec.begin(), dvec.end());
	
	
	for(int i = 0; i < n; i++) compS.push_back(distS[i]);
	sort(compS.begin(), compS.end());
	compS.erase(unique(compS.begin(), compS.end()), compS.end());
	for(int i = 0; i < n; i++){
		distS[i] = lower_bound(compS.begin(), compS.end(), distS[i]) - compS.begin();
	}
	
	for(int i = 0; i < n; i++) compG.push_back(distG[i]);
	sort(compG.begin(), compG.end());
	compG.erase(unique(compG.begin(), compG.end()), compG.end());
	for(int i = 0; i < n; i++){
		distG[i] = lower_bound(compG.begin(), compG.end(), distG[i]) - compG.begin();
	}
	
	for(int i = 0; i < n; i++) point[distG[i]].push_back(distS[i]);
	
	
	llint q;
	cin >> q;
	for(int i = 0; i < q; i++){
		cin >> query[i].first.first >> query[i].first.second;
		query[i].first.second--;
		query[i].second = i;
	}
	for(int i = 0; i < q; i++){
		query[i].first.first = upper_bound(compS.begin(), compS.end(), query[i].first.first) - 1 - compS.begin();
		query[i].first.second = upper_bound(compG.begin(), compG.end(), query[i].first.second) - 1 - compG.begin();
	}
	
	for(int i = 0; i < q; i++){
		if(query[i].first.first >= 0){
			qpoint[query[i].first.second].push_back(make_pair(query[i].first.first, query[i].second));
		}
	}
	
	for(int i = 0; i < q; i++){
		if(query[i].first.second < 0){
			ans[query[i].second] = get(compS[query[i].first.first]);
		}
	}
	for(int i = 0; i <= compG.size(); i++){
		for(int j = 0; j < point[i].size(); j++) add(point[i][j], 1);
		for(int j = 0; j < qpoint[i].size(); j++){
			ans[qpoint[i][j].second] = get(compS[query[qpoint[i][j].second].first.first]) - sum(qpoint[i][j].first);
		}
	}
	for(int i = 0; i < q; i++) cout << ans[i] << endl;
	
	return 0;
}