#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
int t,n,m,v,c[100005],d[100005],ans;
map<int,int>q;
template<typename T>T qpow(T a,T b,T n,T ans=1){
  for(a%=n;b;b>>=1)b&1&&(ans=1ll*ans*a%n),a=1ll*a*a%n;
  return ans;
}
int main(){
  freopen("assign.in","r",stdin);
  freopen("assign.out","w",stdout);
  ios::sync_with_stdio(0),cin.tie(0),cin>>t;
  while(t--){
    cin>>n>>m>>v;
    for(int i=1;i<=m;i++)cin>>c[i]>>d[i];
    for(int i=1;i<=m;i++){
      if(q[c[i]]&&q[c[i]]!=d[i]){
        cout<<"0\n";
        goto tag;
      }
      q[c[i]]=d[i];
    }
    m=0;
    for(map<int,int>::iterator it=q.begin();it!=q.end();it++)c[++m]=(*it).first,d[m]=(*it).second;
    ans=qpow(v,2*n-2,mod);
    for(int i=1,now=qpow(v,2*c[1]-2,mod);i<m;i++){
      ans=(ans-1ll*now*qpow(v,c[i+1]-c[i]-1+2*(n-c[i+1]),mod)%mod*(v-1)%mod+mod)%mod;
      now=1ll*now*(qpow(v,2*(c[i+1]-c[i]),mod)-1ll*qpow(v,c[i+1]-c[i]-1,mod)*(v-1)%mod+mod)%mod;
    }
    cout<<ans<<'\n';
    tag:q.clear();
  }
  return 0;
}
