#include "bits/stdc++.h"
using namespace std;

struct aa {
	int now;
	long long int time;
};
class Compare {
public:
	bool operator()(const aa&l, const aa&r) {
		return l.time > r.time;
	}
};
vector<long long int>djk(const vector<vector<pair<int, int>>>&es,const int start) {
	vector<long long int>memo(es.size(), 1ll << 55);
	memo[start] = 0;
	priority_queue<aa, vector<aa>, Compare>que;
	que.push(aa{start,0 });
	while (!que.empty()) {
		aa atop(que.top());
		que.pop();
		for (auto e : es[atop.now]) {
			const int next = e.first;
			const long long int nexttime = atop.time + e.second;
			if (memo[next] > nexttime) {
				memo[next] = nexttime;
				que.push(aa{ next,nexttime });
			}
		}
	}
	return memo;
}

vector<pair<int, long long int>>start_times, goal_times;

//???????????¨???????????????????§£??????????????¢?????´?????????????¬??????????????????¨????????????????????????
//
//    ????´??????´??°????????????
//    ?????¨????????????????????????
//    ?????? [L,R] ??????????????? [L-1, R], [L+1, R], [L, R-1], [L, R+1] ?????????????????????????????????
//	????????????????????¬??????http://pekempey.hatenablog.com/entry/2016/01/23/185143
//
//Mo_Algo???????????????????§???????
// id : ?????????????????¨??????
// l  : ?????? [
// r  : ?????? )
// ????????????????????????????????£????????????????????????????????????
struct Mo_Query {
	int id;

	//[l,r)
	long long int l;
	long long int r;

	Mo_Query(const int id_, const long long int l_, const long long int r_) :
		id(id_), l(l_), r(r_) {
	}

};

//Mo's Algorighm
//

int N;
class Mo_Algo {
public:
	const int size;
	vector<vector<Mo_Query>>squares;

	vector<int>oks;
	int ans = 0;
  ////example
  //long long int nans = 0;
	//vector<int>sums;

	Mo_Algo(int size_) :size(size_), squares(static_cast<size_t>(sqrt(size - 1) + 1)),oks(size)
	{
		oks.resize(N);
	}


	//v?????????????´????(????????????)?????????
	//??????????????????????????±?????????????????´?????????to_right???????????????????????????
	void add(const  int v, const bool to_right = false) {

		//?????????????£??????????
		if (oks[v]==1)ans++;
		oks[v]++;
	}

	//v?????????????´????(???????????????)????????????
	//??????????????????????????±?????????????????´?????????to_right???????????????????????????
	void rem(const  int v, const bool to_right = false) {

		//?????????????£??????????
		oks[v]--;
		if (oks[v]==1)ans--;
	}

	int getans() {

		//?????????????£??????????
		return ans;
	}

	//[l,r)
	//0????????????
	vector<long long int > solve(vector<Mo_Query> &querys) {
		sort(querys.begin(), querys.end(), [](const Mo_Query&l, const Mo_Query&r) {
			return l.l < r.l;
		});
		for (int i = 0; i < querys.size(); ++i) {

			squares[i / (squares.size())].push_back(querys[i]);
		}
		for (auto& asquare : squares) {
			sort(asquare.begin(), asquare.end(), [](const  Mo_Query &l, const  Mo_Query &r) {
				return l.r < r.r;
			});
		}

		vector<long long int >anss(querys.size());

		int nl = 0;
		int nr = 0;
		for (const auto square : squares) {
			for (const auto q : square) {
				const long long int ql(q.l);
				const long long int qr(q.r);
				while (nr!=N&&goal_times[nr].second >= qr) {
					add(goal_times[nr].first);
					nr++;
				}
				while (nl!= N&&start_times[nl].second <= ql) {
					add(start_times[nl].first);
					nl++;
				}
				while (nr&&goal_times[nr-1].second < qr) {
					nr--;
					rem(goal_times[nr].first);
				}
				while (nl&&start_times[nl - 1].second > ql) {
					nl--;
					rem(start_times[nl].first);
				}

				anss[q.id] = getans();
			}
		}
		return anss;
	}
};

int main() { 
	{
		int M;
		cin >> N >> M;
		vector<vector<pair<int, int>>>edges(N);
		for (int i = 0; i < M; ++i) {
			int a, b, c; cin >> a >> b >> c;
			edges[a].emplace_back(b, c);
			edges[b].emplace_back(a, c);
		}
		{

			vector<long long int>from_s, from_g;
			from_s = djk(edges, 0);
			from_g = djk(edges, N - 1);
			for (int i = 0; i < N; ++i) {
				start_times.emplace_back(i, from_s[i]);
				goal_times.emplace_back(i, from_g[i]);
			}

		}
		sort(start_times.begin(), start_times.end(), [](const pair<int, long long int>&l, const pair<int, long long int>&r) {
			return l.second < r.second;
		});
		sort(goal_times.begin(), goal_times.end(), [](const pair<int, long long int>&l, const pair<int, long long int>&r) {
			return l.second > r.second;
		});
	}
	

	int Q; cin >> Q;
		vector<Mo_Query>querys;
	for (int i = 0; i < Q;++i){
		long long int fs, fg; cin >> fs >> fg;
		querys.push_back(Mo_Query(i, fs, fg));
	}
	 Mo_Algo al(Q);
	 auto ans = al.solve(querys);
	 for (int i = 0; i < ans.size(); ++i) {
		 cout << ans[i] << endl;
	 }
	return 0;
}

Measured memory_bytes: 14532