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<queue>
#include<functional>
#include<vector>
#include<algorithm>
using namespace std;
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> Q;
long long d[200000], n, m, q, a, b, c, f[200000], g[200000]; vector<pair<long long, long long>> x[200000];
pair<long long, long long> F[200000]; vector<long long>G;
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_);
	}
};

void dijkstra(long long s) {
	for (long long i = 0; i < 200000; i++) { d[i] = (long long)((1ULL << 63) - 1); }
	d[s] = 0; Q.push(make_pair(0, s));
	while (!Q.empty()) {
		pair<long long,long long>p = Q.top();
		Q.pop();
		for (long long i = 0; i < x[p.second].size(); i++) {
			long long a = x[p.second][i].first;
			long long b = x[p.second][i].second;
			if (d[a] > d[p.second] + b) {
				d[a] = d[p.second] + b;
				Q.push(make_pair(d[a], a));
			}
		}
	}
}
int main() {
	cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a >> b >> c; x[a].push_back(make_pair(b, c)); x[b].push_back(make_pair(a, c)); }
	dijkstra(0); for (int i = 0; i < 200000; i++)f[i] = d[i];
	dijkstra(n - 1); for (int i = 0; i < 200000; i++)g[i] = d[i];
	for (int i = 0; i < n; i++)F[i] = make_pair(f[i], g[i]); sort(F, F + n);
	for (int i = 0; i < n; i++)G.push_back(F[i].second);
	cin >> q;
	RangeTree<long long> S(G.begin(), G.end());
	for (int i = 0; i < q; i++) {
		cin >> a >> b;
		long long pos1 = lower_bound(F, F + n, make_pair(a + 1, 0LL)) - F;
		long long p1 = S.query(0, pos1, 9223372036854775807LL);
		long long p2 = S.query(0, pos1, b);
		cout << p1 - p2 << endl;
	}
	return 0;
}