#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll P=1e9+7;
const int N=1e6+7;
ll qpow(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1) ans=ans*a%P;
        a=a*a%P,b>>=1;
    }
    return ans%P;
}
struct node{
    int c,d;
}a[N];
bool cmp(node A,node B){
    if(A.c!=B.c) return A.c<B.c;
    return A.d<B.d;
}
int T,m;
ll n,v,ans,las;
int main(){
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    scanf("%d",&T);
    while(T--){
        las=0,ans=1;
        scanf("%lld%d%lld",&n,&m,&v);
        for(int i=1;i<=m;i++) scanf("%d%d",&a[i].c,&a[i].d);
        sort(a+1,a+m+1,cmp);
        // cerr<<"Here!"<<endl;
        ans=ans*qpow(v,2*(a[1].c-1))%P,las=a[1].c;
        for(int i=2;i<=m;i++){
            if(a[i].c==a[i-1].c&&a[i].d!=a[i-1].d){ans=0;break;}
            else if(a[i].c==a[i-1].c) continue;
            ans=ans*((qpow(v,2*(a[i].c-las))-(qpow(v,a[i].c-las-1)*(v-1)%P)+P)%P)%P;
            las=a[i].c;
        }
        if(las!=n) ans=ans*qpow(v,2*(n-las))%P;
        printf("%lld\n",ans);
    }
}