#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
#define M 100005
#define ll long long
using namespace std;

const int p = 1e9+7;

struct Matrix {
	ll a11, a12, a21, a22;
	Matrix(ll a, ll b, ll c, ll d) {
		a11=a%p, a12=b%p, a21=c%p, a22=d%p;
	}
	Matrix operator*(const Matrix &o) const {
		return Matrix((a11*o.a11%p+a12*o.a21%p)%p, (a11*o.a12%p+a12*o.a22%p)%p, 
						(a21*o.a11%p+a22*o.a21%p)%p, (a21*o.a12%p+a22*o.a22%p)%p);
	}
};

Matrix qpow(Matrix ma, long long b) {
	Matrix res = Matrix(1, 0, 0, 1);
	for(; b; b>>=1, ma=ma*ma)
		if(b&1)	res = ma * res;
	return res;
}

int T, n, m, v;
map<int, int> val;
vector<int>	q;

inline void solve() {
	q.clear();
	val.clear();
	scanf("%d%d%d", &n, &m, &v);
	v %= p;
	Matrix m1 = Matrix(0, 0, 1ll*v*v, 1ll*v*v-v+1);
	Matrix m2 = Matrix(1ll*v*v, 1ll*v*v-v, 0, v);
	int flag = 1;
	for(int i=1, c, d; i<=m; ++i) {
		scanf("%d%d", &c, &d);
		if(!val[c])	q.push_back(c), val[c]=d;
		else if(val[c] != d)	flag = 0;
	}
	if(!flag) {
		printf("0\n");
		return;
	}
	sort(q.begin(), q.end());
	Matrix dp = Matrix(val[1]==0, 0, val[1]!=0, 0);
	int lp = 1;
	for(auto now : q)
		if(now > 1)	dp=m1*qpow(m2, now-1-lp)*dp, lp=now;
	if(lp < n)	dp = qpow(m2, n-lp)*dp;
	printf("%lld\n", (dp.a11+dp.a21)%p);
}

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

