#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=100005,mod=1e9+7;
struct node{int x,y;} h[N];
bool cmp(node A,node B){return (A.x!=B.x?A.x<B.x:A.y<B.y);}
int n,m,k;
ll ans;
ll fpow(ll b,ll p,ll mul=1){
	for(;p;p>>=1,b=b*b%mod)if(p&1)mul=mul*b%mod;
	return mul;
}
int sub(int x,int y){x-=y;if(x<0)x+=mod;return x;}
void solve(){
	cin>>n>>m>>k;
	for(int i=1;i<=m;i++)cin>>h[i].x>>h[i].y;
	sort(h+1,h+1+m,cmp),ans=fpow(k,(h[1].x<<1)-2)*fpow(k,(n-h[m].x)<<1)%mod;
	for(int i=2;i<=m;i++){
		if(h[i].x==h[i-1].x){
			if(h[i].y!=h[i-1].y)return cout<<"0\n",void();
			continue;
		}
		ans=sub(ans*fpow(k,(h[i].x-h[i-1].x)<<1)%mod,ans*fpow(k,h[i].x-h[i-1].x-1)%mod*(k-1)%mod);
	}
	cout<<ans<<'\n';
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T;cin>>T;while(T--)solve(); 
	return 0;
}
