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

const double PI = acos(-1);
const double EPS = 1e-15;
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9+7;
const ll MOD1 = 998244353;
typedef pair<ll,ll> P;

#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define rrep(i,a,b) for (int i = (a); i >= (b); i--)
#define REP(i,n) rep(i,0,n)
#define RREP(i,n) rrep(i,n,0)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
//#define mp make_pair

ll n,m;
vector<P> G[100010];
ll ds[100010];
ll dt[100010];
int bit[200010];
map<ll,int> mp;
map<int,ll> mp1;
ll ans[100010];
vector<ll> work;

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m;
  REP(i,m) {
    ll a,b,c;
    cin >> a >> b >> c;
    G[a].pb({b,c});
    G[b].pb({a,c});
  }
  fill(ds, ds + n, INF);
  fill(dt, dt + n, INF);

  priority_queue<P,vector<P>,greater<P>> pq1,pq2;
  pq1.push({0LL,0LL});
  pq2.push({0LL,n-1});

  ds[0] = 0;
  dt[n-1] = 0;

  while (!pq1.empty()) {
    P p = pq1.top();
    pq1.pop();
    ll v = p.second;
    if (ds[v] < p.first) continue;
    for (int i = 0; i < G[v].size(); i++) {
      P e = G[v][i];
      if (ds[e.fi] > ds[v] + e.se) {
        ds[e.fi] = ds[v] + e.se;
        pq1.push(P(ds[e.fi], e.fi));
      }
    }
  }

  while (!pq2.empty()) {
    P p = pq2.top();
    pq2.pop();
    ll v = p.second;
    if (dt[v] < p.first) continue;
    for (int i = 0; i < G[v].size(); i++) {
      P e = G[v][i];
      if (dt[e.fi] > dt[v] + e.se) {
        dt[e.fi] = dt[v] + e.se;
        pq2.push(P(dt[e.fi], e.fi));
      }
    }
  }

  priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> P;
  priority_queue<pair<pair<ll,ll>,int>,vector<pair<pair<ll,ll>,int>>,greater<pair<pair<ll,ll>,int>>> Q;

  REP(i,n) {
    P.push({ds[i],dt[i]});
  }
  sort(dt,dt+n);
  int num = 1;
  REP(i,n) {
    work.pb(dt[i]);
  }

  int q;
  cin >> q;
  REP(i,q) {
    ll fs, ft;
    cin >> fs >> ft;
    Q.push({{fs,ft},i});
    work.pb(ft);
  }
  sort(work.begin(),work.end());
  REP(i,sz(work)) {
    if (!mp.count(work[i])) mp[work[i]] = num++;
  }

  int cnt = 0;
  while (!Q.empty()) {
    ll lim = Q.top().fi.fi;
    while (!P.empty() && P.top().fi <= lim) {
      ll tmp = mp[P.top().se];
      P.pop();
      cnt++;
      while (tmp <= num) {
        bit[tmp]++;
        tmp += tmp & -tmp;
      }
    }
//    cout << "------------" << endl;
//    cout << cnt << endl;
    while (!Q.empty() && Q.top().fi.fi == lim) {

      ll tmp = mp[Q.top().fi.se] - 1;
      int id = Q.top().se;
      //cout << id << " " << tmp << " ";
      ans[id] += cnt;
      while (tmp > 0) {
        ans[id] -= bit[tmp];
        tmp -= tmp & -tmp;
      }
      //cout << ans[id] << endl;
      Q.pop();
    }
  }

  REP(i,q) {
    cout << ans[i] << endl;
  }

  return 0;
}

