#include <bits/stdc++.h>
#define INF 1e+18
#define int long long
using namespace std;

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

int n,m,q,ds[100010],dt[100010],bit[200010];
vector<edge> G[100010];

void dijk(int s,int *d){
	for(int i = 0;i < n;i++) d[i] = INF;
	d[s] = 0;
	priority_queue<P,vector<P>,greater<P> > que;
	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(edge e : G[v]){
			if(d[e.to] > d[v] + e.cost){
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to],e.to));
			}
		}
	}
}

void add(int i,int x){
	while(i < 200010){
		bit[i] += x;
		i += i & -i;
	}
}

int get(int i){
	int s = 0;
	while(i){
		s += bit[i];
		i -= i & -i;
	}
	return s;
}

signed main(){
	cin >> n >> m;
	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});
	}
	dijk(0,ds);
	dijk(n - 1,dt);
	vector<int> d;
	for(int i = 0;i < n;i++) d.push_back(ds[i]);
	sort(d.begin(),d.end());
	d.erase(unique(d.begin(),d.end()),d.end());
	int fs[100010],fg[100010],ans[100010];
	vector<P> query,vec;
	for(int i = 0;i < n;i++) vec.push_back(P(dt[i],lower_bound(d.begin(),d.end(),ds[i]) - d.begin()));
	sort(vec.begin(),vec.end(),greater<P>());
	cin >> q;
	for(int i = 0;i < q;i++){
		cin >> fs[i] >> fg[i];
		query.push_back(P(fg[i],i));
	}
	sort(query.begin(),query.end(),greater<P>());
	int num = 0;
	for(int i = 0;i < q;i++){
		int v = query[i].second,ind = upper_bound(d.begin(),d.end(),fs[v]) - d.begin();
		while(num < n && vec[num].first >= fg[v]) add(vec[num++].second + 1,1);
		ans[v] = get(ind);
	}
	for(int i = 0;i < q;i++) cout << ans[i] << endl;
	return 0;
}
