/*
t1t2均通过全部样例，记得拿到 linux 上面去跑对拍
11:00 的时候去跑对拍
先开 t3t4 
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=1e5+5;
const ll mod=1e9+7;
int tc;
ll n;int m;ll V,V2;
struct node{
	ll pos,val;
	bool operator<(const node&x)const{
		if(pos==x.pos)return val<x.val;
		return pos<x.pos;
	}
}nd[M];
ll p[M];int tot=0;

ll qpow(ll x,ll y){
	ll ret=1;
	while(y){
		if(y&1)ret=ret*x%mod;
		x=x*x%mod;
		y>>=1;
	} 
	return ret;
}

int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);


	cin>>tc;
	
	while(tc--){
		cin>>n>>m>>V;
		V2=V*V%mod;
		for(int i=1;i<=m;i++){
			cin>>nd[i].pos>>nd[i].val;
		}
		sort(nd+1,nd+1+m);
		bool valid=1;
		for(int i=2;i<=m;i++)
			if(nd[i].pos==nd[i-1].pos&&nd[i].val!=nd[i-1].val){
				valid=0;
				break;
			}
		if(!valid){
			cout<<0<<"\n";
			continue;
		}
		tot=0;
		p[++tot]=nd[1].pos;
		for(int i=2;i<=m;i++){
			if(nd[i].pos!=nd[i-1].pos)p[++tot]=nd[i].pos;
		}
		
//		for(int i=1;i<=tot;i++)cout<<p[i]<<" ";cout<<"\n";
		
		ll ans=1;
		ans=ans*qpow(V2,p[1]-1)%mod;
		ans=ans*qpow(V2,n-p[tot])%mod;
		
//		cout<<"b4: "<<ans<<"\n";
		
		for(int i=1;i<tot;i++){
			ll delta=p[i+1]-p[i];
			ll add=qpow(V,delta-1);
			ll mul=add*V%mod;
			mul=(mul*mul%mod-mul+add);
			mul=(mul%mod+mod)%mod;
//			cout<<"i: "<<i<<" mul: "<<mul<<"\n";
			ans=ans*mul%mod;
		}
		cout<<ans<<"\n";
	}

	return 0;
}

