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:
#ifndef ___Class_RangeTree
#define ___Class_RangeTree

// ------ Includes ------ //
#include <limits>
#include <vector>
#include <algorithm>

template<typename Type> class RangeTree {
private:
	int size_; std::vector<std::vector<Type> > dat;
	inline int query_(int a, int b, Type x, int k, int l, int r) {
		// Find range [a, b) : value < x : Now k-th node and the range is [l, r)
		if (r <= a || b <= l) return 0;
		if (a <= l && r <= b) return lower_bound(dat[k].begin(), dat[k].end(), x) - dat[k].begin();
		int lc = query_(a, b, x, (k << 1), l, (l + r) >> 1);
		int rc = query_(a, b, x, (k << 1) + 1, (l + r) >> 1, r);
		return lc + rc;
	}
public:
	RangeTree() : size_(0), dat(std::vector<Type>()) {};
	template<class T>
	RangeTree(T begin_, T end_) {
		int n = (end_ - begin_);
		for (size_ = 1; size_ < n; size_ <<= 1); dat.resize(size_ << 1, std::vector<Type>());
		for (int i = 0; i < n; i++) dat[i + size_] = std::vector<Type>({ *(begin_ + i) });
		for (int i = size_ - 1; i > 0; i--) {
			dat[i].resize(dat[i << 1].size() + dat[(i << 1) + 1].size());
			std::merge(dat[i << 1].begin(), dat[i << 1].end(), dat[(i << 1) + 1].begin(), dat[(i << 1) + 1].end(), dat[i].begin());
		}
	}
	inline int size() { return size_; }
	inline int query(int l, int r, Type x) {
		// Find range [a, b) : value < x
		return query_(l, r, x, 1, 0, size_);
	}
};

#endif

#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
typedef long long ll;
const ll inf = (1LL << 61);
struct edge { int to; ll cost; };
const bool operator<(const edge& e1, const edge& e2) { return e1.cost < e2.cost; }
int N, M, Q, a, b, c; ll z1, z2; vector<edge> G[100009];
vector<ll> dist(int s) {
	vector<ll> ret(N, inf); ret[s] = 0;
	priority_queue<edge> que; que.push(edge{ s, 0 });
	while (!que.empty()) {
		edge u = que.top(); que.pop();
		for (edge e : G[u.to]) {
			if (ret[e.to] > ret[u.to] + e.cost) {
				ret[e.to] = ret[u.to] + e.cost;
				que.push(edge{ e.to, -ret[e.to] });
			}
		}
	}
	return ret;
}
int main() {
	ios::sync_with_stdio(false);
	scanf("%d%d", &N, &M);
	for (int i = 0; i < M; i++) {
		scanf("%d%d%d", &a, &b, &c);
		G[a].push_back(edge{ b, c });
		G[b].push_back(edge{ a, c });
	}
	vector<ll> res1 = dist(0);
	vector<ll> res2 = dist(N - 1);
	vector<pair<ll, ll> > x0(N);
	for (int i = 0; i < N; i++) x0[i] = make_pair(res2[i], res1[i]);
	sort(x0.begin(), x0.end());
	vector<ll> x1(N), x2(N);
	for (int i = 0; i < N; i++) x1[i] = x0[i].second, x2[i] = x0[i].first;
	RangeTree<ll> q(x1.begin(), x1.end());
	scanf("%d", &Q);
	while (Q--) {
		scanf("%lld%lld", &z1, &z2);
		int ptr = lower_bound(x2.begin(), x2.end(), z2) - x2.begin();
		printf("%d\n", q.query(ptr, N, z1 + 1));
	}
	return 0;
}