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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define HUGE_NUM 99999999999999
#define NUM 100005

struct Edge{
	Edge(int arg_to,ll arg_cost){
		to = arg_to;
		cost = arg_cost;
	}
	int to;
	ll cost;
};

struct Query{
	Query(ll arg_from_start,ll arg_from_goal,int arg_index){
		from_start = arg_from_start;
		from_goal = arg_from_goal;
		index = arg_index;
	}
	bool operator<(const struct Query &arg) const{
		return from_start < arg.from_start;
	}
	ll from_start,from_goal;
	int index;
};

struct Info{
	Info(int arg_node_id,ll arg_sum_cost){
		node_id = arg_node_id;
		sum_cost = arg_sum_cost;
	}
	bool operator<(const struct Info &arg) const{
		return sum_cost > arg.sum_cost;
	}
	int node_id;
	ll sum_cost;
};

struct Data{
	bool operator<(const struct Data &arg) const{
		return dist_from_start < arg.dist_from_start;
	}
	ll dist_from_start;
	int dist_from_goal_pos;
};


int N,M,SIZE;
int BIT[NUM],ans[NUM];
ll min_dist_from_start[NUM],min_dist_from_goal[NUM];
vector<Edge> G[NUM];
vector<ll> WORK;
vector<Query> query;
Data data[NUM];

void add(int loc,int value){

	BIT[loc] += value;

	loc += loc & -loc;

	while(loc <= SIZE){
		BIT[loc] += value;
		loc += loc & -loc;
	}
}

int getSum(int loc){

	int sum = BIT[loc];

	loc -= loc & -loc;

	while(loc > 0){
		sum += BIT[loc];
		loc -= loc & -loc;
	}
	return sum;
}


int main(){

	scanf("%d %d",&N,&M);

	int from,to;
	ll cost;

	for(int loop = 0; loop < M; loop++){
		scanf("%d %d %lld",&from,&to,&cost);
		G[from].push_back(Edge(to,cost));
		G[to].push_back(Edge(from,cost));
	}

	int num_query;
	scanf("%d",&num_query);

	ll fs,fg;

	for(int i = 0; i < num_query; i++){
		scanf("%lld %lld",&fs,&fg);
		query.push_back(Query(fs,fg,i));
	}

	for(int i = 0; i < N; i++)min_dist_from_start[i] = HUGE_NUM;
	min_dist_from_start[0] = 0;

	priority_queue<Info> Q;
	Q.push(Info(0,0));

	while(!Q.empty()){

		if(Q.top().sum_cost > min_dist_from_start[Q.top().node_id]){
			Q.pop();
		}else{

			for(int i = 0; i < G[Q.top().node_id].size(); i++){
				if(min_dist_from_start[G[Q.top().node_id][i].to] > Q.top().sum_cost+G[Q.top().node_id][i].cost){
					min_dist_from_start[G[Q.top().node_id][i].to] = Q.top().sum_cost+G[Q.top().node_id][i].cost;
					Q.push(Info(G[Q.top().node_id][i].to,min_dist_from_start[G[Q.top().node_id][i].to]));
				}
			}
			Q.pop();
		}
	}

	for(int i = 0; i < N; i++)min_dist_from_goal[i] = HUGE_NUM;
	min_dist_from_goal[N-1] = 0;

	Q.push(Info(N-1,0));

	while(!Q.empty()){

		if(Q.top().sum_cost > min_dist_from_goal[Q.top().node_id]){
			Q.pop();
		}else{

			for(int i = 0; i < G[Q.top().node_id].size(); i++){
				if(min_dist_from_goal[G[Q.top().node_id][i].to] > Q.top().sum_cost+G[Q.top().node_id][i].cost){
					min_dist_from_goal[G[Q.top().node_id][i].to] = Q.top().sum_cost+G[Q.top().node_id][i].cost;
					Q.push(Info(G[Q.top().node_id][i].to,min_dist_from_goal[G[Q.top().node_id][i].to]));
				}
			}
			Q.pop();
		}
	}

	for(int i = 0; i < N; i++){
		data[i].dist_from_start = min_dist_from_start[i];
		WORK.push_back(min_dist_from_goal[i]);
	}

	sort(WORK.begin(),WORK.end());
	WORK.erase(unique(WORK.begin(),WORK.end()),WORK.end());

	int left,right,m;

	for(int i = 0; i < N; i++){
		left = 0,right = (int)WORK.size()-1,m = (left+right)/2;

		while(left <= right){
			if(WORK[m] == min_dist_from_goal[i]){
				data[i].dist_from_goal_pos = m;
				break;
			}else if(WORK[m] > min_dist_from_goal[i]){
				right = m-1;
			}else{
				left = m+1;
			}
			m = (left+right)/2;
		}
	}

	sort(query.begin(),query.end());
	sort(data,data+N);

	SIZE = WORK.size();

	for(int i = 0; i <= SIZE; i++)BIT[i] = 0;

	int near_small_index;

	int id = 0;
	for(int i = 0; i < num_query; i++){

		while(id < N && data[id].dist_from_start <= query[i].from_start){
			add(data[id].dist_from_goal_pos+1,1);
			id++;
		}

		left = 0,right = (int)WORK.size()-1,m = (left+right)/2;

		near_small_index = -1;
		while(left <= right){
			if(WORK[m] < query[i].from_goal){
				near_small_index = m+1;
				left = m+1;
			}else{
				right = m-1;
			}
			m = (left+right)/2;
		}
		if(near_small_index == -1){
			ans[query[i].index] = id;
			continue;
		}

		ans[query[i].index] = id-getSum(near_small_index);
	}

	for(int i = 0; i < num_query; i++){
		printf("%d\n",ans[i]);
	}

	return 0;
}