#include<iostream>
#include<map>
#include<vector>
using namespace std;
const int p=1e9+7;
long long pow(int n,int k){
	if(k==0) return 1;
	if(k%2) return (n*pow(n,k-1))%p;
	long long t=pow(n,k/2);
	return (t*t)%p;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	cin>>T;
	while(T--){
		map<int,int> h;
		vector<int> k;
		int n,m,v,cnt=0;
		bool f=1;
		cin>>n>>m>>v;
		for(int i=0;i<m;i++){
			int c,d;
			cin>>c>>d;
			if(h[c]==d) continue;
			if(h[c] && h[c]!=d) f=0;
			h[c]=d;
			cnt++;
		}
		if(n==1) cout<<0<<'\n';
		else if(f==0) cout<<0<<'\n';
		else if(cnt==1) cout<<pow(v,2*(n-1))<<'\n';
		else{
			long long ans=0;
			for(auto c:h){
				k.push_back(c.first);
			}
			for(int len=2;len<=k.size();len++){
				for(int i=0;i+len-1<k.size();i++){
					int j=i+len-1;
					int h1=k[i],h2=k[j];
					//cout<<h1<<' '<<h2<<'\n';
					long long t=pow(v,2*(h1-1));
					t=(t*(1LL*pow(v,h2-h1-1)*(v-1)%p))%p;
					t=(t*pow(v,2*(n-h2)))%p;
					if(len%2==0) ans=(ans+((pow(v,2*(n-1))+p-t)%p))%p;
					else ans=(ans+p-((pow(v,2*(n-1))+p-t)%p))%p;
				}
			}
			cout<<ans<<'\n';
		}
	}
}
