#include<bits/stdc++.h>
using namespace std;
#define int long long
#define f(i,j,k) for(int i=j;i<=k;++i)
#define g(i,j,k) for(int i=j;i>=k;--i)
int n,m,s,l;
struct IO{const static int N=1<<20,u=3; char a[N];int l,r;char gc(){if(l==r)fread(a,1,u,stdin),l=0,r=u;return a[l++];}}IO;
inline int read(){
	int x=0;char ch=IO.gc();
	for(;'0'>ch||ch>'9';ch=IO.gc());
	for(;'0'<=ch&&ch<='9';ch=IO.gc())x=(x<<3)+(x<<1)+(ch^48);
	return x; 
}
const int N=2e5,mo=1e9+7;
struct mt{int a,b,c,d;};
inline mt operator*(mt x,mt y){return{(x.a*y.a+x.b*y.c)%mo,(x.a*y.b+x.b*y.d)%mo,(x.c*y.a+x.d*y.c)%mo,(x.c*y.b+x.d*y.d)%mo};}
inline mt ksm(mt x,int p=mo-2,mt y={1,0,0,1}){for(;p;p/=2,x=x*x)if(p&1)y=x*y;return y;}
map<int,int>a;int x,y;
inline void doing(){
	n=read();m=read();s=read();
	a.clear();l=1;
	f(_,1,m){
		x=read();y=read();
		if(a[x]&&a[x]!=y)l=0;
		a[x]=y;
	}
	if(!l)return printf("0\n"),void();
	mt F=a[1]?(mt){1,0,0,0}:(mt){0,1,0,0};
	mt A={s,(s-1)*s%mo,0,s*s%mo},B={((s-1)*s+1)%mo,0,s*s%mo,0};
	for(auto e:a)if((s=e.first)>1)F=F*ksm(A,s-l-1)*B,l=s;
	F=F*ksm(A,n-s);printf("%lld\n",(F.a+F.b)%mo);
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t=read();
	while(t--)doing(); 
	return 0;
}
