#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const long long mod = 1e9+7;
long long qpow(int b,int exp){
	long long temp = b;
	long long ans = 1;
	while(exp != 0){
		if(1&exp){
			ans *= temp;
			ans %= mod;
		}
		temp *= temp;
		temp %= mod;
		exp = exp>>1;
	} 
	return ans;
} 
struct lim{
	int c,d;
};
bool comp(lim a,lim b){
	if(a.c < b.c){
		return true;
	}
	else if(a.c == b.c && a.d < b.d){
		return true;
	}
	return false;
}
lim lims[100005] = {};
int main(){
	
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	
	int T;
	cin>>T;
	for(int t = 0;t<T;t++){
		memset(lims,0,sizeof(lims));
		long long ans = 1;
		int n,m,v;
		cin>>n>>m>>v;
		for(int i = 0;i<m;i++){
			cin>>lims[i].c>>lims[i].d;
		}
		sort(lims,lims+m,comp);
		bool flag = true;
		for(int i = 0;i<m-1;i++){
			if(lims[i].c == lims[i+1].c && lims[i].d != lims[i+1].d){
				flag = false;
				break;
			}
		}
		if(!flag){
			cout<<0<<endl;
			continue;
		}
		if(lims[0].c != 1){
			ans *= qpow(v,(lims[0].c-1)*2);
			ans %= mod;
		}
		
		for(int i = 0;i<m-1;i++){
			if(lims[i+1].c == lims[i].c){
				continue;
			}
			int len = lims[i+1].c - lims[i].c;
			long long temp = qpow(v,len*2) - qpow(v,len) + qpow(v,len-1) + mod;
			temp %= mod;
			ans *= temp;
			ans %= mod;
		}
		
		if(lims[m-1].c != n){
			ans *= qpow(v,(n-lims[m-1].c)*2);
			ans %= mod;
		}
		cout<<ans<<endl;
	}
	
	fclose(stdin);
	fclose(stdout);
	
	return 0;
}

