#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5,mod=1e9+7;
int n,m,v,vis[N],ans;
signed main(){
freopen("assign.in","r",stdin);
freopen("assign.out","w",stdout);
int t; cin>>t;
while(t--){
memset(vis,0,sizeof(vis)); ans=1; int flag=0;
scanf("%lld%lld%lld",&n,&m,&v);
for(int i = 1;i<=m;i++){
int x,d; scanf("%lld%lld",&x,&d);
if(vis[x]!=d&&vis[x]!=0){puts("0"); flag=1; break;}
vis[x]=d;
}
if(flag) continue;
for(int i = 1;i<n;i++){
if(vis[i]&&vis[i+1]) ans=(ans*(v*(v-1)%mod+1))%mod;
else ans=(ans*v*v)%mod;
}
printf("%lld\n",ans);
}
return 0;
}