#include<bits/stdc++.h>
using namespace std;typedef long long ll;
int read(){int x=0,f=1;char ch;for(;!isdigit(ch=getchar());)if(ch=='-')f=-1;for(;isdigit(ch);ch=getchar())x=x*10+(ch-'0');return x*f;}
namespace Main
{
	const int N=100010,mod=1e9+7;int ksm(int x,int y=mod-2){int res=1;for(;y;y>>=1,x=1ll*x*x%mod)if(y&1)res=1ll*res*x%mod;return res;}
	int n,m,k,a[N],x,y,z,xx,yy,zz,ans;map<int,int>e;bool fl;
	void main()
	{
		cin>>n>>m>>k,e.erase(e.begin(),e.end()),fl=0;for(int i=1;i<=m;++i){cin>>a[i]>>x;if(e[a[i]]&&e[a[i]]!=x)fl=1;e[a[i]]=x;}if(fl)return cout<<0<<endl,void();sort(a+1,a+m+1),m=unique(a+1,a+m+1)-a-1;
		ans=ksm(k,2*(a[1]-1+(n-a[m])));for(int i=1;i<m;++i)x=a[i+1]-a[i],ans=1ll*(ksm(k,2*x)-(k-1ll)*ksm(k,x-1)%mod+mod)*ans%mod;cout<<ans<<endl;
	}
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;for(ios::sync_with_stdio(0),cin.tie(0),cout.tie(0),cin>>T;T--;)Main::main();
	return 0;
}
