#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5,mod=1e9+7;
int quick_pow(int n,int k)
{
    int res=1,base=n;
    for(;k>0;k>>=1)
    {
        if((k&1)==1)res=1ll*res*base%mod;
        base=1ll*base*base%mod;
    }
    return res;
}
int x[N],dp[N];
map<int,int>mp;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    int T;
    cin>>T;
    while(T--)
    {
        bool flg=false;
        int n,m,v;
        cin>>n>>m>>v;
        mp.clear();
        for(int i=1,c,d;i<=m;i++)
        {
            cin>>c>>d;
            if(mp[c]!=0&&mp[c]!=d)flg=true;
            mp[c]=d;
        }
        if(flg)
        {
            cout<<"0\n";
            continue;
        }
        int tot=0;
        for(auto[a,b]:mp)x[++tot]=a;
        dp[1]=quick_pow(v*v%mod,x[1]-1);
        for(int i=2;i<=tot;i++)
        {
            int len=x[i]-x[i-1];
            dp[i]=dp[i-1]*(quick_pow(v*v%mod,len)-quick_pow(v,len-1)*(v-1)%mod+mod)%mod;
        }
        dp[tot+1]=dp[tot]*quick_pow(v*v%mod,n-x[tot])%mod;
        cout<<dp[tot+1]<<'\n';
    }
    return 0;
}