#include<bits/stdc++.h>
using namespace std;

using ll = long long;
#define int ll

const int maxn= 1e5+10, mod = 1e9+7;

ll a[maxn];

ll qpow(ll a, ll p) {
    ll ret =1;
    while(p>0) {
        if(p&1) ret = ret*a%mod;
        a=a*a%mod;
        p>>=1;
    }
    return ret;
}

map<int, int> mp;

main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
    int T;
    cin>>T;
    while(T--) {
        ll n,m,v;
        cin>>n>>m>>v;
        ll ret = qpow(v,2*n-2);
        ll iv= qpow(v, mod-2);
        ll w=1;
        for(int i=1;i<=m;i++) {
            int _;
            cin>>a[i]>>_;
            if(mp[a[i]]&&mp[a[i]]!=_) {
            	ret = 0;
            	goto die;
            }
            mp[a[i]]=_;
        }
        sort(a+1,a+1+m);
        m = unique(a+1, a+1+m) - a -1;
        
        
        for(int i=2;i<=m;i++) {
			w=w*((qpow(v,a[i]-a[i-1])-1+iv)%mod)%mod;
        }
        w=w*qpow(v,2*(n-a[m]))%mod;
        w=w*qpow(v,2*(a[1]-1))%mod;
        ret = qpow(v,a[m]-a[1])*w%mod;
        ret%=mod;
    die:
        ret = (ret+mod)%mod;
        cout<<ret<<endl;
        mp.clear();
    }
}
