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

#define PLUS (0)
#define COUNT (1)
#define MAX_N (100100)
#define MAX_Q (100100)

using namespace std;

typedef long long lint;
typedef pair<lint, lint> P;

struct Edge {
	int to, cost;
	Edge(int to, int cost) : to(to), cost(cost){}
	Edge(){}
};

struct Query {
	lint fs, fg, idx, type;
};

bool operator < (const Query &a, const Query &b)
{
	if (a.fs - b.fs) return (a.fs < b.fs);
	return (a.type < b.type);
}

vector<Edge> g[MAX_N];
Query query[2 * MAX_Q];

int bit[2 * MAX_N + 1];
lint ds[MAX_N], dg[MAX_N];
int res[MAX_Q];

void add(int pos, int val)
{
	while (pos <= 2 * MAX_N){
		bit[pos] += val;
		pos += pos & -pos;
	}
}

int sum(int pos)
{
	int ret = 0;
	while (pos){
		ret += bit[pos];
		pos &= (pos - 1);
	}
	return (ret);
}

void dijkstra(int s, lint *mem)
{
	bool done[MAX_N];
	
	memset(done, false, sizeof(done));
	mem[s] = 0;
	
	priority_queue<P, vector<P>, greater<P> > pq;
	
	for (pq.push(P(0, s)); pq.size(); pq.pop()){
		P x = pq.top();
		if (done[x.second]) continue;
		mem[x.second] = x.first;
		done[x.second] = true;
		
		for (int i = 0; i < g[x.second].size(); i++){
			Edge e = g[x.second][i];
			
			pq.push(P(g[x.second][i].cost + x.first, g[x.second][i].to));
		}
	}
}

int main()
{
	int n, m, q;
	vector<lint> vs;
	map<lint, int> conv;
	
	scanf("%d %d", &n, &m);
	
	for (int i = 0; i < m; i++){
		int from, to, cost;
		scanf("%d %d %d", &from, &to, &cost);
		g[from].push_back(Edge(to, cost));
		g[to].push_back(Edge(from, cost));
	}
	
	dijkstra(0, ds);
	dijkstra(n - 1, dg);
	
	scanf("%d", &q);
	for (int i = 0; i < q; i++){
		scanf("%lld %lld", &query[i].fs, &query[i].fg);
		query[i].idx = i;
		query[i].type = COUNT;
		vs.push_back(query[i].fg);
	}
	for (int i = 0; i < n; i++){
		query[i + q].fs = ds[i]; query[i + q].fg = dg[i];
		query[i + q].type = PLUS;
		vs.push_back(query[i + q].fg);
	}
	
	sort(vs.begin(), vs.end());
	int idx = 0;
	conv[vs[0]] = idx++;
	for (int i = 1; i < q + n; i++){
		if (vs[i] != vs[i - 1]) conv[vs[i]] = idx++;
	}
	
	sort(query, query + q + n);
	
	for (int i = 0; i < q + n; i++){
		query[i].fg = conv[query[i].fg];
	}
	
	for (int i = 0; i < q + n; i++){
		if (query[i].type == PLUS) add((int)query[i].fg + 1, 1);
		else res[query[i].idx] = sum(2 * MAX_N) - sum((int)query[i].fg);
	}
	
	for (int i = 0; i < q; i++) printf("%d\n", res[i]);
	
	return (0);
}