#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <map>
#include <unordered_map>

#define int long long
#define LL long long

using namespace std;
char buf[1 << 21], *p1 = buf, *p2 = buf;
char gc()
{
	if(p1 == p2){p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin);}
	return *(p1) ++;
}
LL read()
{
	LL x = 0, s = 1;
	scanf("%lld", &x); return x;
	char ch = gc();
	while(ch < '0' || ch > '9'){s = ch == '-' ? -1 : s; ch = gc();}
	while('0' <= ch && ch <= '0'){x = (x << 1ll) + (x << 3ll) + (ch ^ 48ll); ch = gc();}
	return x * s;
}

const int M = 1e5 + 10;
//const int V = 1e9 + 10;
const int mod = 1e9 + 7;

int n, m, v;
struct LIM{int c, d;}vlm[M]; 

bool cmpl(LIM x, LIM y){return x.c < y.c;}
vector<int> lim;
int ans;
int pre[M], suf[M];
int qpw(int d, int u)
{
	int res = 1;
	while(u)
	{
		if(u & 1){res = res * d % mod;}
		d = d * d % mod; u >>= 1;
	}
	return res;
}
void inc(int &x, int y)
{
	x = x + y;
	while(x < 0){x += mod;}
	while(x >= mod){x -= mod;}
}

void init()
{
	ans = 0;
	lim.clear(); 
	memset(pre, 0, sizeof pre); memset(suf, 0, sizeof suf);
}

int qid;
void solve()
{
	init();
	n = read(); m = read(); v = read();
	for(int i = 1; i <= m; i ++){vlm[i] = {read(), read()};}
	
	sort(vlm + 1, vlm + 1 + m, cmpl);
	for(int i = 1; i <= m; i ++)
	{
		if(i == 1){lim.push_back(vlm[i].c);}
		else
		{
			if(vlm[i - 1].c != vlm[i].c){lim.push_back(vlm[i].c);}
			else if(vlm[i - 1].d != vlm[i].d){puts("0"); return ;}
		}
	}
	
	int all = qpw(v * v % mod, n - 1);
	if(lim.size() <= 1){printf("%lld\n", all); return ;}//¶¼ºÏ·¨£¬Ìø³ö
	int le = lim.size();
	
//	for(int i = 0; i < le; i ++){cerr << lim[i] << " ";}
//	cerr << "\n";
	
	pre[0] = qpw(v * v, lim[0] - 1);
	for(int i = 0; i < le - 1; i ++)
	{
		inc(pre[i + 1], pre[i] * qpw(v * v, lim[i + 1] - lim[i]) % mod);
//		int tmp = v * qpw(v * v, lim[i + 1] - lim[i] - 1) % mod; 
		inc(pre[i + 1], -(pre[i] * qpw(v, lim[i + 1] - lim[i]) % mod)); 
		
//		cerr << "DEL:" << qpw(v, lim[i + 1] - lim[i] - 1) * v << "\n";
//		cerr << pre[i] << "->" << pre[i + 1] << "\n";
	}
	
	
//	cerr << "PRE:";
//	for(int i = 0; i < le; i ++){cerr << pre[i] << " ";}
//	cerr << "\n";
	
//	cerr << "AFT_PRE\n";
//	suf[le - 1] = qpw(v * v, n - lim[le - 1]);
//	int chk = qpw(v * v, lim[0] - 1) * qpw(v * v, n - lim[le - 1]) % mod;
	for(int i = le - 2; i >= 0; i --)
	{
		int rig = qpw(v, lim[i + 1] - lim[i] - 1);
//		chk = chk * rig % mod;
		inc(suf[i], rig * suf[i + 1] % mod);
		inc(suf[i], rig * (v - 1ll) % mod * qpw(v * v, n - lim[i + 1]));
		
//		cerr << "ANS:" << ans << "+" << suf[i] << "*" << pre[i] << "\n"; 
		inc(ans, suf[i] * pre[i] % mod);
//		cerr << "    =" << ans << "\n";
	}
	
//	cerr << "FAIL:" << ans << "\n"; 
//	cerr << "CHK:" << chk << "," << ans<< "\n";

//	cerr << "ALL:" << all << "\n";
	
	ans = (all - ans + mod) % mod;
	
	printf("%lld\n", ans);
}

signed main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	
	int tii = read(); 
	for(int z = 1; z <= tii; z ++){qid = z; solve();}
}

