#include<bits/stdc++.h>
using namespace std;
const int MOD=1000000007;
int T,n,m,v,c[114514],d[114514],x[114514];
map<long long,int>must;
void dfs(int step)
{
    if(step==n)
    {
        ;
    }
    if(must[step]) x[step]=must[step];
    else x[step]=1;
    dfs(step+1);
    if(must[step]) x[step]=must[step];
    else x[step]=2;
    dfs(step+1); 
}
int main()
{
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    cin>>T;
    while(T--)
    {
        int ans=0,flag=0;
        cin >> n >> m >> v;
        for(int i=1;i<=m;i++)
        {
            cin >> c[i]>>d[i];
            if(must[c[i]]) flag=1;
            must[c[i]]=d[i];
        }
        if(flag)
        {
            cout<<0<<endl;
            continue;
        }
        if(v==2&&m==1)
        {
            cout<<int(pow(n-1,4))%MOD;
        }
    }

    return 0;
}
