#include <bits/stdc++.h>
#define RNG(_i,_a,_b) for(int _i=(_a),_i##_END=(_b); _i<=_i##_END; _i++)
#define IRNG(_i,_a,_b) for(int _i=(_a),_i##_END=(_b); _i>=_i##_END; _i--)
using namespace std;
const int MAXN=1e5+10; const long MOD=1e9+7;
long n,k; int m;
pair<long,long> A[MAXN]; long P[MAXN],V[MAXN];
long qpow(long x,long p){
    assert(p>=0);
    long ret=1;
    for(; p; p>>=1,x=x*x%MOD){
        if(p&1) ret=ret*x%MOD;
    }
    return ret;
}
void case_main(){
    cin>>n>>m>>k;
    RNG(i,1,m) cin>>A[i].first>>A[i].second;
    sort(A+1,A+m+1);
    RNG(i,1,m) tie(P[i],V[i])=A[i];
    auto calc=[](long len,bool a,bool b)->long{
//      cerr<<len<<" "<<a<<" "<<b<<"\n";
        if(len<0) return 0;
        if(len==0){
            if(a&&b) return (k*k-k+1)%MOD;
            else return 1;
        }
        if(a&&b) return (qpow(k*k%MOD,len+1)-(qpow(k,len)*(k-1)%MOD)+MOD)%MOD;
        else return qpow(k*k%MOD,len-1+a+b);
    };
    long ans=1;
    RNG(i,1,m){
        if(P[i]==P[i-1]){
            if(V[i]!=V[i-1]){ cout<<0<<"\n"; return; }
        }else ans=ans*calc(P[i]-P[i-1]-1,!!P[i-1],1)%MOD;
    }
    ans=ans*calc(n-P[m],!!P[m],0)%MOD;
    cout<<ans<<"\n";
}
int main(){
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
    int cas; cin>>cas;
    RNG(_,1,cas) case_main();
}
