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;

struct aa {
	int now;
	long long int time;
};
class Compare {
public:
	bool operator()(const aa&l, const aa&r) {
		return l.time > r.time;
	}
};
vector<long long int>djk(const vector<vector<pair<int, int>>>&es,const int start) {
	vector<long long int>memo(es.size(), 1ll << 55);
	memo[start] = 0;
	priority_queue<aa, vector<aa>, Compare>que;
	que.push(aa{start,0 });
	while (!que.empty()) {
		aa atop(que.top());
		que.pop();
		for (auto e : es[atop.now]) {
			const int next = e.first;
			const long long int nexttime = atop.time + e.second;
			if (memo[next] > nexttime) {
				memo[next] = nexttime;
				que.push(aa{ next,nexttime });
			}
		}
	}
	return memo;
}

vector<pair<int, long long int>>start_times, goal_times;

//???????????¨???????????????????§£??????????????¢?????´?????????????¬??????????????????¨????????????????????????
//
//    ????´??????´??°????????????
//    ?????¨????????????????????????
//    ?????? [L,R] ??????????????? [L-1, R], [L+1, R], [L, R-1], [L, R+1] ?????????????????????????????????
//	????????????????????¬??????http://pekempey.hatenablog.com/entry/2016/01/23/185143
//
//Mo_Algo???????????????????§???????
// id : ?????????????????¨??????
// l  : ?????? [
// r  : ?????? )
// ????????????????????????????????£????????????????????????????????????
struct Mo_Query {
	int id;

	//[l,r)
	long long int l;
	long long int r;

	Mo_Query(const int id_, const long long int l_, const long long int r_) :
		id(id_), l(l_), r(r_) {
	}

};

//Mo's Algorighm
//

int N;
class Mo_Algo {
public:
	const int size;
	vector<vector<Mo_Query>>squares;

	vector<int>oks;
	int ans = 0;
  ////example
  //long long int nans = 0;
	//vector<int>sums;

	Mo_Algo(int size_) :size(size_), squares(static_cast<size_t>(sqrt(size - 1) + 1)),oks(size)
	{
		oks.resize(N);
	}


	//v?????????????´????(????????????)?????????
	//??????????????????????????±?????????????????´?????????to_right???????????????????????????
	void add(const  int v, const bool to_right = false) {

		//?????????????£??????????
		if (oks[v]==1)ans++;
		oks[v]++;
	}

	//v?????????????´????(???????????????)????????????
	//??????????????????????????±?????????????????´?????????to_right???????????????????????????
	void rem(const  int v, const bool to_right = false) {

		//?????????????£??????????
		oks[v]--;
		if (oks[v]==1)ans--;
	}

	int getans() {

		//?????????????£??????????
		return ans;
	}

	//[l,r)
	//0????????????
	vector<long long int > solve(vector<Mo_Query> &querys) {
		sort(querys.begin(), querys.end(), [](const Mo_Query&l, const Mo_Query&r) {
			return l.l < r.l;
		});
		for (int i = 0; i < querys.size(); ++i) {

			squares[i / (squares.size())].push_back(querys[i]);
		}
		for (auto& asquare : squares) {
			sort(asquare.begin(), asquare.end(), [](const  Mo_Query &l, const  Mo_Query &r) {
				return l.r < r.r;
			});
		}

		vector<long long int >anss(querys.size());

		int nl = 0;
		int nr = 0;
		for (const auto square : squares) {
			for (const auto q : square) {
				const long long int ql(q.l);
				const long long int qr(q.r);
				while (nr!=N&&goal_times[nr].second >= qr) {
					add(goal_times[nr].first);
					nr++;
				}
				while (nl!= N&&start_times[nl].second <= ql) {
					add(start_times[nl].first);
					nl++;
				}
				while (nr&&goal_times[nr-1].second < qr) {
					nr--;
					rem(goal_times[nr].first);
				}
				while (nl&&start_times[nl - 1].second > ql) {
					nl--;
					rem(start_times[nl].first);
				}

				anss[q.id] = getans();
			}
		}
		return anss;
	}
};

int main() { 
	{
		int M;
		cin >> N >> M;
		vector<vector<pair<int, int>>>edges(N);
		for (int i = 0; i < M; ++i) {
			int a, b, c; cin >> a >> b >> c;
			edges[a].emplace_back(b, c);
			edges[b].emplace_back(a, c);
		}
		{

			vector<long long int>from_s, from_g;
			from_s = djk(edges, 0);
			from_g = djk(edges, N - 1);
			for (int i = 0; i < N; ++i) {
				start_times.emplace_back(i, from_s[i]);
				goal_times.emplace_back(i, from_g[i]);
			}

		}
		sort(start_times.begin(), start_times.end(), [](const pair<int, long long int>&l, const pair<int, long long int>&r) {
			return l.second < r.second;
		});
		sort(goal_times.begin(), goal_times.end(), [](const pair<int, long long int>&l, const pair<int, long long int>&r) {
			return l.second > r.second;
		});
	}
	

	int Q; cin >> Q;
		vector<Mo_Query>querys;
	for (int i = 0; i < Q;++i){
		long long int fs, fg; cin >> fs >> fg;
		querys.push_back(Mo_Query(i, fs, fg));
	}
	 Mo_Algo al(Q);
	 auto ans = al.solve(querys);
	 for (int i = 0; i < ans.size(); ++i) {
		 cout << ans[i] << endl;
	 }
	return 0;
}