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 <queue>
#include <algorithm>
 
#define rep(i, n) for(int i = 0; i < (n); ++i)
 
using namespace std;

typedef long long ll;
typedef pair<ll, int> LP;
typedef vector<int> V;
typedef vector<ll> LV;

const ll inf = 2e18;

struct E{
    int to, cost;
};

int n;
int m;
vector<E> g[100000];
int q;
ll ds[100000];
ll dg[100000];
LV cp, cpg;
V st[1 << 18];

void add_edge(int a, int b, int c){
    E e = {b, c};
    g[a].push_back(e);
    E f = {a, c};
    g[b].push_back(f);
}

void dijkstra(int s, ll* d){
    fill_n(d, n, inf);

    priority_queue<LP, vector<LP>, greater<LP>> q;
    d[s] = 0;
    q.push(LP(0, s));
    while(!q.empty()){
        LP v = q.top();
        q.pop();

        if(v.first > d[v.second]){
            continue;
        }

        for(E& e: g[v.second]){
            if(d[e.to] > v.first + e.cost){
                d[e.to] = v.first + e.cost;
                q.push(LP(d[e.to], e.to));
            }
        }
    }
}

void compress(ll* ds, LV& cp){
    rep(i, n){
        cp.push_back(ds[i]);
    }
    sort(cp.begin(), cp.end());
    cp.erase(unique(cp.begin(), cp.end()), cp.end());

    rep(i, n){
        ds[i] = lower_bound(cp.begin(), cp.end(), ds[i]) - cp.begin();
    }
}

void merge(V a, V b, V& c){
    int i = 0;
    int j = 0;
    while(i < a.size() || j < b.size()){
        if(i < a.size()){
            if(j < b.size() && a[i] > b[j]){
                c.push_back(b[j++]);
            }
            else{
                c.push_back(a[i++]);
            }
        }
        else{
            c.push_back(b[j++]);
        }
    }
}

void init1(){
    rep(i, n){
        st[(1 << 17) + ds[i] - 1].push_back(dg[i]);
    }
    rep(i, n){
        sort(st[(1 << 17) + i - 1].begin(), st[(1 << 17) + i - 1].end());
    }
}

V init2(int l = 0, int r = 1 << 17, int k = 0){
    if(r - l == 1){
        return st[k];
    }
    merge(init2(l, (l + r) / 2, 2 * k + 1), init2((l + r) / 2, r, 2 * k + 2), st[k]);
    return st[k];
}

int count_dj(int fs, int fg, int l = 0, int r = 1 << 17, int k = 0){
    if(fs <= l){
        return 0;
    }

    if(r <= fs){
        return st[k].end() - lower_bound(st[k].begin(), st[k].end(), fg);
    }

    int m = (l + r) / 2;
    int p = count_dj(fs, fg, l, m, 2 * k + 1);
    int q = count_dj(fs, fg, m, r, 2 * k + 2);
    return p + q;
}

int main(){
    cin >> n >> m;
    rep(i, m){
        int a, b, c;
        cin >> a >> b >> c;
        add_edge(a, b, c);
    }

    dijkstra(0, ds);
    dijkstra(n - 1, dg);

    compress(ds, cp);
    compress(dg, cpg);

    init1();
    init2();

    cin >> q;
    rep(i, q){
        ll fs, fg;
        cin >> fs >> fg;
        fs = upper_bound(cp.begin(), cp.end(), fs) - cp.begin();
        fg = lower_bound(cpg.begin(), cpg.end(), fg) - cpg.begin();
        cout << count_dj(fs, fg) << endl;
    }
    return 0;
}