#include<bits/stdc++.h>
using namespace std;
#define fo(a,b,c) for(int a=b;a<=c;a++)
#define go(a,b,c) for(int a=b;a>=c;a--)
typedef long long ll;
typedef pair<int,int> pii;
const ll P=1e9+7;
const int MAX=2e6+10,MC=1e4+5;
int n,m;
ll f[MAX],v[MAX],choose[MC][MC],inv[MAX],v_[MAX];
vector<int> s;
map<int,int> mp;
void prework()
{
	choose[0][0]=1;
	choose[1][0]=choose[1][1]=1;
	fo(i,2,5)
	{
		choose[i][0]=1;
		fo(j,0,i)
		{
			choose[i][j]=(choose[i-1][j]+choose[i-1][j-1])%P;
//			cout<<choose[i][j]<<' ';
		}
//		cout<<endl;
	}
}
void baoli()
{
	mp.clear();
	bool fg=1;
	s.clear();
	fo(i,1,m)
	{
		int c,d;
		cin>>c>>d;
		if(mp.count(c))
		{
			if(mp[c]!=d)
			{
				fg=0;
			}
		}
		else
		{
			mp[c]=d;
			s.push_back(c);
		}
	}
	if(fg==0)
	{
		cout<<0<<'\n';
		return;
	}
	sort(s.begin(),s.end());
	int cnt=0;
	fo(i,0,(int)s.size()-2)
	{
		if(s[i]+1==s[i+1])
		{
			cnt++;
		}
	}
	fo(i,2,2*n-2)
	{
		v[i]=v[i-1]*v[1]%P;
	}
	v_[1]=v[1]-1;
	fo(i,2,n)
	{
		v_[i]=v_[i-1]*v_[1]%P;
	}
	ll sum=0,l=1;
//	cout<<cnt;
	fo(i,0,cnt)
	{
		sum=(sum+choose[cnt][i]*v[2*(n-1-i)]%P*v_[i]%P*l+P)%P;
//		cout<<choose[cnt][i]<<' '<<v[2*(n-1-i)]<<' '<<v_[i]<<endl;
		if(sum<0)
			sum+=P;
		l=-1;
	}
	cout<<(sum)%P<<'\n';
}
void solve()
{
	cin>>n>>m>>v[1];
//	if(n<=100000)
//	{
		baoli();
//	}
}
int main()
{
	ios::sync_with_stdio(false);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	prework();
	v[0]=v_[0]=1;
	int t=1;
	cin>>t;
	while(t--)
		solve();
}
