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 <cmath>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
#include <sstream>
using namespace std;

typedef long long int64;
const int64 inf = (int64)1e18;

typedef vector<pair<int, int> > edge;
typedef vector<edge> graph;

struct SegTree {
	
	vector<vector<int64> > dat;
	int size;
	
	void init(int n, vector<int64> arg)
	{
		size = 1;
		while(size < n) size *= 2;
		
		dat.resize(size * 2);
		for(int i = 0; i < size; ++i)
			dat[i + size].push_back(i < arg.size() ? arg[i] : -inf);
		
		for(int i = size - 1; i > 0; --i) {
			int l = i * 2 + 0, r = i * 2 + 1;
			dat[i].resize(dat[l].size() + dat[r].size());
			merge(dat[l].begin(), dat[l].end(), dat[r].begin(), dat[r].end(), dat[i].begin());
		}
	}
	
	int find(int a, int b, int64 x, int k, int l, int r)
	{
		if(a >= r || b <= l)
			return 0;
		if(a <= l && b >= r)
			return dat[k].end() - lower_bound(dat[k].begin(), dat[k].end(), x);
		
		int mid = (l + r) / 2;
		int lv = find(a, b, x, k * 2 + 0, l, mid);
		int rv = find(a, b, x, k * 2 + 1, mid, r);
		
		return lv + rv;
	}
	
	int find(int a, int b, int64 x)
	{
		return find(a, b, x, 1, 0, size);
	}
};

void dijkstra(int s, graph &g, vector<pair<int64, int> > &dist)
{
	int n = g.size();
	
	dist.resize(n);
	for(int i = 0; i < n; ++i)
		dist[i] = make_pair(inf, i);
	
	priority_queue<pair<int64, int> > q;
	q.push(make_pair(0LL, s));
	
	while(!q.empty()) {
		
		int64 d = -q.top().first;
		int v = q.top().second;
		q.pop();
		if(dist[v].first < d)
			continue;
		
		dist[v].first = d;
		
		for(int i = 0; i < g[v].size(); ++i) {
			
			int w = g[v][i].first, cost = g[v][i].second;
			if(dist[w].first == inf)
				q.push(make_pair(-(d + cost), w));
		}
	}
}

int main()
{
	int n, m, q;
	graph g;
	
	scanf("%d%d", &n, &m);
	g.resize(n);
	for(int i = 0; i < m; ++i) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		g[a].push_back(make_pair(b, c));
		g[b].push_back(make_pair(a, c));
	}
	scanf("%d", &q);
	
	vector<pair<int64, int> > sdist, gdist;
	vector<int64> arg;
	
	dijkstra(0, g, sdist);
	dijkstra(n - 1, g, gdist);
	sort(sdist.begin(), sdist.end());
	
	arg.resize(n);
	for(int i = 0; i < n; ++i)
		arg[i] = gdist[sdist[i].second].first;
	
	SegTree tree;
	
	tree.init(n, arg);
	
	for(int query = 0; query < q; ++query) {
		
		int64 fs, fg;
		
		scanf("%lld%lld", &fs, &fg);
		
		int ub = upper_bound(sdist.begin(), sdist.end(), make_pair(fs, n)) - sdist.begin();
		
		int ans = tree.find(0, ub, fg);
		
		//printf("[debug] (%d, %d) -> ub:%d -> %d\n", fs, fg, ub, ans);
		
		printf("%d\n", ans);
	}
	
	return 0;
}

/* ハラスメントに負けず */
/* 0完太陽にも負けず */
/* はやく人権を獲得したい */
/* nullmineralが書きましたが */
/* 責任はまったくとりません */