#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

constexpr ll mod = 1e9+7;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    
    friend ostream &operator<<(ostream& os,const mint& a){
        return os << a.x;
    }

    friend istream &operator>>(istream& is,mint& a){
        ll t;
        is >> t;
        a = mint(t);
        return (is);
    }

    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

class combination{
private:
    vec<mint> fact,inv,finv;
public:
    combination(int N){
        fact = inv = finv = vec<mint>(N+1);
        fact[0] = fact[1] = 1;
        inv[0] = inv[1] = 1;
        finv[0] = finv[1] = 1;
        for(ll i=2;i<=N;i++){
            fact[i] = fact[i-1]*i;
            inv[i] = (mint) mod - inv[mod%i]*(mod/i);
            finv[i] = finv[i-1]*inv[i];
        }
    }
    mint f(int i){
        return fact[i];
    }

    mint fiv(int i){
        return finv[i];
    }
    
    mint comb(int n,int k){
        if(n<k) return 0;
        if(n<0 || k<0) return 0;
        return fact[n]*finv[k]*finv[n-k];
    }
    mint hcomb(int n,int k){
        if(n==0 && k==0) return 1;
        return comb(n+k-1,k);
    }
};

mint lagrange_polynomial(const vec<mint>& Y,ll t){
    ll N = Y.size()-1;
    combination comb(N);
    if(t<=N) return Y[t];
    mint res = 0;
    vec<mint> dp(N+1,1),pd(N+1,1);
    for(int i=0;i<N;i++) dp[i+1] = dp[i]*(t-i);
    for(int i=N;i>0;i--) pd[i-1] = pd[i]*(t-i);
    for(int i=0;i<=N;i++){
        mint tmp = Y[i]*dp[i]*pd[i]*comb.fiv(i)*comb.fiv(N-i);
        if((N-i)&1) res -= tmp;
        else res += tmp;
    }
    return res;
}

struct edge{
    int to,coin;
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,M;
    while(cin >> N >> M && N){
        vvec<edge> g(N);
        for(int i=0;i<M;i++){
            int a,b,c;
            cin >> a >> b >> c;
            a--; b--;
            g[a].push_back({b,c});
        }
        vvec<mint> dp(N,vec<mint>(3*N+1,0));
        dp[0][0] = 1;
        for(int i=0;i<N;i++) for(int j=0;j<3*N;j++){
            dp[i][j+1] += dp[i][j];
            for(auto& e:g[i]) if(j+e.coin<=3*N){
                dp[e.to][j+e.coin] += dp[i][j];
            }
        }

        auto solve = [&](){
            ll d,e;
            cin >> d >> e;
            d--;
            if(e<=3*N) cout << dp[d][e] << "\n";
            else{
                vec<mint> Y;
                for(int i=2*N;i<=3*N;i++) Y.push_back(dp[d][i]);
                cout << lagrange_polynomial(Y,e-2*N) << "\n";
            }
        };

        int Q;
        cin >> Q;
        while(Q--){
            solve();
        }
    }
}

### Prompt

Please create a solution in Cpp to the following problem:

Mysterious button

You decide to earn coins in a dungeon on the outskirts of town. There are N rooms in this dungeon, numbered from 1 to N. In addition, there are mysterious buttons called "coin button", "escape button", and "warp button" in the dungeon. The details of each button are as follows.

* There is exactly one coin button in each room. You can press the coin button as many times as you like, and you will get one coin each time.
* There is exactly one escape button in each room. When you press the escape button, you immediately escape from the dungeon and end this adventure.
* There are a total of M warp buttons. The i-th warp button is in room ai, and when you press it, you warp into room bi and you get ci coins. However, ai <bi and 1 ≤ ci ≤ 3 are satisfied. There may be multiple warp buttons in one room, or there may be rooms without warp buttons at all.



The room cannot be moved by any method other than pressing a button, and multiple buttons cannot be pressed at the same time. Also, both buttons are distinguishable from each other.

You have adventured in this dungeon Q times. If you remember correctly, the jth adventure should have started in room 1, and finally pressed the escape button in room dj, and the total number of coins earned should have been exactly ej. Find out how many such buttons are pressed for each of the Q adventures. The answer can be large, so find the remainder divided by 109 + 7, or 1000000007.

However, the two different "button pressing methods" mean that the total number of times the button is pressed is different, or that there is an integer k and the button pressed the kth time is different.

Perhaps your memory is wrong and there is no way to press such a button. In that case, output 0.

Input

The input consists of multiple datasets. Each dataset is represented in the following format.

> N M a1 b1 c1 a2 b2 c2 ... aM bM cM Q d1 e1 d2 e2 ... dQ eQ

