#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define _ 100005
ll T,n,m,v,a[_],b[_],u[_],p[_],ans;
const ll mod=1e9+7;
map<ll,ll> mp;
ll ksm(ll a,ll b) {
    ll s=1;
    while(b) {
        if(b&1) s*=a,s%=mod;
        a*=a,a%=mod;
        b>>=1;
    }
    return s;
}
ll check() {
    for(ll i=1;i<=n;i++) p[i]=u[i];
    for(ll i=1;i<=n;i++) {
        if(p[i]!=0) {
            if(p[i]==a[i]) {
                if(p[i+1]!=b[i]&&p[i+1]!=0) return 0;
                p[i+1]=b[i];
            }
        }
    }
    return 1;
}
void dfs(ll x) {
    if(x>=n) {
        if(check()) ans++;
        return ;
    }
    for(a[x]=1;a[x]<=v;a[x]++) {
        for(b[x]=1;b[x]<=v;b[x]++) dfs(x+1);
    }
}
void solve() {
    mp.clear();
    ll bj=0;ans=0;
    cin>>n>>m>>v;
    if(n<=12&&m<=12&&v<=2) {
        memset(u,0,sizeof(u));
        for(ll i=1,x,y;i<=m;i++) {
            cin>>x>>y;
            if(mp.count(x)) {
                if(mp[x]!=y) bj=1; 
            }
            else {
                mp[x]=y,u[x]=y;
            }
        }
        if(bj) {cout<<0<<'\n';return ;}
        dfs(1);
        cout<<ans<<endl;
        return ;
    }
    for(ll i=1,x,y;i<=m;i++) {
        cin>>x>>y;
        // cerr<<x<<' '<<v<<endl;
        if(mp.count(x)) {
            if(mp[x]!=y) bj=1; 
        }
        else {
            if(mp.count(x-1)) ans++;
            if(mp.count(x+1)) ans++;
            mp[x]=y;
        }
    }
    // cerr<<ans<<endl;
    if(bj) {cout<<0<<'\n';return ;}
    cout<<ksm(v*v%mod,n-1-ans)*ksm((v-1)*v%mod+1,ans)%mod<<'\n';
}
int main() {
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>T;
    while(T--) solve();
    return 0;
}