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;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

struct SegTree{
    int n; vector<int> dat;
    //初期化
    SegTree(int _n){
        n=1;
        while(n<_n) n*=2;
        dat=vector<int>(2*n-1,0);
    }

    void add(int k, int a){
        k+=n-1;
        dat[k] += a;
        //更新
        while(k>0){
            k=(k-1)/2;
            dat[k]=dat[2*k+1]+dat[2*k+2];
        }
    }
    //内部的に投げられるクエリ
    int _query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return 0;

        if(a<=l && r<=b) return dat[k];

        int vl=_query(a,b,2*k+1,l,(l+r)/2);
        int vr=_query(a,b,2*k+2,(l+r)/2,r);
        return vl+vr;
    }
    //[a,b)
    int query(int a, int b){
        return _query(a,b,0,0,n);
    }
};

const int N = 100000;
const ll INF = LLONG_MAX/3;

struct edge{ int to,cost; };
vector<edge> G[N];

int main(){
    int n,m;
    scanf(" %d %d", &n, &m);
    rep(i,m){
        int a,b,c;
        scanf(" %d %d %d", &a, &b, &c);
        G[a].pb({b,c});
        G[b].pb({a,c});
    }

    auto dijkstra = [&](int start){
        using P = pair<ll,int>;
        vector<ll> d(n,INF);
        priority_queue<P, vector<P>, greater<P>>pq;
        d[start] = 0;
        pq.push({0,start});
        while(!pq.empty()){
            P now = pq.top();
            pq.pop();
            int v = now.se;
            if(now.fi > d[v]) continue;
            for(const auto &e:G[v]){
                if(d[e.to] > d[v]+e.cost){
                    d[e.to] = d[v]+e.cost;
                    pq.push({d[e.to], e.to});
                }
            }
        }
        return d;
    };

    vector<ll> s = dijkstra(0), g = dijkstra(n-1);

    using pl = pair<ll,ll>;
    using query = pair<pl,int>;

    vector<ll> ug;

    int Q;
    scanf(" %d", &Q);
    vector<query> q(Q);
    rep(i,Q){
        ll fs,fg;
        scanf(" %lld %lld", &fs, &fg);
        q[i] = {{fs,fg}, i};
        ug.pb(fg);
    }
    sort(all(q));

    vector<pl> v(n);
    rep(i,n){
        v[i] = {s[i],g[i]};
        ug.pb(g[i]);
    }
    sort(all(v));

    sort(all(ug));
    ug.erase(unique(all(ug)), ug.end());
    int UG = ug.size();

    SegTree st(UG);

    int idx = 0;
    vector<int> ans(Q);
    rep(i,Q){
        int ID = q[i].se;
        ll fs = q[i].fi.fi, fg = q[i].fi.se;

        while(idx<n && v[idx].fi<=fs){
            int a_idx = lower_bound(all(ug), v[idx].se)-ug.begin();
            st.add(a_idx, 1);
            ++idx;
        }

        int gidx = lower_bound(all(ug), fg)-ug.begin();
        ans[ID] = st.query(gidx, UG);
    }

    rep(i,Q) printf("%d\n", ans[i]);
    return 0;
}