Problem: You are involved in the development of a certain game. The game is for players to explore randomly generated dungeons.

There are n rooms in the dungeon generated by this game, and they are numbered from 0 to n-1. The rooms are connected by a passage. There are m passages connecting rooms. The passage can go in either direction. In addition, a distance is set between the rooms. In the generated dungeon, it is possible to go from one room to all other rooms via several passages. Then, when the player plays the game, the 0th room is selected as the starting point and the n-1th room is selected as the goal point.

I want to set up a treasure chest in some room with items that will help users explore the dungeon. At that time, it is meaningless if it is too far from the starting point or too close to the goal point. Therefore, we would like to make a room that takes at least fg as a candidate for installing a treasure chest when we reach within the distance fs from the start and take the shortest path to reach the goal.

Given the generated dungeon and q. For q queries, count the number of candidate locations for treasure chests.



Input

The input is given in the following format.


n m
a1 b1 c1
..
..
..
am bm cm
q
fs1 fg1
..
..
..
fsq fgq


ai bi ci means that the distance of the passage connecting the rooms ai and bi is ci.

Input meets the following constraints
2 ≤ n ≤ 100,000
0 ≤ ai, bi <n
0 ≤ ci ≤ 100,000
n-1 ≤ m ≤ 100,000
1 ≤ q ≤ 100,000
0 ≤ fsi, fgi ≤ 260

Output

Print the answer value on one line for each query

Example

Input

4 4
0 1 3
1 3 4
0 2 2
2 3 1
4
0 0
2 2
2 1
3 4


Output

1
1
2
1

Solution (in CPP):

Solution:
#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;
}