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 <bits/stdc++.h>
using namespace std;
typedef long long ll;

typedef long long W;

const W INF = 1LL << 50;

struct edge {
	int to;
	W cost;
};
typedef pair<W, int> P;
typedef vector<vector <edge > > Graph;

vector<W> dijkstra(int s, const Graph& G) {
	vector<W> d(G.size());
	priority_queue< P, vector<P>, greater<P> > que;
	fill(d.begin(), d.end(), INF);
	d[s] = 0;
	que.push(P(0, s));
	while(!que.empty()) {
		P p = que.top();
		que.pop();
		int v = p.second;
		if(d[v] < p.first) continue;
		for(int i = 0; i < G[v].size(); i++) {
			edge e = G[v][i];
			if(d[e.to] > d[v] + e.cost) {
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
	return d;
}

template <class T>
struct BIT {
	int n;
	vector<T> x;
	BIT(int n_ = 0) : n(n_), x(n + 1, 0) {}
	int size() const { return n; }
	T sum(int r) const {
		T S = 0;
		for(r = r - 1; r >= 0; r = (r & (r + 1)) - 1) S += x[r];
		return S;
	}
	T sum(int l, int r) const { return sum(r) - sum(l); }
	void add(int k, const T &a) {
		for(; k < n; k |= k + 1) x[k] += a;
	}
	void set(int k, const T &a) { add(k, a - sum(k, k + 1)); }
	int lower_bound(T x) {
		int ng = -1, ok = n - 1;
		while(abs(ng - ok) > 1) {
			int mid = (ng + ok) / 2;
			if(sum(0, mid + 1) >= x) ok = mid;
			else ng = mid;
		}
		return ok;
	}
};

struct Q {
	ll fs, fg;
	int id;
	bool operator <(const Q& x) const {
		return fs < x.fs;
	}
};

typedef pair<ll, ll> Pll;

int ans[100000];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, m;
	cin >> n >> m;

	Graph G(n);
	for(int i = 0; i < m; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		G[a].push_back({ b, c });
		G[b].push_back({ a, c });
	}

	vector<W> ds = dijkstra(0, G);
	vector<W> dg = dijkstra(n - 1, G);
	vector<Pll> dd(n);
	map<W, int> dg_map;
	for(int i = 0; i < dg.size(); i++) {
		dg_map[dg[i]] = -1;
		dd[i] = { ds[i], dg[i] };
	}
	sort(dd.begin(), dd.end());
	int cnt = 0;
	for(auto v : dg_map) {
		dg_map[v.first] = cnt++;
	}

	int qn;
	cin >> qn;
	vector<Q> qs(qn);
	for(int i = 0; i < qn; i++) {
		cin >> qs[i].fs >> qs[i].fg;
		qs[i].id = i;
	}
	sort(qs.begin(), qs.end());

	BIT<ll> b(cnt);
	int di = 0;
	for(auto q : qs) {
		while(di < n && dd[di].first <= q.fs) {
			int idx = dg_map[dd[di].second];
			b.add(idx, 1);
			di++;
		}
		auto it = dg_map.lower_bound(q.fg);
		if(it == dg_map.end()) continue;
		ans[q.id] = b.sum(it->second, cnt);
	}

	for(int i = 0; i < qn; i++) {
		cout << ans[i] << endl;
	}
}