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
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename T,T MOD = 1000000007>
struct Mint{
  static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T,T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T,T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}


template<typename M>
M lagrange_interpolation(vector<M> &ys,M t){
  int n=ys.size()-1;
  if(t.v<=n) return ys[t.v];

  vector<M> dp(n+1,1),pd(n+1,1);
  for(int i=0;i<n;i++) dp[i+1]=dp[i]*(t-M(i));
  for(int i=n;i>0;i--) pd[i-1]=pd[i]*(t-M(i));

  vector<M> fact(n+1,1),finv(n+1,1);
  for(int i=1;i<=n;i++) fact[i]=fact[i-1]*M(i);
  finv[n]=M(1)/fact[n];
  for(int i=n;i>=1;i--) finv[i-1]=finv[i]*M(i);

  M res(0);
  for(int i=0;i<=n;i++){
    M tmp=ys[i]*dp[i]*pd[i]*finv[i]*finv[n-i];
    if((n-i)&1) res-=tmp;
    else res+=tmp;
  }
  return res;
}

//INSERT ABOVE HERE
using M = Mint<int>;
const int MAX = 6060;
M dp[2020][MAX]={};
M ad[2020][MAX]={};
signed main(){
  int n,m;
  while(cin>>n>>m,n||m){
    vector<int> as(m),bs(m),cs(m);
    vector< vector<int> > G(n+1);
    for(int i=0;i<m;i++){
      cin>>as[i]>>bs[i]>>cs[i];
      G[as[i]].emplace_back(i);
    }
    as.emplace_back(0);
    bs.emplace_back(1);
    cs.emplace_back(0);
    G[0].emplace_back(m);

    dp[0][0]=M(1);
    for(int v=0;v<=n;v++){
      for(int j=0;j<MAX;j++){
        if(j) ad[v][j]+=ad[v][j-1];
        dp[v][j]+=ad[v][j];
      }

      for(int i:G[v])
        for(int j=0;j+cs[i]<MAX;j++)
          ad[bs[i]][j+cs[i]]+=dp[as[i]][j];
    }

    int q;
    cin>>q;
    for(int i=0;i<q;i++){
      int d,e;
      cin>>d>>e;
      vector<M> ys(dp[d]+n+n,dp[d]+n+n+n+1);
      if(e<n+n+n) cout<<dp[d][e]<<"\n";
      else cout<<lagrange_interpolation(ys,M(e-n-n))<<"\n";
    }

    for(int v=0;v<=n;v++)
      for(int j=0;j<MAX;j++)
        dp[v][j]=ad[v][j]=M(0);
  }
  cout<<flush;
  return 0;
}

