#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x){
    x=0;
    char ch=getchar();
    while(!isdigit(ch))
        ch=getchar();
    while(isdigit(ch)){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
}
const int N=2e5+7;
const long long Mod=1e9+7;
int T,n,m,v,V,num[N];
struct intt{
    int num,x;
}s[N];
bool cmp(intt a,intt b){
    return a.num<b.num;
}
int fpow(int a,int b){
    int res=1;
    while(b){
        if(b&1)
            res=1ll*res*a%Mod;
        a=1ll*a*a%Mod;
        b>>=1;
    }
    return res;
}
int main(){
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    scanf("%d",&T);
    while(T--){
        read(n),read(m),read(v);
        V=1ll*v*v%Mod;
        for(int i=1;i<=m;++i)
            read(s[i].num),read(s[i].x);
        sort(s+1,s+m+1,cmp);
        bool fl=true;
        int now=0;
        for(int i=1;i<=m;++i){
            if(i>1&&s[i].num==s[i-1].num&&s[i].x!=s[i-1].x){
                fl=false;
                break;
            }
            if(i==1||s[i].num!=s[i-1].num)
                num[++now]=s[i].num;
        }
        if(!fl){
            printf("0\n");
            continue;
        }
        int ans=fpow(V,num[1]-1);
        for(int i=2;i<=now;++i)
            if(num[i]-1==num[i-1])
                ans=1ll*ans*(1+1ll*v*(v-1)%Mod)%Mod;
            else
                ans=1ll*ans*(fpow(V,num[i]-num[i-1])+Mod-1ll*fpow(v,num[i]-num[i-1]-1)*(v-1)%Mod)%Mod;
        ans=1ll*ans*fpow(V,n-num[now])%Mod;
        printf("%d\n",ans);
    }
    return 0;
}
