#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N=1e6+10;
const int INF=0x3f3f3f3f;
const LL p=1e9+7 ;
int read()
{
	char ch=getchar();
	int f=1,num=0;
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		num=(num<<1)+(num<<3)+ch-'0';
		ch=getchar();
	}
	return f*num;
}
int t,n,m;
LL v;
int d[N],a[N],vis[N];
struct node
{
	LL c,d;
}q[N];
bool cmp(node a,node b)
{
	return a.c<b.c;
}
LL qpow(LL x,LL y)
{
	LL res=1;
	while(y)
	{
		if(y&1)
			res=res*x%p;
		x=x*x%p;
		y>>=1;
	}
	return res%p;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	t=read();
	while(t--)
	{
		n=read(),m=read(),v=read();
		for(int i= 1;i<=m;i++)
			q[i].c=read(),q[i].d=read(); 
		sort(q+1,q+1+m,cmp);
		int fl=0;
		for(int i = 1;i<m;i++)
			if(q[i].c==q[i+1].c&&q[i].d!=q[i+1].d)
			{
				fl=1;
				break;
			}
		if(fl)
		{
			puts("0");
			continue;
		}
		if(n<=12&&m<=12)
		{
			for(int i = 1;i<=n;i++)
				vis[i]=a[i]=0;
			for(int i = 1;i<=m;i++)
				a[q[i].c]=q[i].d,vis[q[i].c]=1;
			int res=0;
			for(int i = 0;i<(1<<2*(n-1));i++)
			{
//				printf("%d\n",i);
				int fl=0;
				for(int i = 1;i<=n;i++)
					vis[i]=a[i]=0;
				for(int i = 1;i<=m;i++)
					a[q[i].c]=q[i].d,vis[q[i].c]=1;
				for(int j =1;j<n;j++)
				{
					if(!vis[j])
						continue;
					int x=(i&(1<<j-1))?2:1;
					int y=(i&(1<<j-1+n-1))?2:1;
//					printf("%d %d %d\n",j,x,y);
					if(a[j]==x)
					{
						if(vis[j+1]&&a[j+1]!=y)
						{
							fl=1;
							break;
						}
						vis[j+1]=1,a[j+1]=y;
					}
				}
				if(!fl) res++;
			}
			printf("%d\n",res);
			continue; 
		}
		if(m==1)
		{
			printf("%lld\n",qpow(v,(n-1)*2)%p);
			continue;
		}
			fl=0;
			for(int i = 1;i<=m;i++)
				if(q[i].c!=i)
				{
					fl=1;
					break;
				}
			if(n==m&&fl==0)
			{
//				puts("1");
				printf("%lld\n",qpow((v-1ll)*v%p+1ll,n-1)%p);
				continue;
			}
			LL ans=qpow(v,2ll*(q[1].c-1ll));
			for(int i = 2;i<=m;i++)
			{
				if(q[i].c==q[i-1].c)
					continue;
				LL now=qpow(v,2*(q[i].c-q[i-1].c));
				now=(now+p-qpow(v,q[i].c-q[i-1].c-1ll)*(v-1ll)%p)%p;
				ans=ans*now%p;
			}
//			puts("1");
			printf("%lld\n",ans*qpow(v,2ll*(n-q[m].c))%p);
	}
}
