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<vector>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cassert>
#include<tuple>
#include<unordered_map>

using namespace std;
#define int ll

#define REP(i, n) for(int i=0;i<(int)n;i++)
#define rep(i, n) for(int i=0;i<(int)n;i++)
#define reep(i, s, t) for(int i=s;i<(int)t;i++)
#define irep(i, n) for(int i=(int)n-1;i>=0;i--)
#define ireep(i, n, s) for(int i=(int)n-1;i>=s;i--)
#define all(v) v.begin(), v.end()
#define vc vector
#define eb emplace_back
#define pb push_back

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, ll> pil;
typedef vector<ll> vi;
const ll INF = (1ll<<60) + 10;

struct BIT{
	int n;
	vi vs;
	
	BIT(int size=0):n(size), vs(n){}
	void add(int k, int a){
		for(int i=k;i<n;i |= i+1) vs[i] += a;
	}
	int sum(int s, int t){
		if(s > 0) return sum(0, t) - sum(0, s);
		int res = 0;
		for(int i=t-1;i>=0;i=(i&(i+1))-1) res += vs[i];
		return res;
	}
};

template<typename T=int>
struct Zipper{
	vector<T> dec;
	unordered_map<T, int> enc;
	Zipper(){}
	void add(T t){dec.pb(t);}
	size_t size(){return dec.size();}
	void compile(){
		sort(all(dec)); dec.erase(unique(all(dec)), dec.end());
		REP(i, dec.size()) enc[dec[i]] = i;
	}
	T operator[](int i){return dec[i];}
	T operator()(T i){
		assert(enc.find(i) != enc.end());
		return enc[i];
	}
	int lb(T t){return lower_bound(all(dec), t) - dec.begin();}
};


int n, m;

vector<ll> ijk(const vector<vector<pil>> &g, int s){
	vector<ll> d(g.size(), INF);
	d[s] = 0;
	priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
	pq.emplace(0, s);
	while(!pq.empty()){
		int u; ll c; tie(c, u) = pq.top(); pq.pop();
		if(d[u] < c) continue;
		for(auto &e : g[u]){
			if(d[e.first] > c + e.second){
				d[e.first] = c + e.second;
				pq.emplace(c + e.second, e.first);
			}
		}
	}
	
	return d;
}

signed main(){
	scanf("%lld%lld", &n, &m);
	vector<vector<pil>> g(n);
	REP(i, m){
		int u, v, w;
		scanf("%lld%lld%lld", &u, &v, &w);
		g[u].eb(v, (ll)w);
		g[v].eb(u, (ll)w);
	}
	auto d1 = ijk(g, 0);
	auto d2 = ijk(g, n-1);
	vector<pll> vs;
	Zipper<ll> zip;
	zip.add(0);
	zip.add(INF);
	REP(i, n){
		assert(d1[i] < INF && d2[i] < INF);
		vs.eb(d1[i], d2[i]);
		zip.add(d2[i]);
	}
	vector<tuple<ll, ll, int>> query;
	scanf("%lld", &m);
	REP(i, m){
		ll a, b;
		scanf("%lld%lld", &a, &b);
		query.eb(a, b, i);
		zip.add(b);
	}
	zip.compile();
	vs.eb(INF, 0);
	sort(all(vs));
	sort(all(query));
//	rep(i, vs.size()) cout << vs[i].first << " " << vs[i].second << endl;cout << endl;
	int j=0;
	vi ans(m);
	BIT bit(zip.size());
	REP(i, vs.size()){
		while(j < m && get<0>(query[j]) < vs[i].first){
//			cout << a << ", " << b << ", " << qi << endl;
			ans[get<2>(query[j])] = bit.sum(zip(get<1>(query[j])), zip.size());
//			cout << "ans[" << get<2>(query[j]) << " = " << bit.sum(zip.lb(get<1>(query[j])), zip.size()) << endl;
			j ++;
		}
		bit.add(zip(vs[i].second), 1);
//		cout << "add " << vs[i].second << ", " << zip(vs[i].second) << endl;
		// add vs[i].second
	}
	assert(j >= m);
	for(int v : ans) printf("%lld\n", v);
	return 0;
}