#include <bits/stdc++.h>
using namespace std;

map <int,int> mp;
long long n,m,k;
long long P = 1e9 + 7;
long long f[100010];
long long a[100010];
long long fst(long long x,long long y)
{
	if (y == 0)
	{
		return 1;
	}
	long long z = fst(x,y / 2);
	if (y % 2 == 0)
	{
		return z * z % P;
	}
	else
	{
		return z * z % P * x % P;
	}
}

int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	cin >> T;
	while (T--)
	{
		int cnt = 0;
		cin >> n >> m >> k;
		int fl = 1;
		for (int i = 1 ; i <= m ; i++)
		{
			int x,y;
			scanf("%d %d",&x,&y);
			if (mp[x] == 0)
			{
				cnt++;
				a[cnt] = x;
			}
			if (mp[x] != 0 && mp[x] != y)
			{
				fl = 0;
			}
			mp[x] = y;
		}
		for (int i = 1 ; i <= cnt ; i++)
		{
			mp[a[i]] = 0;
		}
		if (fl == 0 || n == 1)
		{
			cout << 0 << endl;
			continue;
		}
		sort(a + 1,a + cnt + 1);
		f[1] = 0;
		for (int i = 2 ; i <= cnt ; i++)
		{
			f[i] = (fst(k,(a[i - 1] - 1) * 2) - f[i - 1] + P) % P * (fst(k,a[i] - a[i - 1] - 1) * (k - 1) % P) % P;
			f[i] = f[i] + fst(k,(a[i] - a[i - 1]) * 2) * f[i - 1] % P;
			f[i] %= P;
		}
		long long ans = fst(k,(n - a[cnt]) * 2) % P * (fst(k,(a[cnt] - 1) * 2) - f[cnt] + P) % P;
		printf("%lld",ans);
		cout << endl;
	}
	return 0;
} 
