#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <ctime>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define all(a) a.begin(),a.end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a) rep(_X,sz(a)){printf("%d ",a[_X]);}puts("");
#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf("%d ",a[_X][_Y]);puts("");}
#define bitcount(b) __builtin_popcount(b)
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)

#define delete(a,n) a.erase(remove(all(a),n),a.end())
template<typename T, typename S> vector<T>& operator<<(vector<T>& a, S b) { a.push_back(b); return a; }
template<typename T> void operator>>(vector<T>& a, int b) {while(b--)if(!a.empty())a.pop_back();}
bool isprime(int n){ if(n<2)return false;  for(int i=2;i*i<=n;i++)if(n%i==0)return false;  return true;} 
ll b_pow(ll x,ll n){return n ? b_pow(x*x,n/2)*(n%2?x:1) : 1ll;}
string itos(int n){stringstream ss;ss << n;return ss.str();}
#define INF (1ll<<55)


struct NODE{
	int to;
	long long cost;
	NODE(int to,long long cost) : to(to) , cost(cost) {}
};
bool operator <(const NODE &a,const NODE &b){
	return a.cost > b.cost;
}
vector<NODE> g[100000];

long long goal[100000] = {};
int done[100000] = {};

vector<long long> bit[100010];

int add(int x,long long v){
	for(int i = x ; i < 100010 ; i+=i&-i){
		bit[i].push_back(v);
	}
}
int get(int x,long long v){
	int answer = 0;
	for(int i = x ; i > 0; i-=i&-i){
		answer += bit[i].size() - (upper_bound(bit[i].begin(),bit[i].end(),v-1) - bit[i].begin());
	}
	return answer;
}
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i = 0 ; i < m ; i++){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		g[a].push_back(NODE(b,c));
		g[b].push_back(NODE(a,c));
	}
	priority_queue<NODE> Q;
	Q.push(NODE(0,0));
	vector< pair<long long,int> > st;
	while(Q.size()){
		NODE q = Q.top(); Q.pop();
		if( done[q.to]++ ) continue;
		st.push_back(make_pair(q.cost,q.to));
		for(int i = 0 ; i < g[q.to].size() ; i++)
			Q.push(NODE(g[q.to][i].to,g[q.to][i].cost+q.cost));
	}
	for(int i = 0 ; i < 100000 ; i++) goal[i] = -1;
	Q.push(NODE(n-1,0));
	while(Q.size()){
		NODE q = Q.top(); Q.pop();
		if( goal[q.to] != -1) continue;
		else goal[q.to] = q.cost;
		
		for(int i = 0 ; i < g[q.to].size() ; i++)
			Q.push(NODE(g[q.to][i].to,g[q.to][i].cost+q.cost));
	}
	vector<long long> dummy;
	
	sort(st.begin(),st.end());
	for(int i = 0 ; i < st.size() ; i++) dummy.push_back(st[i].first);
	
	dummy.push_back(-INF);
	dummy.push_back(INF);
	sort(dummy.begin(),dummy.end());
	dummy.erase(unique(dummy.begin(),dummy.end()),dummy.end());
	
	for(int i = 0 ; i < st.size() ; i++){
		st[i].first = lower_bound(dummy.begin(),dummy.end(),st[i].first) - dummy.begin();
		//cout << st[i].second << " " << dummy[st[i].first] << " " << goal[st[i].second] << endl;
		add(st[i].first,goal[st[i].second]);
	}
	for(int i = 0 ; i < 100010 ; i++)
		sort(bit[i].begin(),bit[i].end());
	
	int q;
	scanf("%d",&q);
	for(int i = 0 ; i < q ; i++){
		long long fs,fg;
		scanf("%lld%lld",&fs,&fg);
		fs = upper_bound(dummy.begin(),dummy.end(),fs) - dummy.begin() - 1;
		//fg = upper_bound(dummy.begin(),dummy.end(),fs) - dummy.begin() - 1;
		//if( fs > fg ) cout <<"What" << endl;
		printf("%d\n",get(fs,fg));
		
		
	}
}

Measured memory_bytes: 26260