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 <iostream>
#include <vector>
#include <utility>
#include <queue>
#include <algorithm>
#include <unordered_map>

using namespace std;

int main() {
  int64_t n, m;
  int64_t a, b, c;
  int64_t q;
  int64_t fs, fg;
  int64_t inf = 10000000000000;
  int64_t chunk_size = 512;
  cin >> n >> m;
  vector<unordered_map<int64_t, int64_t>> edges(n);
  for (int i = 0; i < m; ++i) {
    cin >> a >> b >> c;
    if (edges[a].find(b) != edges[a].end()) {
      if (c < edges[a][b]) {
        edges[a].erase(b);
        edges[b].erase(a);
      }
    }
    edges[a].emplace(b, c);
    edges[b].emplace(a, c);
  }
  vector<pair<int64_t, int64_t>> costs(n);
  for (int i = 0; i < n; ++i) {
    costs[i].first = inf;
    costs[i].second = -1;
  }
  priority_queue<pair<int64_t, int64_t>> pq;
  pq.emplace(0, 0);
  while (!pq.empty()) {
    int64_t cost = -pq.top().first;
    int64_t id = pq.top().second;
    pq.pop();
    if (costs[id].first != inf) {
      continue;
    }
    costs[id].first = cost;
    for (auto& edge : edges[id]) {
      if (costs[edge.first].first != inf) {
        continue;
      }
      pq.emplace(-(cost + edge.second), edge.first);
    }
  }
  pq.emplace(0, n - 1);
  while (!pq.empty()) {
    int64_t cost = -pq.top().first;
    int64_t id = pq.top().second;
    pq.pop();
    if (costs[id].second != -1) {
      continue;
    }
    costs[id].second = cost;
    for (auto& edge : edges[id]) {
      if (costs[edge.first].second != -1) {
        continue;
      }
      pq.emplace(-(cost + edge.second), edge.first);
    }
  }
  sort(costs.begin(), costs.end());
  vector<vector<int64_t>> chunked_cost;
  for (int i = 0; i < n; i += chunk_size) {
    vector<int64_t> chunk(chunk_size, -1);
    for (int j = i; j < min(i + chunk_size, n); ++j) {
      chunk[j - i] = costs[j].second;
    }
    sort(chunk.begin(), chunk.end());
    chunked_cost.push_back(chunk);
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> fs >> fg;
    int cnt = 0;
    int nmin_fs = upper_bound(costs.begin(), costs.end(), pair<int64_t, int64_t> {fs, inf}) - costs.begin();
    for (int j = 0; j < nmin_fs / chunk_size; j++) {
      cnt += chunked_cost[j].end() - lower_bound(chunked_cost[j].begin(), chunked_cost[j].end(), fg);
    }
    for (int j = nmin_fs / chunk_size * chunk_size; j < nmin_fs; ++j) {
      if (costs[j].second >= fg) {
        ++cnt;
      }
    }
    cout << cnt << endl;
  }
}