#include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
long long c[500005],p[55],t[55][55],f[500005][2],n,m,v,ans;
void dfs(long long x)
{
    if(x==2*n-1)
    {
        for(long long i=1;i<=n;i+=2)
        {
            if(c[p[i+1]]!=p[i])return ;            
        }
        ans++;
        return ;
    }
    p[x]=1;
    dfs(x+1);
    p[x]=2;
    dfs(x+1);
}
long long qpow(long long a,long long b)
{
    long long res=1;
    while(b)
    {
        if(b&1)res=a*res%mod;
        b>>=1;
        a=a*a%mod;
    }
    return res;
}
void solve()
{
    ans=0;
    long long flag=0;
    scanf("%lld%lld%lld",&n,&m,&v);
    for(long long i=1;i<=n;i++)c[i]=0;
    for(long long i=1;i<=m;i++)
    {
        long long x,y;
        scanf("%lld%lld",&x,&y);
        if(c[x]!=0)flag=1;
        c[x]=y;
    }
    if(flag==1)
    {
        cout<<0<<endl;
        return ;
    }
    if(n<=12)
    {
        dfs(1);
        cout<<ans<<endl;
        return ;
    }
    if(m==1)
    {
        printf("%lld\n",qpow(v,2*n-2));
        return ;
    }
    memset(f,0,sizeof(f));
    f[0][1]=f[0][0]=1;
    for(long long i=1;i<=n;i++)
    {
        if(i==1||c[i-1]==0)
        {
            if(c[i]!=0)f[i][1]=1,f[i][0]=((v-1)*f[i-1][0]%mod);
            else f[i][1]=0,f[i][0]=v*f[i-1][0]%mod;
            continue;
        }
        if(c[i]!=0)
        {
            f[i][0]=f[i-1][0]*v%mod;
            f[i][1]=f[i-1][1]*(v-1)%mod;
        }
        else f[i][0]=(f[i-1][0]*v%mod)+(f[i-1][0]*v)%mod,f[i][1]=0;
    }
    cout<<(f[n][0]+f[n][1])%mod<<endl;
}
int main()
{
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    long long T;
    cin>>T;
    while(T--)solve();
}
