#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
const int MAXN=1e5+5;
const long long mod=1e9+7;
int t;
int n, m, v;
int a[MAXN], c[MAXN], d[MAXN];
void init() {
n=m=v=0;
memset (a, 0, sizeof a);
memset (d, 0, sizeof d);
memset (c, 0, sizeof c);
}
long long qpow (long long x, long long p) {
x%=mod;
long long ans=1;
while (p) {
if (p&1) ans=ans*x%mod;
x=x*x%mod;
p>>=1;
}
return ans;
}
namespace C {
void solve() {
printf("%lld\n", qpow ((long long)v, (long long)2*(n-1)));
}
}
namespace A {
void solve() {
long long bv=(long long)v*v-v+1;
bv%=mod;
long long ans=qpow (bv, (long long)(n-1));
printf("%lld\n", ans);
}
}
namespace B {
void solve() {
std::map <int, bool> mp;
mp.clear();
long long ans=1;
for (int i=1; i<=m; i++) {
mp[c[i]]=true;
}
int SZ=mp.size();
int e[SZ+5];
memset (e, 0, sizeof e);
int cnt=1;
for (std::map <int, bool>::iterator it=mp.begin(); it!=mp.end(); it++, cnt++) {
e[cnt]=it->first;
}
for (int i=2; i<=SZ; i++) {
ans*=qpow ((long long)v, (long long)2*(e[i]-e[i-1]))%mod-qpow ((long long)v-1, (long long)e[i]-e[i-1]-1)%mod;
ans=(long long)(ans+2*mod)%mod;
}
long long pre=e[1]-1, post=n-e[SZ];
pre*=2;
pre%=mod;
post*=2;
post%=mod;
ans*=qpow ((long long)v, pre)%mod;
ans%=mod;
ans*=qpow ((long long)v, post)%mod;
ans%=mod;
printf("%lld\n", ans);
}
}
int main () {
freopen ("assign.in", "r", stdin);
freopen ("assign.out", "w", stdout);
scanf("%d", &t);
while (t--) {
init();
bool flagA=true, flagB=true, flag=false;
scanf("%d%d%d", &n, &m, &v);
if (n>=MAXN) {
for (int i=1; i<=m; i++) {
int x, y;
scanf("%d%d", &x, &y);
}
printf("0\n");
continue;
}
for (int i=1; i<=m; i++) {
scanf("%d%d", &c[i], &d[i]);
if (a[c[i]]&&a[c[i]]!=d[i]) {
flag=true;
break;
}
else a[c[i]]=d[i];
if (c[i]!=i) flagA=false;
if (d[i]!=1) flagB=false;
}
if (flag) {
printf("0\n");
continue;
}
else if (m<=1) C::solve();
else if (m==n&&flagA) A::solve();
else if (flagB) B::solve();
else C::solve();
}
return 0;
}