#include<bits/stdc++.h>
using namespace std;
const long long Z=1000000007ll;
int t;
long long n,m,v,ans;
long long q[40];
struct A{
	long long x,y;
}a[100001];
bool cmp(A qwe,A asd){
	if(qwe.x==asd.x){
		return qwe.y<asd.y;
	}
	return qwe.x<asd.x;
}
void build(long long x,int p){
	if(p>=40)return ;
	q[p]=x;
	build(x*x%Z,p+1);
	return ;
}
long long exp(long long x){
	long long s=1;
	for(int i=1;x>0;i++){
		if(x%2){
			s=s*q[i]%Z;
		}
		//cout<<i<<' '<<s<<' '<<x<<' '<<q[i]<<endl;
		x=x/2;
	}
	return s;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&t);
	while(t--){
		scanf("%lld%lld%lld",&n,&m,&v);
		for(int i=1;i<=m;i++){
			scanf("%lld%lld",&a[i].x,&a[i].y);
		}
		ans=1;
		build(v,1);
		if(m==0){
			printf("%lld\n",exp(n-1));
			continue;
		}
		sort(a+1,a+1+m,cmp);
		for(int i=1;i<=m;i++){
			if(i>1&&a[i].x==a[i-1].x&&a[i].y!=a[i-1].y){
				puts("0");
				goto End;
			}
		//	cout<<2*(a[i].x-1)<<endl<<exp(2*(a[i].x-1))<<endl;
			if(i==1){
				ans=exp(2*(a[i].x-1));
			}
			else{
				ans=(exp(2*(a[i].x-a[i-1].x))-exp(a[i].x-a[i-1].x)+exp(a[i].x-a[i-1].x-1)+Z)%Z*ans%Z;
			}
		}
		ans=ans*exp(2*(n-a[m].x))%Z;
		printf("%lld\n",ans);
		End:;
	}
	return 0;
} 
