mnlbnkk @ 2024-12-07 11:33:40
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=1e5+9,Mod=1e9+7;
struct node{ll c,d;}a[N];
ll T,n,m,v,f,ans;
inline bool cmp(node x,node y){return x.c<y.c;}
inline ll ksm(ll x,ll y)
{
x%=Mod;
ll num=1;
while(y)
{
if(y&1) num*=x,num%=Mod;
x*=x;x%=Mod;y>>=1;
}
return num;
}
int main()
{
cin>>T;
while(T--)
{
cin>>n>>m>>v;
for(int i=1;i<=m;i++)
cin>>a[i].c>>a[i].d;
sort(a+1,a+m+1,cmp);
f=0;ans=ksm(v,2*(a[1].c-1))*ksm(v,2*(n-a[m].c))%Mod;
for(int i=1;i<m;i++)
if(a[i].c!=a[i+1].c)ans=(ans*((ksm(v,2*(a[i+1].c-a[i].c))-ksm(v,2*(a[i+1].c-a[i].c-1))*(v-1)%Mod+Mod)%Mod)%Mod);
else if(a[i].d!=a[i+1].d){cout<<"0\n";f=1;break;}
if(!f)cout<<ans<<endl;
}
return 0;
}