#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5;
const ll P=1e9+7;
int n,m,v;
struct stu{
	int c,d;
}a[N+5];
ll Pow(ll x,int k){
	ll ans=1;
	for(;k;x=x*x%P,k>>=1)
		if(k&1) ans=ans*x%P;
	return ans;
}

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;cin>>T;
	while(T--){
		cin>>n>>m>>v;
		for(int i=1;i<=m;++i) cin>>a[i].c>>a[i].d;
		sort(a+1,a+1+m,[](stu &a,stu &b){return a.c<b.c;});
		ll s=Pow(v,(a[1].c-1)*2);
		for(int i=1;i<m;++i){
			if(a[i].c==a[i+1].c){
				if(a[i].d!=a[i+1].d){s=0;break;}
				continue;
			}
			s=s*(Pow(v,(a[i+1].c-a[i].c)*2)-Pow(v,a[i+1].c-a[i].c-1)*(v-1)%P+P)%P;
		}
		s=s*Pow(v,(n-a[m].c)*2)%P;
		cout<<s<<'\n';
	}
}
