#include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int N = 100010;
const int INF = 1LL<<55;
const int mod = (1e9)+7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
typedef pair<int,int> P;
typedef long long ll;
ostream& operator<<(ostream& o,P p){
  return o<<"("<<p.first<<","<<p.second<<")";
}
istream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}
template<class T> istream& operator>>(istream& i,vector<T> &a){for(auto &t:a)cin>>t;return i;}
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
template<class T> void prArr(T a,string s=" "){int i=0;for(auto t:a)cout<<(i++?s:"")<<t;cout<<endl;}

class RSQ{
public:
  typedef long long ll;
  ll n;
  vector<ll> dat,td;
  
  RSQ(){n = -1;}
  RSQ(int n_){
    n=1;
    while(n<n_)n*=2; //要素数nを2のべき乗に
    dat.resize(2*n-1,0),td.resize(2*n-1,0);
  }
  
  //[a,b)の区間にxを加算する,query(a,b,x);
  ll add(int a,int b,ll x,int k=0,int l=0,int r=-1){
    if(r==-1) r=n, assert(a < n && b <= n);
    if(r<=a||b<=l)return 0;
    if(a<=l&&r<=b){
      dat[k]+=(r-l)*x; 
      td[k]+=x;
      return dat[k];
    }

    dat[k]+=(min(r,b)-max(l,a))*x;
    ll kl=k*2+1,kr=k*2+2,t=td[k]*(r-l)/2;
    dat[kl]+=t, dat[kr]+=t;
    td[kl]+=td[k], td[kr]+=td[k];
    td[k]=0;

    ll vl=add(a,b,x,k*2+1,l,(l+r)/2);
    ll vr=add(a,b,x,k*2+2,(l+r)/2,r);
    return vl+vr;
  }
  
  //[a,b)の総和を得る
  ll sum(int a,int b){return add(a,b,0);}
};


vector<vector<P> > G;

vector<int> dijkstra(int start){
  int n = G.size();
  vector<int> D(n,INF);
  priority_queue<P,vector<P>,greater<P> > Q;
  Q.push(P(0,start));
  D[start] = 0;
  
  while(!Q.empty()){
    auto t = Q.top(); Q.pop();
    int cost,pos; tie(cost,pos) = t;

    if(D[pos] < cost) continue;
    
    for(auto p:G[pos]){
      int to = p.first;
      int ncost = cost + p.second;
      if(D[to] <= ncost) continue;
      D[to] = ncost;
      Q.push(P(ncost,to));
    }
    
  }
  return D;
}

typedef pair<P,int> PP;
signed main(){
  int n,m;

  cin>>n>>m;

  G.resize(n);
  for(int i=0;i<m;i++){
    int a,b,c;
    cin>>a>>b>>c;
    G[a].push_back(P(b,c));
    G[b].push_back(P(a,c));
  }


  auto DA = dijkstra(0);
  auto DB = dijkstra(n-1);

  priority_queue<P,vector<P>,greater<P> > D;
  for(int i=0;i<n;i++) D.push(P(DA[i],DB[i]));
  
  vector<int> X;
  for(int i=0;i<n;i++){
    int a = DB[i];
    X.push_back(a);
    X.push_back(a+1);
    X.push_back(a-1);
  }

  for(int i=0;i<n;i++){
    int a = DA[i];
    X.push_back(a);
    X.push_back(a+1);
    X.push_back(a-1);
  }
  
  
  auto get=[&](int x){
    return lower_bound(X.begin(),X.end(),x) - X.begin();
  };
  
  
  int q;
  cin>>q;
  vector<PP> A(q);
  for(int i=0;i<q;i++){
    int fs,fg;
    cin>>fs>>fg;
    A[i] = PP(P(fs,fg),i);
  }

  for(int i=0;i<q;i++){
    int a = A[i].first.first;
    X.push_back(a);
    X.push_back(a+1);
    X.push_back(a-1);
  }
  
  for(int i=0;i<q;i++){
    int a = A[i].first.second;
    X.push_back(a);
    X.push_back(a+1);
    X.push_back(a-1);
  }

  sort(X.begin(),X.end());
  X.erase(unique(X.begin(),X.end()),X.end());


  sort(A.begin(),A.end());


  RSQ rsq(X.size()+100);

  auto add=[&](int fs){
    while(!D.empty() && D.top().first <= fs){
      int fg = D.top().second; D.pop();
      int idx = get(fg);
      rsq.add(idx, idx+1,1);
    }
  };

  auto calc=[&](int fg){
    int idx = get(fg);
    int res = rsq.sum(idx, rsq.n);
    return res;
  };


  vector<int> Ans(q);
  for(int i=0;i<q;i++){
    int fs = A[i].first.first;
    int fg = A[i].first.second;
    int idx = A[i].second;
    
    add(fs);
    int ans = calc(fg);
    Ans[idx] = ans;
  }

  for(int i=0;i<q;i++) cout<<Ans[i]<<endl;
  
  return 0;
}




Measured memory_bytes: 42288