#include<iostream>
#include<map>
using namespace std;
typedef long long ll;
const int N=1e5+5,p=1e9+7;

inline int read()
{
	int s=0;
	char c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9')s=s*10+c-'0',c=getchar();
	return s;
}

ll c[N],d[N];
map<int,int>a;

int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T=read();
	while(T--)
	{
		a.clear();
		ll n=read(),m=read(),v=read();
		for(int i=1;i<=m;i++)c[i]=read(),d[i]=read();
		for(int i=1;i<=m;i++)a[c[i]]=-1;
		bool b=1;
		for(int i=1;i<=m;i++)
		{
			if(a[c[i]]!=-1 && a[c[i]]!=d[i])
			{
				b=0;
				break;
			}
			else a[c[i]]=d[i];
		}
		if(b==0)cout<<0<<'\n';
		else
		{
			ll ans=n*(v*(v-1)%p+1)%p;
			cout<<ans<<'\n';
		}
	}
	return 0;
}
