#include<bits/stdc++.h>
#define int long long
#define mod 1000000007
using namespace std;
int a[100001];
map<int,int> mi;
int qpow(int x,int p)
{
	if(p==0) return 1;
	int q=qpow(x,p/2);
	if(p%2==0) return (q*q)%mod;
	else return (((q*q)%mod)*x)%mod;
}
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t;
	cin>>t;
	while(t--)
	{
		int n,m,v,x=1,y=0,c=0,l=0;
		cin>>n>>m>>v;
		for(int i=1;i<=m;i++)
		{
			cin>>a[i]>>x;
			if(mi[a[i]]!=x&&mi[a[i]]!=0) c=1;
			mi[a[i]]=x;
		}
		mi.clear();
		if(c) 
		{
			cout<<0<<endl;
			continue;
		}
		sort(a+1,a+1+m);
//		a[m]=n-1;
		int k=0;
		while(a[m]==n) k=1;
		for(int i=1;i<m;i++)
		{
			if(a[i]==a[i-1]) continue;
			int yi=y*qpow(v,a[i]-l-1)%mod,xi=(qpow(v,2*(a[i]-1))-yi+mod)%mod;
			if(a[i]+1==a[i+1]) xi=x,yi=y;
//			cout<<xi<<" "<<yi<<endl;
			x=((((xi+yi)%mod)*(v-1)%mod)*v)%mod;
			if(a[i]+1==a[i+1])  y=(xi+yi)%mod;
			else y=(((xi+yi)%mod)*v)%mod;
//			cout<<x<<' '<<y<<endl;
//			cout<<endl; 
			l=a[i];
		}
		if(a[m-1]!=n-1)
		y=y*qpow(v,n-1-l-1)%mod,x=(qpow(v,2*(n-1))-y+mod)%mod;
//		cout<<n-l-2<<endl; 
//		cout<<x<<' '<<y<<endl;
		if(!k) cout<<(x+y)%mod<<endl;
		else cout<<(x+(y/v))%mod<<endl;
	}

	return 0;
} 
/*
1
5 1 2
1 2
*/
