#include<bits/stdc++.h>
using namespace std;
namespace my_std{
	#define ll long long
	#define bl bool
	#define fr(i,x,y) for(ll i=(x);i<=(y);i++)
	#define pfr(i,x,y) for(ll i=(x);i>=(y);i--)
	#define go(u) for(ll i=head[u];i;i=e[i].nxt)
	#define il inline
	#define db double
	#define pf printf
	#define pc putchar
	#define MP make_pair
	#define fir first
	#define sec second
	#define space pc(' ')
	#define enter pc('\n')
	il ll read(){
		ll sum=0,fu=0;
		char ch=getchar();
		while(!isdigit(ch)){
			if(ch=='-') fu=1;
			ch=getchar();
		}
		while(isdigit(ch)){
			sum=sum*10+(ch-'0');
			ch=getchar();
		}
		return fu?(-sum):sum;
	}
	void write(ll x){
		if(x<0){
			pc('-');
			x=-x;
		}
		if(x>9) write(x/10);
		pc('0'+(x%10));
	}
	il void writesp(ll x){
		write(x);
		space;
	}
	il void writeln(ll x){
		write(x);
		enter;
	}
}
using namespace my_std;
#define mod 1000000007
ll t,n,m,v;
pair<ll,ll> p[100010];
il ll my_pow(ll x,ll y){
	ll res=1;
	while(y){
		if(y&1) res=res*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return res;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	t=read();
	while(t--){
		n=read();
		m=read();
		v=read();
		bl ck=0;
		fr(i,1,m){
			p[i].fir=read();
			p[i].sec=read();
		}
		sort(p+1,p+m+1);
		m=unique(p+1,p+m+1)-p-1;
		fr(i,1,m-1) if(p[i].fir==p[i+1].fir) ck=1;
		if(ck){
			writeln(0);
			continue;
		}
		if(n==1){
			writeln(1);
			continue;
		}
		if(!m){
			writeln(my_pow(v,2*(n-1)));
			continue;
		}
		ll ans=my_pow(v,2*(p[1].fir-1))*my_pow(v,2*(n-p[m].fir))%mod;
		fr(i,2,m){
			ll tmp=my_pow(v,p[i].fir-p[i-1].fir-1)*(v-1)%mod;
			ans=ans*(my_pow(v,2*(p[i].fir-p[i-1].fir))-tmp+mod)%mod;
		}
		writeln(ans);
	}
}
