#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N=1e5+4;
const ull MOD=1e9+7;
ull table[N]={1ull},n,m,v;
inline void _fio(void) {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	return;
}
inline void _fc(void) {
	fclose(stdin);
	fclose(stdout);
	return;
}
template<typename T>
inline void rd(T &a) {
	register char c=getchar();
	a=0;
	while(!isdigit(c)) c=getchar();
	while(isdigit(c)) a=(a<<3)+(a<<1)+(c^48),c=getchar();
	return;
}
template<typename T>
inline void wt(T a) {
	if(!a) {
		printf("0\n");
		return;
	}
	ull top=0,books[20]={0};
	while(a) books[++top]=a%10,a/=10;
	while(top) putchar(books[top--]^48);
	putchar('\n');
	return;
}
inline ull qp(const ull &a,ull b) {
	if(b<min(n,100001ull)) return table[b];
	ull t=b&1,k=qp(a,b>>1);
	return ((k%MOD)*(k%MOD)*(t?a:1ll))%MOD;
}
struct node {
	int c,d;
} dt[N];
inline const bool cmp(const node &a,const node &b) {
	return a.c<b.c;
}
void solve(void) {
	ull ans=0;
	rd(n),rd(m),rd(v);
	for(int i=1;i<min(n,100001ull);++i) table[i]=(table[i-1]*v)%MOD;
	for(int i=0; i<m; ++i) rd(dt[i].c),rd(dt[i].d);
	sort(dt,dt+m,cmp);
	for(int i=1; i<m; ++i) {
		if(dt[i].c==dt[i-1].c&&dt[i].d!=dt[i-1].d) {
			printf("0\n");
			return; 
		}
	}
	if(v==1) {
		wt(1);
		return;
	}
	if(m>0&&dt[0].c>1) (ans+=qp(v,dt[0].c))%=MOD;
	for(int i=0; i<m-1; ++i) {
		ull tmp=dt[i+1].c-dt[i].c;
		(ans+=qp(v,tmp-1)*((v*v-v+1)%MOD))%=MOD;
	}
	if(m>0&&dt[m-1].c<n) (ans+=qp(v,n-dt[m-1].c+1))%=MOD;
	wt(ans);
	return;
}
int main(void) {
	_fio();
	int t;
	rd(t);
	while(t--) solve();
	_fc();
	return 0;
}
