#include <cstdio>
#include <algorithm>
#include <utility>
using ll = long long;
using namespace std;
const int M = 1e5, P = 1e9 + 7;
int n,m,k;
pair<int,int> a[M + 2];
ll qpow(ll a,ll n) {
	ll x=1;
	for(; n; n>>=1,a=a*a%P) if(n&1) x=x*a%P;
	return x;
}
int main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	scanf("%d",&T);
	while(T--) {
		scanf("%d%d%d",&n,&m,&k);
		for(int i=1; i<=m; ++i) scanf("%d%d",&a[i].first,&a[i].second);
		sort(a+1,a+m+1);
		m=unique(a+1,a+m+1)-a-1;
		bool ok=true;
		for(int i=1; i<m; ++i)
			if(a[i].first==a[i+1].first) {
				ok=false;
				break;
			}
		if(!ok) {
			printf("0\n");
			continue;
		}
		ll prod=1;
		for(int i=1; i<m; ++i)
			(prod*=(qpow(k,2*(a[i+1].first-a[i].first))-qpow(k,a[i+1].first-a[i].first-1)*(k-1)%P+P)%P)%=P;
		(prod*=qpow(k,2*(a[1].first-1+n-a[m].first)))%=P;
		printf("%lld\n",prod);
	}
	return 0;
}