#include<cstdio>
#include<cctype>
#include<algorithm>
using namespace std;
const int N=1e5+10,mod=1e9+7;
using ll=long long;
template<typename _Tp>
inline void read(_Tp& x){
    static char ch;
    while(ch=getchar(),!isdigit(ch));
    x=(ch^48);
    while(ch=getchar(),isdigit(ch))x=(x<<3)+(x<<1)+(ch^48);
}
template<typename _Tp,typename ..._Args>
inline void read(_Tp& x,_Args &...args){
    read(x);
    read(args...);
}
inline ll fsp(ll x,ll y){
    ll ret=1;
    while(y){
        if(y&1)ret=ret*x%mod;
        x=x*x%mod;
        y>>=1;
    }
    return ret;
}
int n,m,v;
ll res;
struct st{
    int c,d;
    bool operator<(const st& x)const{
        if(c!=x.c)return c<x.c;
        return d<x.d;
    }
};
st qry[N];
void run(){
    read(n,m,v);
    res=1;
    for(int i=1;i<=m;i++){
        read(qry[i].c,qry[i].d);
    }
    sort(qry+1,qry+m+1);
    res=res*fsp(v,2*(qry[1].c-1))%mod;
    res=res*fsp(v,2*(n-qry[m].c))%mod;
    for(int i=2,tmp=0;i<=m;i++){
        if(qry[i].c==qry[i-1].c){
            if(qry[i].d!=qry[i-1].d){
                res=0;
                break;
            }
            continue;
        }
        tmp=qry[i].c-qry[i-1].c-1;
        res=res*(fsp(v,2*tmp+2)-fsp(v,tmp)*(v-1)%mod+mod)%mod;
    }
    printf("%lld\n",res);
}
int main(){
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    int T=1;
    read(T);
    while(T--)run();
}
/*
9:21 passed all samples.
*/