#include<bits/stdc++.h>
#define Code using
#define by namespace
#define wjb std
Code by wjb;
#define int long long
int in()
{
	int k=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9')k=k*10+c-'0',c=getchar();
	return k*f;
}
void out(int x)
{
	if(x<0)putchar('-'),x=-x;
	if(x<10)putchar(x+'0');
	else out(x/10),putchar(x%10+'0');
}
const int mod=1e9+7;
int ksm(int a,int b)
{
	int A=1;
	for(;b;a=a*a%mod,b>>=1)if(b&1)A=A*a%mod;
	return A;
}
vector<int>c;
map<int,int>a;
void work()
{
	int n=in(),m=in(),v=in();
	a.clear(),c.clear();bool f=false;
	while(m--)
	{
		int cc=in(),d=in();
		if(!a.count(cc))a[cc]=d,c.push_back(cc);
		else if(a[cc]!=d)f=true;
	}
	if(f){out(0),putchar('\n');return;}
	sort(c.begin(),c.end());int len=c.size();
	int s1=1,s2=ksm(v*v%mod,c[0]-1)*ksm(v*v%mod,n-c[c.size()-1])%mod;
	for(int i=0;i<len-1;i++)
		if(c[i+1]==c[i]+1)s1=(v*(v-1)%mod+1)%mod*s1%mod;
		else if(c[i+1]!=c[i])s2=(ksm(v*v%mod,c[i+1]-c[i])+mod-ksm(v,c[i+1]-c[i]-1))%mod*s2%mod;
	//cout<<s1<<" "<<s2<<"\n";
	out(s1*s2%mod),putchar('\n');
}
signed main()
{
	freopen("assign.in","r",stdin),freopen("assign.out","w",stdout);
	int t=in();
	while(t--)work();
	return 0;
}
/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2

1
10 5 2
10 2
7 2
2 2
3 2
4 2

? 2 2 2 ? ? 2 ? ? 2

9 16384

129600

1
3 2 2
1 1
3 1
*/
