#include<cstdio>
#include<vector>
#include<array>
#include<algorithm>
int main(){
  using ll = long long;
  const int mod=1e9+7;
#ifndef LOCAL_TEST
  freopen("assign.in","r",stdin);
  freopen("assign.out","w",stdout);
#endif
  // freopen("assign1.in","r",stdin);
  int T;
  scanf("%d",&T);
  while(T--){
    int n,m,v;
    scanf("%d %d %d",&n,&m,&v);
    std::vector<std::array<int,2>> rst(m);
    bool flag=1;
    for(int i=0;i<m;i++){
      int c,d;
      scanf("%d %d",&c,&d);
      rst[i]={c,d};
    }
    std::sort(rst.begin(),rst.end());
    // for(auto x:rst){
      // fprintf(stderr,"dbg:%d %d\n",x[0],x[1]);
    // }
    auto qpow=[](ll a,int b){
      ll r=1;
      while(b){
        if(b&1)(r*=a)%=mod;
        (a*=a)%=mod;
        b>>=1;
      }
      return r;
    };
    ll ans=1;
    (ans*=qpow(v,2*(rst[0][0]-1)))%=mod;
    (ans*=qpow(v,2*(n-rst[m-1][0])))%=mod;
    for(int i=1;i<m;i++){
      if(rst[i][0]==rst[i-1][0]){
        if(rst[i][1]!=rst[i-1][1]){
          ans=0;
          goto output;
        }
        else continue;
      }
      // fprintf(stderr,"dbg:%d %d\n",rst[i][0],rst[i-1][0]);
      ll x=qpow(v,2*(rst[i][0]-rst[i-1][0]));
      x=(x-qpow(v,rst[i][0]-rst[i-1][0]-1)+mod)%mod;
      (ans*=x)%=mod;
    }
  output:
    printf("%lld\n",ans);
  }
  return 0;
}
