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;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using Pi = pair<int, int>;
using Tapris = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  const int sqrtN = 512;

  int n, m;
  cin >> n >> m;

  struct edge {
    int to, cost;
    edge(){}
    edge(int to, int cost):to(to), cost(cost){}
  };
  vector<vector<edge> > graph(n);
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    graph[a].emplace_back(b, c);
    graph[b].emplace_back(a, c);
  }

  auto dijkstra = [&](int s){
    vint res(n, inf);
    res[s] = 0;
    priority_queue<Pi, vector<Pi>, greater<Pi> > que;
    que.emplace(res[s], s);
    while(!que.empty()) {
      Pi p = que.top(); que.pop();
      int v = p.second;
      if(res[v] < p.first) continue;
      for(edge& e : graph[v]) {
	if(e.cost+res[v] < res[e.to]) {
	  res[e.to] = e.cost+res[v];
	  que.emplace(res[e.to], e.to);
	}
      }
    }
    return res;
  };

  auto ds = dijkstra(0);
  auto dg = dijkstra(n-1);

  vint press_ds;
  rep(i, n) press_ds.push_back(ds[i]);

  int q;
  cin >> q;
  vector<Pi> qs;
  rep(i, q) {
    int f, g;
    cin >> f >> g;
    qs.emplace_back(f, g);
    press_ds.push_back(f);
  }

  sort(all(press_ds));
  press_ds.erase(unique(all(press_ds)), press_ds.end());

  int bucket_sz = ((int)press_ds.size()+sqrtN-1)/sqrtN;
  vector<vector<int> > bucket_dat(bucket_sz);
  vector<vector<int> > dat(press_ds.size());
  rep(i, n) {
    int p = lower_bound(all(press_ds), ds[i])-press_ds.begin();
    bucket_dat[p/sqrtN].push_back(dg[i]);
    dat[p].push_back(dg[i]);
  }

  rep(i, bucket_sz) sort(all(bucket_dat[i]));
  rep(i, dat.size()) sort(all(dat[i]));

  auto ask = [&](int a, int b, int x) {
    int res = 0;
    rep(i, bucket_sz) {
      int l = i*sqrtN, r = (i+1)*sqrtN;
      if(r <= a || b <= l) continue;
      if(a <= l && r <= b) {
	res += bucket_dat[i].end() - lower_bound(all(bucket_dat[i]), x);
      } else {
	reps(j, max(a, l), min(b, r)) {
	  res += dat[j].end() - lower_bound(all(dat[j]), x);
	}
      }
    }
    return res;
  };

  for(Pi p : qs) {
    int l = 0, r = upper_bound(all(press_ds), p.first)-press_ds.begin();
    cout << ask(l, r, p.second) << endl;
  }

  return 0;
}