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

#define ll long long

const ll mod=1e9+7;
const int maxn=1e5+5;

struct node{int c,d;}ak[maxn];

int n,m,v;

ll ans;
ll f[maxn],g[maxn];

inline int read()
{
    int f=1,sum=0;
    char c=getchar();
    while(c!='-'&&(c<'0'||c>'9')) c=getchar();
    if(c=='-') f=-1;
    while('0'<=c&&c<='9') sum=(sum<<3)+(sum<<1)+c-'0',c=getchar();
    return f*sum;
}

inline bool cmp(node a,node b){return a.c<b.c;}
inline void clr()
{
    memset(f,0,sizeof(f));
    memset(g,0,sizeof(g));
    memset(ak,0,sizeof(ak));
    ans=n=m=v=0;
}
inline int dis(int x,int y){return ak[y].c-ak[x].c;}
inline ll ksm(ll x,ll y)
{
    ll sum=1;
    for(;y;y/=2,x=x*x%mod) if(y&1) sum=sum*x%mod;
    return sum;
}

int main()
{
    // freopen("assign3.in","r",stdin);
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    int _;
    scanf("%d",&_);
    while(_--)
    {
        clr();
        scanf("%d%d%d",&n,&m,&v);
        for(int i=1;i<=m;i++) ak[i].c=read(),ak[i].d=read();
        sort(ak+1,ak+m+1,cmp);
        bool flg=1;
        f[1]=0;
        g[1]=ksm(v,ak[1].c-1)*ksm(v,ak[1].c-1)%mod;
        for(int i=2;i<=m;i++)
        {
            if(ak[i].c==ak[i-1].c&&ak[i].d!=ak[i-1].d) {flg=0;break;}
            else if(ak[i].c==ak[i-1].c) {f[i]=f[i-1],g[i]=g[i-1];continue;}
            ll res1=ksm(v,dis(i-1,i));
            f[i]=(f[i-1]*res1%mod*res1%mod+g[i-1]*ksm(v,dis(i-1,i)-1)%mod*(v-1)%mod)%mod;
            ll res2=ksm(v,dis(0,i)-1);
            g[i]=(res2*res2%mod-f[i]+mod)%mod;
        }
        if(!flg) {printf("0\n");continue;}
        ans=g[m]*ksm(v,n-ak[m].c)%mod*ksm(v,n-ak[m].c)%mod;
        printf("%lld\n",ans);
    }
}