#include<bits/stdc++.h>

#define N 400010

#define mod 1000000007

using namespace std;

int t,n,m,V;

struct node
{
    int c,d;
    bool operator == (const node oth) const
    {
        return ((oth.c==c)&&(oth.d==d));
    }
}arr[N];

bool cmp(node a,node b)
{
    if(a.c==b.c) return a.d<b.d;
    return a.c<b.c;
}

int quick_pow(int x,int a)
{
    if(a==0) return 1;
    int c=quick_pow(x,a>>1);
    c=(long long)c*c%mod;
    if(a%2==1)
    {
        c=(long long)c*x%mod;
    }
    return c;
}

int main()
{
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
//    cout<<quick_pow(10,mod+1);
//    cout<<quick_pow(578482625,((long long)2*817800450)%mod);
//    cout<<quick_pow(10,5);

    cin>>t;

    for(int k=1;k<=t;++k)
    {
        cin>>n>>m>>V;
        for(int i=1;i<=m;++i)
        {
            cin>>arr[i].c>>arr[i].d;
        }
        sort(arr+1,arr+m+1,cmp);
        bool check=false;
        for(int i=2;i<=m;++i)
        {
            if(arr[i].c==arr[i-1].c&&arr[i].d!=arr[i-1].d)
            {
                check=true;
                break;
            }
        }
        if(check)
        {
            cout<<0<<'\n';
            continue;
        }
        m=unique(arr+1,arr+m+1)-arr-1;
//        cout<<m<<'\n';
        /*
        for(int i=1;i<=m;++i)
        {
            cout<<arr[i].c<<' '<<arr[i].d<<'\n';
        }
        */
//        cout<<"Ok"<<'\n';
        int ans=1;
        ans=(long long)ans*quick_pow(V,2*(arr[1].c-1))%mod;
//        cout<<ans<<'\n';
        for(int i=1;i<=m-1;++i)
        {
            int p=arr[i+1].c-arr[i].c;
            int p1=quick_pow(V,2ll*p);
            int p2=(long long)quick_pow(V,p-1)*(V-1)%mod;
            int delta_ans=(p1-p2+mod)%mod;
            ans=(long long)ans*delta_ans%mod;
//            cout<<ans<<'\n';
        }
        ans=(long long)ans*quick_pow(V,2*(n-arr[m].c))%mod;
        cout<<ans<<'\n';
    }

    return 0;
}


