#include<bits/stdc++.h>
using namespace std;
int n,m,v;
const int M = 1e5+7;
const long long mod = 1e9+7;
long long fastpow(long long a,long long b){
	int res = 1;
	while(b){
		if(b&1){
			res = (res*a)%mod;
		}
		a = (a*a)%mod;
		b = b/2;
	}
	return res;
}
struct kkk{
	int c,d;//第c个位置的变量取值为d 
}a[M],tt[M];
long long f[M];//f[i]表示c_i - 1条二元限制都确定了的方案数目 
bool cmp(kkk a,kkk b){
	return a.c<b.c;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0); 
	int T;
	cin>>T;
	while(T--){
		cin>>n>>m>>v;
		for(int i = 1;i <= m;i++){
			cin>>tt[i].c>>tt[i].d;
		}
		sort(tt+1,tt+1+m,cmp);
		for(int i = 0;i <= m;i++){
			f[i] = 0; 
		}
		bool pd = 0;
		for(int i = 1;i <= m;i++){
			if(tt[i].c == tt[i-1].c&&tt[i].d != tt[i-1].d){
				pd = 1;
			}
		}
		if(pd){
			cout<<0<<endl;
			continue;
		}
		int tot = 0;
		for(int i = 1;i <= m;i++){
			if(tt[i].c != tt[i-1].c){
				tot++;
				a[tot].c = tt[i].c;
				a[tot].d = tt[i].d;
			}
		}
		m = tot;
	//	cout<<m<<endl;
		for(int i = 1;i<= m;i++){
			//cout<<a[i].c<<" "<<a[i].d<<endl;
		} 
		//注意 f[m]不是最终答案，多测要清空 	
		//f[i]表示 c_i - 1 条二元限制都确定了的方案数目 
		if(a[1].c-1 == 0)f[1] = 1;
		else f[1] = (fastpow(v,a[1].c-1)*fastpow(v,a[1].c-1))%mod;//[1,c1 - 1]之前随便
		for(int i = 1;i <= m-1;i++){
			if(a[i].c + 1 == a[i+1].c){//相邻
				long long tmp = (f[i]*( ((v-1)*v)%mod))%mod;
				f[i+1] += f[i];
				f[i+1]%=mod;
				f[i+1] += tmp;
				f[i+1]%=mod;
			 	//a = d 时，b 要等于d_{i+1} 
			}
			else{//不相邻 
				//完全传递链 ai = d,bi = a_{i+1},b_{i+1} = a_{i+2}
				//合法的话要满足 b_{c_{i+1} -1} = d{i+1}
				//cout<<"no"<<" "<<a[i].c<<" "<<a[i+1].c<<" "<<(a[i+1].c-1) -(a[i].c+1)+1<<endl;
				int L = a[i].c+1,R = a[i+1].c-1;
				long long res = fastpow(v,(R-L+1));
				f[i+1] += (f[i]*(fastpow(v,(R-L+1))))%mod;
				f[i+1]%=mod;
				long long l = a[i].c,r = a[i+1].c-1;
				long long tmp = fastpow(v,2*(r-l+1));
				tmp = (tmp-res+mod)%mod;
				tmp = (tmp-( fastpow(v,(R-L+1)) * (v-1)  )%mod+mod)%mod;
				f[i+1] += (f[i]*tmp)%mod;
				f[i+1]%=mod;
			} 
			
		}
		if(a[m].c == n){
			cout<<f[m]<<endl;
		}
		else{
			
			long long ans = f[m];
			int l = a[m].c,r = n-1;
			long long tmp = (fastpow(v,r-l+1)*fastpow(v,r-l+1))%mod;
			ans = (ans*tmp)%mod;
			cout<<ans<<endl;
		}
	}
	
	return 0;
} 
