#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define bll __int128
#define vii vector<int>
#define vll vector<ll>
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define sec second
#define mp make_pair
#define qii queue<int>
#define puu pair<ull,ull>
#define clr clear
using namespace std;
const int mod=1e9+7,N=1e5+5;
struct node{int c,d;}a[N];
int T;
int m,n,v,k;
map<int,int>vis;
ll ksm(ll x,int y){ll z=1;while(y){if(y&1)z=z*x%mod;x=x*x%mod,y>>=1;}return z;}
bool cmp(node x,node y){return x.c<y.c;}
ll sqr(ll x){return x*x%mod;}
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;	
	while(T--)
	{
		cin>>n>>m>>v,k=0,vis.clr();
		int bz=0;
		for(int i=1;i<=m;i++)
		{
			int c,d;cin>>c>>d;
			if(vis.find(c)==vis.end())a[++k]={c,d},vis[c]=d;
			else if(vis[c]!=d)bz=1;
		}
		if(bz){cout<<0<<endl;continue;}
		sort(a+1,a+1+k,cmp);
		ll ans=sqr(ksm(v,a[1].c-1))*sqr(ksm(v,n-a[k].c))%mod;
		for(int i=1;i<k;i++)ans=ans*(sqr(ksm(v,a[i+1].c-a[i].c))-ksm(v,a[i+1].c-a[i].c-1)*(v-1)%mod+mod)%mod;
		cout<<ans<<endl;
	}
}
