#include <bits/stdc++.h>

#define int long long

using namespace std;

#define fileio(x,y) freopen(x,"r",stdin),freopen(y,"w",stdout)
#define tup tuple<int,int,int>
#define pii pair<int,int>
#define pb emplace_back
#define i12 __int128_t
#define mt make_tuple
#define mp make_pair

const int   maxn=1e5+10;
const int   mod=1e9+7;

int power(int a,int b,int p) { int tar=1; for(; b; b>>=1,a=1ll*a*a%p) if(b&1) tar=1ll*tar*a%p; return tar; }

map<int,int>    pos;
int             n,m,v;

int g(int x) { return ((power(v,x,mod)+power(v,2*x+2,mod)-power(v,x+1,mod))%mod+mod)%mod; }

void work()
{
    /* Code */
    pos.clear();
    cin>>n>>m>>v;
    // g[0]=v*(v-1)+1; for(int i=1; i<maxn; i++) g[i]=v*g[i-1]%mod+(v-1)*power(v,2*i+1,mod)%mod;
    for(int i=1,x,y; i<=m; i++)
    {
        cin>>x>>y;
        if(pos[x]&&pos[x]!=y) return (void)(cout<<0<<'\n');
        pos[x]=y;
    }
    int lst=0,tar=1;
    for(pii it:pos)
    {
        // cerr<<it.first<<' '<<lst<<'\n';
        if(!lst) tar=power(v,2*(it.first-1),mod);
        else (tar*=g(it.first-lst-1))%=mod;
        lst=it.first;
    }
    // cerr<<'\n';
    (tar*=power(v,2*(n-lst),mod))%=mod;
    cout<<tar<<'\n';
    return;
}

signed main()
{
    fileio("assign.in","assign.out");
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t,stTime=clock();
    cin>>t;
    while(t--) work();
    // cerr<<"Time : "<<(double)(clock()-stTime)/CLOCKS_PER_SEC<<'\n';
    return 0;
}