#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dec(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
typedef long long LL;
const int M=1e9+7,N=1e5+5;
int T,n,m,v,t[N];
pair<int,int> mp[N];
LL ans;
inline int Rd(){
	int s=0,w=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
	while (ch>='0'&&ch<='9') s=(s<<1)+(s<<3)+ch-'0',ch=getchar();
	return s*w;
}
LL qp(LL a,LL b){
	LL res=1;
	while (b){
		if(b&1) res=res*a%M;
		a=a*a%M;
		b>>=1;
	}
	return res;
}
LL Sol(){
	n=Rd();m=Rd();v=Rd();
	int sz=0;
	rep(i,1,m){
		int c=Rd(),d=Rd();
		mp[++sz]={c,d};
	}
	if(!m||m==1) return qp(v,2*(n-1));
	sort(mp+1,mp+sz+1);
	sz=unique(mp+1,mp+sz+1)-mp-1;
	rep(i,1,sz) if(mp[i].first==mp[i-1].first) return 0;
	rep(i,1,sz) t[i]=mp[i].first;
	ans=qp(v,2*(t[1]-1))*qp(v,2*(n-t[sz]))%M;
//	rep(i,1,sz) printf("%d ",t[i]);puts("");
	rep(i,1,sz-1){
		int len=t[i+1]-t[i];
		ans=ans*(qp(v,2*len)-qp(v,len-1)*(v-1)%M+M)%M;
	}
	return ans;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	T=Rd();
	while (T--) printf("%lld\n",Sol());
	return 0;
}