The first line of the dataset is given the integer N, which represents the number of rooms, and the integer M, which represents the number of warp buttons, and satisfies 1 ≤ N, M ≤ 20000. The following M line gives the warp button information. The i-th line of the M line is given the room number ai where the i-th warp button exists, the room number bi of the warp destination, and the number of coins that can be obtained ci, and 1 ≤ ai <bi ≤ N and 1 ≤ Satisfy ci ≤ 3. The second line of M + is given the integer Q, which represents the number of adventures, and satisfies 1 ≤ Q ≤ 2 000. The following Q line gives you information about the adventure in your memory. The jth line of the Q line is given the room number dj you pressed the escape button in the jth adventure and the number of coins earned ej, satisfying 1 ≤ dj ≤ N and 1 ≤ ej ≤ 109.

The end of the input is represented by a line consisting of only two zeros. The total number of all datasets does not exceed 50.

Output

The output for each dataset consists of Q lines. On the jth line, start from room 1 and finally press the escape button in room dj, and divide the total number of button presses such that the total number of coins earned is exactly ej by 109 + 7. Output the remainder.

Sample Input


4 9
1 2 1
1 2 2
1 3 1
1 3 2
1 3 3
2 3 1
2 3 2
1 4 1
3 4 2
8
1 5
twenty five
3 5
4 5
1 1234567
2 1234567
3 1234567
4 1234567
4 8
1 2 1
1 2 1
one two Three
3 4 3
1 4 2
2 4 1
2 4 3
2 4 3
8
13
2 6
3 8
4 12
1 31415926
2 53589793
3 23846
4 26433832
0 0


Output for the Sample Input


1
9
37
twenty one
1
2469133
307629943
60012504
1
16
0
424
1
160769377
0
43581113






Example

Input

4 9
1 2 1
1 2 2
1 3 1
1 3 2
1 3 3
2 3 1
2 3 2
1 4 1
3 4 2
8
1 5
2 5
3 5
4 5
1 1234567
2 1234567
3 1234567
4 1234567
4 8
1 2 1
1 2 1
1 2 3
3 4 3
1 4 2
2 4 1
2 4 3
2 4 3
8
1 3
2 6
3 8
4 12
1 31415926
2 53589793
3 23846
4 26433832
0 0


Output

1
9
37
21
1
2469133
307629943
60012504
1
16
0
424
1
160769377
0
43581113

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

constexpr ll mod = 1e9+7;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    
    friend ostream &operator<<(ostream& os,const mint& a){
        return os << a.x;
    }

    friend istream &operator>>(istream& is,mint& a){
        ll t;
        is >> t;
        a = mint(t);
        return (is);
    }

    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

class combination{
private:
    vec<mint> fact,inv,finv;
public:
    combination(int N){
        fact = inv = finv = vec<mint>(N+1);
        fact[0] = fact[1] = 1;
        inv[0] = inv[1] = 1;
        finv[0] = finv[1] = 1;
        for(ll i=2;i<=N;i++){
            fact[i] = fact[i-1]*i;
            inv[i] = (mint) mod - inv[mod%i]*(mod/i);
            finv[i] = finv[i-1]*inv[i];
        }
    }
    mint f(int i){
        return fact[i];
    }

    mint fiv(int i){
        return finv[i];
    }
    
    mint comb(int n,int k){
        if(n<k) return 0;
        if(n<0 || k<0) return 0;
        return fact[n]*finv[k]*finv[n-k];
    }
    mint hcomb(int n,int k){
        if(n==0 && k==0) return 1;
        return comb(n+k-1,k);
    }
};

mint lagrange_polynomial(const vec<mint>& Y,ll t){
    ll N = Y.size()-1;
    combination comb(N);
    if(t<=N) return Y[t];
    mint res = 0;
    vec<mint> dp(N+1,1),pd(N+1,1);
    for(int i=0;i<N;i++) dp[i+1] = dp[i]*(t-i);
    for(int i=N;i>0;i--) pd[i-1] = pd[i]*(t-i);
    for(int i=0;i<=N;i++){
        mint tmp = Y[i]*dp[i]*pd[i]*comb.fiv(i)*comb.fiv(N-i);
        if((N-i)&1) res -= tmp;
        else res += tmp;
    }
    return res;
}

struct edge{
    int to,coin;
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,M;
    while(cin >> N >> M && N){
        vvec<edge> g(N);
        for(int i=0;i<M;i++){
            int a,b,c;
            cin >> a >> b >> c;
            a--; b--;
            g[a].push_back({b,c});
        }
        vvec<mint> dp(N,vec<mint>(3*N+1,0));
        dp[0][0] = 1;
        for(int i=0;i<N;i++) for(int j=0;j<3*N;j++){
            dp[i][j+1] += dp[i][j];
            for(auto& e:g[i]) if(j+e.coin<=3*N){
                dp[e.to][j+e.coin] += dp[i][j];
            }
        }

        auto solve = [&](){
            ll d,e;
            cin >> d >> e;
            d--;
            if(e<=3*N) cout << dp[d][e] << "\n";
            else{
                vec<mint> Y;
                for(int i=2*N;i<=3*N;i++) Y.push_back(dp[d][i]);
                cout << lagrange_polynomial(Y,e-2*N) << "\n";
            }
        };

        int Q;
        cin >> Q;
        while(Q--){
            solve();
        }
    }
}

```