#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 1e9 + 7;
const int maxm = 1e5 + 5;

inline ll read()
{
	char c = getchar();
	ll f = 0,w = 1;
	while(c < '0' or c > '9'){if(c == '-') w = -1;c = getchar();}
	while(c >= '0' and c <= '9'){f = f * 10 + c - '0';c = getchar();}
	return w * f;
}

ll qpow(ll a,ll b)
{
	ll sum = 1;
	while(b)
	{
		if(b & 1)
			sum = sum * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return sum % mod;
}


map <int,int> Map;
struct node
{
	int id,val;
}a[maxm];

ll ans = 1;
int cnt;
int post[maxm];
int cnt_val[maxm];
int T,m,n;
ll v;

bool cmp(node u,node v)
{return u.id < v.id;}

int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
//	cout << qpow(3,9) << endl;
//	a[1].id = 99,a[1].val = 3;
//	a[2].id = 1,a[2].val = 1;
//	sort(a + 1,a + 2 + 1,cmp);
//	cout << a[1].id;
//	return 0;
//	cout << qpow(1ll * 464289274 * 464289274,557584238 - 1) << endl;; 
//	return 0;
	T = read();
	while(T--)
	{
		ans = 1,cnt = 0;
		Map.clear();
		memset(cnt_val,0,sizeof cnt_val);
//		memset(a,0,sizeof a);
//		memset(post,0,sizeof post);
		n = read();
		m = read();
		v = read();
		ll v2 = 1ll * v * v % mod;
		ll v3 = (1ll * v * v - v + 1 + mod) % mod;
//		cout << v2 << "-----" << v3 << endl;
		for(int i = 1;i <= m;i++)
		{
			int u = read(),v = read();
			a[i].id = u;
			a[i].val = v;
		}
		sort(a + 1,a + m + 1,cmp);
		for(int i = 1;i <= m;i++)
		{
			if(!Map[a[i].id])
			{
				Map[a[i].id] = ++cnt;
				post[cnt] = a[i].id;
				cnt_val[cnt] = a[i].val;
			}
			if(cnt_val[Map[a[i].id]] != a[i].val)
			{
				printf("0\n");
				goto Next;
			}
		}
		for(int i = 1;i < cnt;i++)
		{
			if(post[i + 1] - post[i] == 1)
			{
				(ans *= v3) %= mod;
				continue;
			}
			ll opt = qpow(v2,1ll * post[i + 1] - post[i]);
			opt = opt + mod - qpow(v,1ll * post[i + 1] - post[i] - 1) * (v - 1) % mod;
			opt = (opt + mod) % mod;
//			opt = (opt + mod) % mod;
			(ans *= opt) %= mod;
		}
//		printf("%lld %lld ",ans,v2);
		(ans *= qpow(v2,post[1] - 1 + n - post[cnt])) %= mod;
		printf("%lld\n",ans);
		Next:;
	}
	
	return 0;
}
