#include<cstdio>
#include<map>
using namespace std;
const int N=1e6+10,mod=1e9+7,inv2=500000004;
int T,n,m,p,ans;
bool bz;
map<int,int>t;
int mi(int x,int y)
{
    if(!y) return 1;
    if(!(y&1)) return mi(1ll*x*x%mod,y/2);
    return 1ll*x*mi(1ll*x*x%mod,y/2)%mod;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&p);
        bz=0;
        t.clear();
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(t[x]&&t[x]!=y)
				bz=1;
            t[x]=y;
        }
        if(bz)
        {
            printf("0\n");
            continue;
        }
        int ls=0;
        ans=mi(p,n+n-2);
        for(auto i:t)
        {
            if(ls)
            {
                int sum=i.first-ls+1,numm=mi(p,sum);
                ans=(ans-1ll*ans*(p-1)%mod*mi(numm,mod-2)%mod+mod)%mod;
            }
            ls=i.first;
        }
        printf("%d\n",ans);
    }
}
