#include<bits/stdc++.h>
#define ll long long
#define N 100005
#define pll pair<ll,ll>
#define fi first
#define se second
using namespace std;
ll n,m,v,ans,w[N];
const ll mod=1000000007;
map<ll,ll>ma;
map<ll,ll>::iterator it;
vector<pll>ve;
ll read(){char c=getchar();
    ll n=0;
    while(c<'0'||c>'9')c=getchar();
    while(c<='9'&&c>='0')n=(n<<1)+(n<<3)+c-'0',c=getchar();
    return n;
}
ll qpow(ll a,ll b){ll ans=1;
    while(b){
        if(b&1)ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
int main(){ll i,k,j,x,y,t,ji,len,sum=0;bool p;
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    ios::sync_with_stdio(0);
    cout.tie(0);
    t=read();
    while(t--){p=true,ans=0,ma.clear(),ve.clear();
        n=read(),m=read(),v=read();
        for(i=1;i<=m;i++){
            x=read(),y=read();
            if(ma.find(x)!=ma.end()&&ma[x]!=y){
                p=false;
                break;
            }ma[x]=y;
        }
        if(!p){
            cout<<"0\n";
            continue;
        }
        if(ma.size()==1){
            cout<<qpow(v,2*(n-1))<<'\n';
            continue;
        }ji=0;
        for(auto i:ma){
            if(ji)ve.emplace_back(ji,i.first);
            ji=i.first;
        }
        for(i=0;i<ve.size();i++)w[i]=qpow(qpow(v,ve[i].se-ve[i].fi+1),mod-2)*(v-1)%mod;
        ans=(qpow(v,2*(n-1))+(mod-1)*qpow(v,2*(n-1))%mod*w[0]%mod)%mod;
        for(i=1;i<ve.size();i++)ans=(ans*(mod-1)%mod*w[i]%mod+ans)%mod;
        cout<<ans<<'\n';
    }
}