#include<bits/stdc++.h>
#define int long long
using namespace std;
const int p=1e9+7;
int T,n,m,v,c,d;
int ksm(int x,int k){int s=1;for(;k;k>>=1,x=x*x%p)if(k&1)s=s*x%p;return s;}
signed main(){
	freopen("assign.in","r",stdin);freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>T;
	for(int t=1;t<=T;t++){
		cin>>n>>m>>v;
		map<int,int>mp;bool zha=0;
		while(m--)if(cin>>c>>d,mp.count(c)&&mp[c]!=d)zha=1;
			else mp[c]=d;
		if(zha){cout<<"0\n";continue;}
		int sum=1,las=0,v2=v*v%p;
		for(auto i:mp){
			int x=i.first;
			if(las)sum=sum*(ksm(v2,x-las)-ksm(v,x-las-1)*(v-1)%p+p)%p;
			else sum=sum*ksm(v2,x-1)%p;
			las=x;
		}
		sum=sum*ksm(v2,n-las)%p;
		cout<<sum<<'\n';
	}
}
