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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const ll INFTY=1ll<<61;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

struct Edge{
	int src,dst;
	ll weight;
	Edge(){}
	Edge(int s,int d,ll w):src(s),dst(d),weight(w){}
	bool operator<(const Edge& e)const{return weight<e.weight;}
	bool operator>(const Edge& e)const{return weight>e.weight;}
};
typedef vector<vector<Edge>> Graph;

void Dijkstra(const Graph& g,int s,vector<ll>& cost)
{
	int n=g.size();
	cost.assign(n,INFTY);
	priority_queue<Edge,vector<Edge>,greater<Edge>> pq;
	pq.emplace(-1,s,0);
	while(!pq.empty()){
		Edge cur=pq.top(); pq.pop();
		if(cost[cur.dst]!=INFTY)
			continue;
		cost[cur.dst]=cur.weight;
		for(Edge e:g[cur.dst])
			pq.emplace(e.src,e.dst,e.weight+cur.weight);
	}
}

struct Point{
	ll x,y;
	Point(){}
	Point(ll x,ll y):x(x),y(y){}
};
bool LessX(Point a,Point b){
	return a.x<b.x;
}
bool LessY(Point a,Point b){
	return a.y<b.y;
}

struct RangeTree2D{
	vector<Point> yps;
	struct RangeNode2D{
		vector<Point> xps;
		RangeNode2D *left,*right;
		RangeNode2D(const vector<Point>& ps):xps(ps),left(0),right(0){
			sort(all(xps),LessX);
		}
		~RangeNode2D(){
			delete left;
			delete right;
		}
	}*root;
	RangeTree2D(const vector<Point>& ps):yps(ps){
		sort(all(yps),LessY);
		root=Build(0,yps.size());
	}
	~RangeTree2D(){
		delete root;
	}
	RangeNode2D* Build(int first,int last){
		RangeNode2D* node=new RangeNode2D(vector<Point>(yps.begin()+first,yps.begin()+last));
		if(last-first>=2){
			int middle=(first+last)/2;
			node->left=Build(first,middle);
			node->right=Build(middle,last);
		}
		return node;
	}
	int Query(ll to,ll le,ll bo,ll ri,RangeNode2D* root,int first,int last){
		if(first==last || yps[last-1].y<to || bo<=yps[first].y)
			return 0;
		if(to<=yps[first].y && yps[last-1].y<bo){
			iter(root->xps) i=lower_bound(all(root->xps),Point(le,0),LessX);
			iter(root->xps) j=lower_bound(all(root->xps),Point(ri,0),LessX);
			return j-i;
		}
		int middle=(first+last)/2,res=0;
		res+=Query(to,le,bo,ri,root->left,first,middle);
		res+=Query(to,le,bo,ri,root->right,middle,last);
		return res;
	}
	int Query(ll to,ll le,ll bo,ll ri){
		return Query(to,le,bo,ri,root,0,yps.size());
	}
};

int main()
{
	for(int n,m;cin>>n>>m;){
		Graph g(n);
		rep(_,m){
			int s,d,w; cin>>s>>d>>w;
			g[s].emplace_back(s,d,w);
			g[d].emplace_back(d,s,w);
		}
		vector<ll> dist1,dist2;
		Dijkstra(g,0,dist1);
		Dijkstra(g,n-1,dist2);
		
		vector<Point> ps;
		rep(i,n) ps.emplace_back(dist1[i],dist2[i]);
		
		RangeTree2D rt(ps);
		
		int q; cin>>q;
		rep(_,q){
			ll fs,fg; cin>>fs>>fg;
			cout<<rt.Query(fg,0,INFTY,fs+1)<<endl;
		}
	}
	
	return 0;
}