#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cassert>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
#include <iomanip>
#include <fstream>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
const ll INF = 1LL<<61;
typedef pair<int,int> pii;

struct Query {
  ll fs, fg;
  int id;
  bool operator<(const Query &b) const {
    return fs < b.fs;
  }
};

vector<pii> g[100000];
int n,m;
ll dist[100000];
int idmap[100000];

typedef pair<ll,int> pli;
vector<pli> v;

void dijkstra_etc() {
  priority_queue<pli> Q;
  Q.push(pli(0,n-1));
  REP(i,n)dist[i]=INF;
  dist[n-1] = 0;
  while(!Q.empty()) {
    pli p = Q.top();  Q.pop();
    ll d = -p.first;
    if (dist[p.second] < d) continue;
    FOR(it, g[p.second]) {
      ll dd = d + it->second;
      if (dist[it->first] > dd) {
        dist[it->first] = dd;
        Q.push(pli(-dd,it->first));
      }
    }
  }
  v.clear();
  REP(i,n) v.push_back(pli(dist[i], i));
  sort(ALL(v));
  REP(i,n) {
    idmap[v[i].second] = i;
  }
}

const int MAX_N = 1<<17;        // 131072

struct SegSum {
  int bit[MAX_N], n;
  SegSum(int n) : n(n) {
    memset(bit, 0, sizeof(bit));
  }
  int sum(int i) {              // [1,i]の和
    int s = 0;
    while(i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  int sum2(int a, int b) {      // [a,b]の和
    return sum(b)-sum(a-1);
  }
  void add(int i, int x) {      // i(>=1)にx足す
    while(i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }
  void addSeg(int a, int b, int x) {   // [a,b]をx個加える
    add(a, x);
    add(b+1, -x);
  }
};

ll dis2[100000];
int ans[100000];

int main() {
  cin>>n>>m;
  REP(i,m) {
    int a, b, c;
    cin >> a >> b >> c;
    g[a].push_back(pii(b,c));
    g[b].push_back(pii(a,c));
  }
  dijkstra_etc();
  int qn;
  cin >> qn;
  vector<Query> qs;
  REP(i,qn) {
    Query tmp;
    cin >> tmp.fs >> tmp.fg;
    tmp.id = i;
    qs.push_back(tmp);
  }
  sort(ALL(qs));
  SegSum segsum(n);
  priority_queue<pli> Q;
  REP(i,n) dis2[i]=INF;
  Q.push(pli(0,0));
  dis2[0] = 0;
  FOR(q, qs) {
    // cout << q->fs << " " << q->fg << " " << q->id << endl;
    while(!Q.empty()) {
      pli p = Q.top();
      ll d = -p.first;
      if (d > q->fs) break;
      Q.pop();
      if (dis2[p.second]<d) continue;
      segsum.add(idmap[p.second]+1, 1);
      // cout << "a" << p.second << " " << idmap[p.second] << endl;
      FOR(it, g[p.second]) {
        ll dd = d + it->second;
        if (dis2[it->first] > dd) {
          dis2[it->first] = dd;
          Q.push(pli(-dd, it->first));
        }
      }
    }
    int up = lower_bound(ALL(v), pli(q->fg, 0)) - v.begin();
    // FOR(it, v) cout << it->first << " "; cout << endl;
    // FOR(it, v) cout << it->second << " "; cout << endl;
    // cout << "up = " << up << endl;
    ans[q->id] = segsum.sum2(up+1,n);
  }
  REP(i,qn) {
    cout << ans[i] << endl;
  }  
}

Measured memory_bytes: 15560