#include<cstdio>
#include<utility>
#include<algorithm>
#define MMUL(p,q) (1ll*(p)*(q)%key)
#define MSUB(p,q) (((p)-(q)+key)%key)
#define MMULEQ(p,q) (p=MMUL(p,q))
const int key=1'000'000'007;
namespace fastI{
    const int siz=1<<20;
    char buf[siz],*p=buf,*pend=buf;
    #define gc() ((p==pend&&(pend=(p=buf)+fread(buf,1,siz,stdin)),p==pend?EOF:*p++))
    int inp(){
        int x=0,f=1;
        char c=gc();
        while(c<'0'||c>'9'){
            if(c=='-')f=-1;
            c=gc();
        }
        while(c>='0'&&c<='9'){
            x=x*10+c-'0';
            c=gc();
        }
        return x*f;
    }
    #undef gc
}
using fastI::inp;
#define M_ 100010
int T,n,m,v,ans;
std::pair<int,int> cd[M_];
int fpow(int x,int y){
    int res=1;
    for(;y;y>>=1){
        if(y&1)MMULEQ(res,x);
        MMULEQ(x,x);
    }
    return res;
}
int inv(int x){
    return fpow(x,key-2);
}
int main(){
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    for(int T=inp(),i,x;T;--T){
        n=inp(),m=inp(),v=inp();
        for(i=1;i<=m;++i){
            cd[i].first=inp();
            cd[i].second=inp();
        }
        std::sort(cd+1,cd+m+1);
        ans=fpow(v,(n-1)<<1);
        for(i=2;i<=m;++i){
            if(cd[i].first^cd[i-1].first){
                x=fpow(v,cd[i].first-cd[i-1].first+1);
                MMULEQ(ans,MMUL(MSUB(x,v-1),inv(x)));
            }
            else if(cd[i].second^cd[i-1].second)break;
        }
        printf("%d\n",i<=m?0:ans);
    }
    return 0;
}
