#include<bits/stdc++.h>

#define int long long
#define mem(x,y) memset(x,y,sizeof(x))
#define rep(i,l,r) for(int i = l;i <= r;i ++)
#define dep(i,l,r) for(int i = l;i >= r;i --)

using namespace std;

int read(){
	int s = 0,w = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9'){if(ch == '-')w = -1;ch = getchar();}
	while(ch >= '0' && ch <= '9')s = s * 10 + ch - '0',ch = getchar();
	return s * w;
}

const int mod = 1e9 + 7;

void Add(int &x,int y){x = (0ll + x + y) % mod;}
void Mul(int &x,int y){x = (1ll * x * y) % mod;}

struct Matrix{
	int a[4][4];
	
	void clear(){mem(a,0);}
	
	void init(){clear();rep(i,1,2)a[i][i] = 1;}
	
	friend Matrix operator * (Matrix x,Matrix y){
		Matrix s;s.clear();
		rep(k,1,2)rep(i,1,2)rep(j,1,2)Add(s.a[i][j],x.a[i][k] * y.a[k][j] % mod);
		return s;
	}
}mat,base;

Matrix poww(Matrix x,int y){
	Matrix s;s.init();
	for(;y;y >>= 1,x = (x * x))if(y & 1)s = (s * x);
	return s;
}

struct node{
	int p;
	int w;
	
	friend bool operator < (node x,node y){return x.p < y.p;}
}e[1000010];

int t;
int n,m,v;
bool fg;

void solve(){
	fg = 0;
	n = read(),m = read(),v = read();
	base.clear();
	base.a[1][1] = v * v % mod;
	base.a[2][1] = v * (v - 1) % mod;
	base.a[1][2] = 0;
	base.a[2][2] = v;
	rep(i,1,m)e[i].p = read(),e[i].w = read();
	sort(e + 1,e + m + 1);
	rep(i,1,m - 1)if(e[i].p == e[i + 1].p && e[i].w != e[i + 1].w){fg = 1;break;}
	if(fg){
		puts("0");
		return ;
	}
	int s0,s1;
	if(e[1].p == 1)s0 = v - 1,s1 = 1;
	else s0 = v,s1 = 0;
	int now = 1;
	rep(i,1,m){
		int p = e[i].p;
		if(p > now + 1){
			int dis = p - now;
			dis --;
			mat.clear();
			mat.a[1][1] = s0,mat.a[1][2] = s1;
			mat = (mat * poww(base,dis));
			now += dis;
			s0 = mat.a[1][1],s1 = mat.a[1][2]; 
		}
		if(p == now + 1){
			int w0,w1;w0 = w1 = 0;
			Add(w0,s0 * (now == n - 1 ? 1 : (v - 1)) % mod * v % mod);
			Add(w1,s0 * v % mod);
			Add(w0,s1 * (now == n - 1 ? 1 : (v - 1)) % mod);
			Add(w1,s1);
		//	cout<<"w0 = "<<w0<<endl;
			s0 = w0,s1 = w1;
			now ++;
		}
	}
//	cout<<"now = "<<now<<endl;
	int p = n;
	if(p > now + 1){
		int dis = p - now;
		dis --;
		mat.clear();
		mat.a[1][1] = s0,mat.a[1][2] = s1;
		mat = (mat * poww(base,dis));
		now += dis;
		s0 = mat.a[1][1],s1 = mat.a[1][2]; 
	}
	if(now + 1 == n){
	//	cout<<"s0 = "<<s0<<",s1 = "<<s1<<endl;
		int w0,w1;w0 = w1 = 0;
		if(e[m].p == n){
			Add(w0,s0 * v % mod);
			Add(w1,s0 * v % mod);
			Add(w0,s1);
			Add(w1,s1);
		}
		else {
			Add(w0,s0 * v % mod);
			Add(w0,s1 * v % mod);
			Add(w1,s1 * v % mod);
		}
		s0 = w0;
	}
	printf("%lld\n",(s0 % mod + mod) % mod);
}

signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout); 
	cin>>t;
	while(t --)solve();
}

/*

1
7 1 2
4 1

1
2 2 2
1 1
2 2


*/
