#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <stack>
#include <climits>
#include <deque>
#include <bitset>
#include <cassert>
#include <ctime>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
const int dy[]={-1,0,1,0},dx[]={0,1,0,-1};
// adjust problem by problem
const double EPS=1e-8;
const double PI=acos(-1.0);
#ifdef __GNUC__
int popcount(int n){return __builtin_popcount(n);}
int popcount(ll n){return __builtin_popcountll(n);}
#endif
#ifndef __GNUC__
template<class T> int popcount(T n){int cnt=0;while(n){if(n%2)cnt++;n/=2;}return cnt;}
#endif
template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}
bool EQ(double a,double b){return abs(a-b)<EPS;}
void fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);}
vector<string> split(string str,char del){
  vector<string> res;
  for(int i=0,s=0;i<SIZE(str);i++){
    if(str[i]==del){if(i-s!=0)res.push_back(str.substr(s,i-s));s=i+1;}
    else if(i==SIZE(str)-1){res.push_back(str.substr(s));}
  }
  return res;
}


const int MAX_N=1<<20;
const ll INF=1LL<<61;
class RMQ{
public:
  int n;
  vector<ll> dat[2*MAX_N-1];
  // 初期化
  void init(int n_,ll dis[100001]){
    n=1;
    while(n<n_)n*=2;
    initDFS(0,dis);
  }
  void initDFS(int now,ll dis[100001]){
    vector<ll> &res=dat[now];
    if(now>=n-1){
      if(now-n+1>=100001)res.push_back(0);
      else res.push_back(dis[now-n+1]);
    }
    else{
      initDFS(now*2+1,dis);
      initDFS(now*2+2,dis);
      vector<ll> &v1=dat[now*2+1];
      vector<ll> &v2=dat[now*2+2];
      res.resize(v1.size()+v2.size());
      merge(v1.begin(),v1.end(),v2.begin(),v2.end(),res.begin());
    }
  }
  int _query(int a,int b,int k,int l,int r,ll val){
    if(r<=a||b<=l)return 0;
    else if(a<=l&&r<=b){
      // lower_boundで範囲の値を計算
      return dat[k].end()-lower_bound(dat[k].begin(),dat[k].end(),val);
    }
    int vl=_query(a,b,k*2+1,l,(l+r)/2,val);
    int vr=_query(a,b,k*2+2,(l+r)/2,r,val);
    return vl+vr;
  }
  int query(int a,int b,ll val){
    return _query(a,b,0,0,n,val);	
  }
};

RMQ rmq;
int N,M,Q;
struct edge{
  int to;
  ll cost;
};
vector<edge> G[100001];
ll dist[100001];
ll gDist[100001];
typedef pair<ll,ll> pll;
void dijkstra(int s,ll dis[100001]){
  priority_queue<pll,vector<pll>,greater<pll> > pq;
  for(int i=0;i<N;i++)dis[i]=INF;
  dis[s]=0;
  pq.push(pii(0,s));
  while(pq.size()){
    pll p=pq.top();pq.pop();
    ll ccost=p.first;
    int cnode=p.second;
    if(dis[cnode]<ccost)continue;
    for(int i=0;i<(int)G[cnode].size();i++){
      edge &e=G[cnode][i];
      if(dis[e.to]>ccost+e.cost){
        dis[e.to]=ccost+e.cost;
        pq.push(pll(dis[e.to],e.to));
      }
    }
  }
}
int main(){
  fastStream();
  cin>>N>>M;
  for(int i=0;i<M;i++){
    int a,b,c;
    cin>>a>>b>>c;
    edge e;
    e.to=b;e.cost=c;
    G[a].push_back(e);
    e.to=a;
    G[b].push_back(e);
    // G[a].push_back((edge){b,c});
    // G[b].push_back((edge){a,c});
  }
  dijkstra(0,dist);
  dijkstra(N-1,gDist);
  vector<pll> v;
  for(int i=0;i<N;i++)v.push_back(make_pair(dist[i],gDist[i]));
  sort(v.begin(),v.end());
  for(int i=0;i<(int)v.size();i++)gDist[i]=v[i].second;
  rmq.init(N,gDist);
  cin>>Q;
  for(int q=0;q<Q;q++){
    ll s,g;
    cin>>s>>g;
    int pos=upper_bound(v.begin(),v.end(),pll(s,INF))-v.begin();
    int cnt=0;
    if(pos>0)cnt+=rmq.query(0,pos,g);
    cout<<cnt<<endl;
  }
  return 0;
}

Measured memory_bytes: 84536