#include<bits/stdc++.h>
using namespace std;

const int MOD=1e9+7;

typedef long long ll;

typedef pair<int,int> pii;

const int N=1e6+10;

int t,n,m,v;
map<int,int> a;

pii l[N];

int read(){
    int sym=1,num=0;char cnt=getchar();
    while(cnt<'0'||cnt>'9'){
        if(cnt=='-') sym=-1;
        cnt=getchar();
    }
    while(cnt>='0'&&cnt<='9'){
        num=(num<<3)+(num<<1)+(cnt^48);
        cnt=getchar();
    }
    return sym*num;
}

ll qpow(ll x,ll y){
    ll cnt=1;
    while(y){
        if(y&1) cnt=cnt*x%MOD;
        x=x*x%MOD;y>>=1;
    }
    return cnt;
}

int main(){
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    t=read();
    for(int i=1;i<=t;++i){
        n=read(),m=read(),v=read();
        for(int i=1;i<=m;++i)
            l[i].first=read(),l[i].second=read();
        sort(l+1,l+m+1);
        if(m==1){
            printf("%lld\n",qpow(v,2*n-2));
            continue;
        }
        bool flag=true;ll ans=1,us=0;
        for(int i=1;i<=m;++i){
            if(l[i].first!=l[i-1].first) ++us;
            if(a[l[i].first]&&a[l[i].first]!=l[i].second){
              //  printf("lyd %d %d %d %d 0\n",i,l[i].first,l[i].second,a[l[i].first]);
                flag=false;
                break;
            }
            a[l[i].first]=l[i].second;
            if(l[i].first!=l[i-1].first){
                if(l[i-1].first==0){
                    ans=ans*qpow(v,2*l[i].first-2);
                    continue;
                }
                int len=l[i].first-l[i-1].first;
                ans=(ans*((qpow(v,2*len)-qpow(v,len)+qpow(v,len-1))%MOD))%MOD;
              //  printf("%lld\n",ans);
            }
        }
        if(!flag){
            //for(int i=1;i<=m;++i) a[l[i].first]=0;
            a.clear();
            printf("0\n");
            continue;
        }
        if(us==1){
            printf("%lld\n",qpow(v,2*n-2));
            a.clear();
           // for(int i=1;i<=m;++i) a[l[i].first]=0;
            continue;
        }
        ans=ans*qpow(v,n-l[m].first)%MOD;
        printf("%lld\n",ans);
       // for(int i=1;i<=m;++i) a[l[i].first]=0;
        a.clear();
    }
    return 0;
}
