#include<bits/stdc++.h>
#define LL long long
using namespace std;
const LL M=1e9+7;
const int N=1e5+5;
struct node{
	int pos,val;
}S[N];
bool cmp(node c,node y){
	return c.pos<y.pos;
}
int Rd(){
	char c=getchar();
	int x=0;
	while(c<'0' || c>'9') c=getchar();
	while(c>='0' && c<='9'){
		x=x*10+(int)c-48;
		c=getchar();
	}
	return x;
}
void Wt(LL x){
	if(x>9ll) Wt(x/10ll);
	putchar((char)(x%10ll+48ll));
}
LL Qp(LL now,int x){
	LL ans=1ll;
	while(x){
		if(x&1) ans=ans*now%M;
		now=now*now%M,x>>=1;
	}
	return ans;
}
int n,m,v,R;
int T;
bool con;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T); ++T;
	while(--T){
		scanf("%d%d%d",&m,&n,&v);
		for(int i=1;i<=n;++i){
			scanf("%d",&S[i].pos);
			scanf("%d",&S[i].val);
		}
		LL now=(LL)v; now=now*now%M;
		if(n==1){
			Wt(Qp(now,m-1));
			putchar('\n');
			continue;
		}
		sort(S+1,S+1+n,cmp);
		con=false,R=0;
		for(int i=2;i<=n;++i){
			if(S[i].pos==S[i-1].pos){
				if(S[i].val!=S[i-1].val){
					putchar('0');
					putchar('\n');
					con=true;
					break;
				}
				++R;
			}
		}
		if(con) continue;
		Wt(Qp(now-(LL)v+1ll,m-1));
		putchar('\n');
	}
	return 0;
} 
