#include<bits/stdc++.h>
using namespace std;
const int N=100005,mod=1000000007;
int dp[N];
int ksm(int x,int y){
    int sum=1;
    while(y){
        if(y&1) sum=1ll*sum*x%mod;
        x=1ll*x*x%mod;
        y>>=1;
    }
    return sum;
}
struct node{
    int pl,val;
}mp[N];
bool cmp(node x,node y){
    return x.pl==y.pl?x.val<y.val:x.pl<y.pl;
}
void solve(){
    int n,m,V;
    cin>>n>>m>>V;
    for(int i=1;i<=m;i++) scanf("%d%d",&mp[i].pl,&mp[i].val);
    sort(mp+1,mp+m+1,cmp);
    mp[m+1].pl=-1;
    m=0;
    for(int i=1;mp[i].pl>0;i++){
        if(mp[i].pl!=mp[m].pl||mp[i].val!=mp[m].val) ++m;
        mp[m]=mp[i];
    }
    for(int i=2;i<=m;i++)
        if(mp[i].pl==mp[i-1].pl&&mp[i].val!=mp[i-1].val){
            cout<<0<<endl;
            return;
        }
    dp[1]=ksm(V,2*(mp[1].pl-1));
    for(int i=2;i<=m;i++){
        int x=mp[i].pl-mp[i-1].pl-1;
        int now=(0ll+ksm(V,2*x+2)-ksm(V,x+1)+ksm(V,x)+mod)%mod;
        dp[i]=1ll*dp[i-1]*now%mod;
    }
    cout<<1ll*dp[m]*ksm(V,2*(n-mp[m].pl))%mod<<endl;
}
int main(){
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    int t;
    cin>>t;
    for(;t;--t) solve();
}
