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

const int M = 1e5+50,mod = 1e9+7,Nv = 50;
int t,m;
ll n,v,x[Nv];
struct limit{
    int c,d;
}l[M];
bool cmp(limit a,limit b){return a.c < b.c;}
bool A,no,B;
ll fpow(ll base,ll k){
    ll ans = 1;
    while(k){
        if(k&1) ans = (ans*base)%mod;
        base = (base*base)%mod;
        k>>=1;
    }
    return ans;
}
void init(){
    A = B = 1;
    no = 0;
    memset(x,0,sizeof(x));
}
int main()
{
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    scanf("%d",&t);
  //  printf("%lld\n",fpow(2,10));
    while(t--){
        init();
        scanf("%lld%d%lld",&n,&m,&v);
        for (int i = 1;i <= m;i++){
            scanf("%d%d",&l[i].c,&l[i].d);
            if(l[i].d != 1) B = 0;
            if(n <= 12 && v <= 2){
                if(x[l[i].c] && l[i].d != x[l[i].c]) no = 1;
                x[l[i].c] = l[i].d;
            }
            if(l[i].c != i) A = 0;

        }
        if(no){
            printf("0\n");
            continue;
        }
        /*
        if(t == 7){
            printf("%d %d %d\n",n,m,v);
            for (int i = 1;i <= m;i++){
                printf("%d %d\n",l[i].c,l[i].d);
            }
        }
        */
/*
        for (int i = 1;i <= n;i++) printf("%d ",x[i]);
        printf("\n");
*/


        if(m == 1){
            printf("%lld\n",fpow(((v%mod)*(v%mod))%mod,n-1));
            continue;
        }
        if(m == n && A){
            printf("%lld\n",fpow((v*(v-1)+1)%mod,n-1));
            continue;
        }
        if(v <= 2 && n <= 12){
            int ans = 0;
            for (int i = 0;i < (1ll<<(n-1));i++){
                for (int j = 0;j < (1ll<<(n-1));j++){
                    bool tf = 1;
                    for (int k = 0;k < n-1;k++){
                        if(x[k+1] == 0){
                        //    ans++;
                            continue;
                        }
                        int v1 = (((1<<k)&i)?2:1),v2 = (((1<<k)&j)?2:1);
                       // printf("%d %d\n",v1+1,v2+1);
                        if(x[k+1] == v1){
                            if(x[k+2] != 0 && x[k+2] != v2){
                                tf = 0;
                                break;
                            }
                        }
                    }
                    if(tf) ans++;
                }
            }

            printf("%d\n",ans);
            continue;
        }
        else{
            sort(l+1,l+m+1,cmp);
            int cnt = 0;
            for (int i = 2;i <= m;i++){
                if(l[i].c == l[i-1].c+1) cnt++;
            }
            printf("%lld\n",(fpow(((v%mod)*(v%mod))%mod,n-1-cnt)*fpow((v*(v-1)+1)%mod,cnt))%mod);
            /*
            for (int i = 1;i <= m;i++){
                printf("%d %d\n",l[i].c,l[i].d);
            }
            printf("%d %lld %lld\n",cnt,(fpow(((v%mod)*(v%mod))%mod,n-1-cnt)*fpow((v*(v-1)+1)%mod,cnt))%mod,fpow(((v%mod)*(v%mod))%mod,n-1));
            */
            continue;
        }
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}

