#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;
}

Measured memory_bytes: 53544