#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

typedef long long ll;

const ll mod = 1e9 + 7;

int T;
int n,m;
ll v;
map <int,int> a;
ll ans;
bool fl;

void ini()
{
	a.clear();
	fl = true;
	ans = 0;
}

ll ksm(ll x,int y)
{
	ll res = 1;
	while (y)
	{
		if (y & 1) res = res * x % mod;
		x = x * x % mod;
		y >>= 1;
	}
	return res;
}

void solve()
{
	scanf("%d%d%lld",&n,&m,&v);
	ini();
	for (int i = 1;i <= m;i ++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		if (!a[x]) a[x] = y;
		else if (a[x] != y) fl = false;
	}
	if (!fl)
	{
		printf("0\n");
		return ;
	}
	map <int,int> :: iterator it = a.begin();
	while (it != a.end())
	{
		int l = it -> first;
		if (it == a.begin())
			ans = ksm(v,2 * l - 2);
		it ++;
		if (it == a.end())
		{
			ans = ans * ksm(v,2 * n - 2 * l) % mod;
			break;
		}
		int r = it -> first;
		ans = ans * (ksm(v,2 * r - 2 * l) - ksm(v,r - l - 1) * (v - 1) % mod + mod) % mod;
	}
	printf("%lld\n",ans);
}

int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while (T --) solve();
}
