#include <bits/stdc++.h>
#define GET_MACRO(_1,_2,_3,_4,_5,NAME,...) NAME
#define pr(...) GET_MACRO(__VA_ARGS__,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__)
#define pr1(a) (#a)<<"="<<(a)
#define pr2(a,b) pr1(a)<<", "<<pr1(b)
#define pr3(a,b,c) pr2(a,b)<<", "<<pr1(c)
#define pr4(a,b,c,d) pr3(a,b,c)<<", "<<pr1(d)
#define pr5(a,b,c,d,e) pr4(a,b,c,d)<<", "<<pr1(e)
#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;
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);}
ostream& operator<<(ostream& o,P p){return o<<"("<<p.first<<","<<p.second<<")";}
istream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}
ostream& operator<<(ostream& o,vector<auto> &a){int i=0;for(auto t:a)o<<(i++?" ":"")<<t;return o;}
istream& operator>>(istream& i,vector<auto> &a){for(auto &t:a)i>>t;return i;}
void prArr(auto 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);}
};


class Dijkstra{
public:
  typedef tuple<int,int> T;
  int INF = 1LL<<55;
  int V;
  vector<vector<T> > G;
  
  
  Dijkstra():V(-1){}
  Dijkstra(int V):V(V),G(V){}
  
  void add_edge(int a,int b,int c){
    assert(a >= 0 && b >= 0);
    assert(a < V && b < V);
    G[a].push_back(T(b,c));
    G[b].push_back(T(a,c));
  }

  vector<int> dijkstra(int start){
    vector<int> D(V,INF);
    vector<int> visited(V,0);
    priority_queue<T,vector<T>,greater<T> > Q;
    Q.push(T(0,start));
    D[start] = 0;
    while(!Q.empty()){
      int cost, pos;
      tie(cost,pos) = Q.top(); Q.pop();

      assert(!visited[pos] || D[pos] <= cost);
      if(visited[pos]++) continue;

      for(auto t:G[pos]){
        int to = get<0>(t);
        int ncost = cost + get<1>(t);
        if(D[to] <= ncost) continue;
        D[to] = ncost;
        Q.push(T(ncost,to));
      }
    }
    return D;
  }
};

class Compress{
public:
  vector<int> X;
  bool ok;
  Compress(){};
  Compress(const vector<int> &X):X(X),ok(false){}
  
  int size(){return X.size();}
  void add(int a){ok = false; X.push_back(a); X.push_back(a-1); X.push_back(a+1);}
  void add(const vector<int> &a){for(int x:a) add(x);}
  
  void build(){
    ok = true;
    sort(X.begin(),X.end());
    X.erase(unique(X.begin(),X.end()),X.end());
  }
  
  int get(int x){assert(ok); return lower_bound(X.begin(),X.end(),x) - X.begin();}
  

};

signed main(){
  int n,m;
  cin>>n>>m;

  Dijkstra dijkstra(n);
  for(int i=0;i<m;i++){
    int a,b,c;
    cin>>a>>b>>c;
    dijkstra.add_edge(a,b,c);
  }

  auto DS = dijkstra.dijkstra(0);
  auto DT = dijkstra.dijkstra(n - 1);

  priority_queue<P,vector<P>,greater<P> > Q;
  for(int i=0;i<n;i++) Q.push(P(DS[i],DT[i]));
  
  Compress cmp;
  cmp.add(DS);
  cmp.add(DT);
  
  int q;
  cin>>q;
  typedef tuple<int,int,int> T;
  vector<T> A(q);
  for(int i=0;i<q;i++){
    int fs,fg;
    cin>>fs>>fg;
    A[i] = T(fs,fg, i);
    cmp.add({fs,fg});
  }

  cmp.build();

  sort(A.begin(),A.end());
  
  
  
  RSQ rsq(cmp.size() + 100);
  auto add=[&](int fs){
    while(!Q.empty() && fs >= Q.top().first){
      int fg = Q.top().second; Q.pop();
      int idx = cmp.get(fg);
      rsq.add(idx,idx+1,1);
    }
  };

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

  vector<int> ans(q);  
  for(int i=0;i<q;i++){
    int fs,fg,idx; tie(fs,fg,idx) = A[i]; 
    add(fs);
    int a = calc(fg);
    ans[idx] = a;
  }

  for(int a:ans) cout<<a<<endl;
  
  return 0;
}



Measured memory_bytes: 42260