#include<bits/stdc++.h>
#define fo(i,x,y) for(int i(x);i<=y;i=-~i)
#define fd(i,x,y) for(int i(x);i>=y;--i)
#define ll long long
using namespace std;
char ST;

const int N=1e6+5,mo=1e9+7;

int T,n,m,v,ans;
struct node{
    int x,y;
}a[N];

int read(){
    char ch(getchar());int nu(0),o(0);
    while(ch<48||ch>57){
        if(ch=='-')o=1;ch=getchar();
    }
    while(ch>=48&&ch<=57){
        nu=(nu<<1)+(nu<<3)+(ch^48);ch=getchar();
    }
    if(o)nu=-nu;
    return nu;
}
int ksm(int x,int y){
    int res(1);
    while(y){
        if(y&1)res=1ll*res*x%mo;y>>=1;x=1ll*x*x%mo;
    }
    return res;
}
void upd(int &x,int y){
    x+=y;if(x>=mo)x-=mo;
}

namespace task1{
    int f[N][2],d[N];
    void solve(){
        fo(i,1,n)d[i]=0;
        fo(i,1,m){
            int x(a[i].x),y(a[i].y);
            if(!d[x])d[x]=y;
            else if(d[x]!=y){
                printf("0\n");return;
            }
        }
        fo(i,0,n)fo(j,0,1)f[i][j]=0;
        f[0][0]=1;
        fo(i,1,n-1){
            //0
            if(!d[i])upd(f[i][0],1ll*f[i-1][0]*v*v%mo);
            else{
                //ai!=di
                upd(f[i][0],1ll*f[i-1][0]*(v-1)%mo*v%mo);
                //ai=di
                if(!d[i+1])upd(f[i][1],1ll*f[i-1][0]*v%mo);
                else upd(f[i][1],f[i-1][0]);
            }
            //1
            //ai!=b_{i-1}
            upd(f[i][0],1ll*f[i-1][1]*(v-1)%mo*v%mo);
            //ai=b_{i-1}
            if(!d[i+1])upd(f[i][1],1ll*f[i-1][1]*v%mo);
            else upd(f[i][1],f[i-1][1]);
        }
        ans=0;
        upd(ans,f[n-1][0]);upd(ans,f[n-1][1]);
        printf("%d\n",ans);
    }
}

void solve(){
    n=read();m=read();v=read();
    fo(i,1,m)a[i]=(node){read(),read()};
    if(m==1){
        printf("%d\n",ksm(v,2*n-2));
        return;
    }
    task1::solve();
}

char EN;
int main(){
    freopen("assign.in","r",stdin);freopen("assign.out","w",stdout);
    // cerr<<(&EN-&ST)/1048576.0<<'\n';
    T=read();
    while(T--)solve();
    return 0;
}
