#include<iostream>
#include<vector>
#include<string>
#include <algorithm>
#include <cstdint>
#include <time.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
typedef long long ll;
#define mod 1000000007
template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;
public:
  u64 a;
  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};

int main(){    
    //clock_t start = clock();
    using mint = modint<1000000007>;
    int n,m,q;
    while(scanf("%d %d",&n,&m)&& n!=0){
        //cerr << n << " " << m << endl;
        //cout << endl;
        vector<vector<pair<int,int> > > s(n);
        rep(i,m){
            int a,b,c;
            scanf("%d %d %d",&a,&b,&c);
            a--;b--;
            s[b].push_back(make_pair(a,c));
        }
        vector<vector<mint> > f(n);
        vector<int> g(n);
        rep(i,n){
            if(i==0){
                f[0].push_back(1);
                g[0] = 1;
                continue;
            }
            int mx_f = 0;
            int mx_g = 0;
            for(auto x : s[i]){
                if(f[x.first].size()!=0){
                    mx_g = max(mx_g,g[x.first]);
                }
            }
            for(auto x : s[i]){
                if(f[x.first].size()!=0){
                    mx_f = max( mx_f, x.second + (int)f[x.first].size() + mx_g - g[x.first] );
                }
            }
            f[i].resize(mx_f+1);
            g[i] = mx_g+1;
            for(auto x : s[i]){
                int id = x.first;
                int p = x.second;
                int k = mx_g - g[id];
                if(f[id].size()==0)continue;
                vector<mint> ff((int)f[id].size()+k);
                rep(j,(int)f[id].size()){
                    ff[j] = f[id][j];
                }
                rep(zz,k){
                    for(int j=(int)ff.size()-1;j>0;j--){
                        if(j!=0){
                            ff[j] = ff[j] - ff[j-1];
                        }
                    }
                }
                rep(j,(int)ff.size()){
                    f[i][j+p] += ff[j];                    
                }
                
            }
            while(1){
                if(f[i].size()==0)break;
                if(f[i][f[i].size()-1].value()==0){
                    f[i].pop_back();
                }else{
                    break;
                }
            }
            if(f[i].size()==0)g[i]=0;
        }
        scanf("%d",&q);
        // rep(i,q){
          
        //     int d,e;
        //     scanf("%d %d",&d,&e);
        //     d--;
        //     mint a = 1;
        //     if(f[d].size()==0||g[d]==0){
        //         printf("0\n");
        //         continue;
        //     }
        //     int p = g[d];
        //     rep(j,p-1){
        //         a *= (e-j+p-1);
        //     }
        //     mint b = 1;
        //     rep(j,p-1){
        //         b *= p-1-j;
        //     }
        //     a /= b;
        //     mint res = 0;
        //     rep(j,min(e+1,(int)f[d].size())){
        //         res += f[d][j] * a;
        //         a /=  e+p-1-j;
        //         a *=  (e-j);
        //         if(a.value()==0)break;
        //     }
        //     printf("%d\n",res.value());
        // }
        rep(i,q){
            int d,e;
            scanf("%d %d",&d,&e);
            d--;
            if(f[d].size()==0||g[d]==0){
                printf("0\n");
                continue;
            }
            int p = g[d];
            if(p==1){
              
              printf("1\n");
              continue;
            }
            p--;
            int len = f[d].size()+p+2;
            vector<mint> ss(len);
            bool breakflag =0;
            rep(k,len){
              ss[k] = max(0,e+p-k);
            }
            vector<mint> toleft(len);
            vector<mint> toright(len);
            rep(k,len){
              if(k%p==0){
                toright[k] = 1;
              }
              if(k%p!=0){
                toright[k] = ss[k-1];
                toright[k] *= toright[k-1];
                
              }
            }
            for(int k=len-1;k>=0;k--){
              toleft[k] = ss[k];
              
              if(k!=(len-1)&&k%p!=p-1){
                toleft[k] *= toleft[k+1];
                
              }
            }
            mint b = 1;
            rep(j,p){
                b *= p-j;
            }
            mint c = (mint)1/b;
            mint res = 0;
            mint a;
            rep(j,min(e+1,(int)f[d].size())){
                
                a = toleft[j] * toright[j+p];
                a *= c;
                

                if(a.value()==0)break;
                //cerr << f[d][j] << " " << a << endl;
                res += f[d][j] *a;
              
            }
            printf("%d\n",res.value());
        }
    }
    // clock_t end = clock();

    // const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
    //cerr << time << endl;
    return 0;

}





Measured memory_bytes: 29028