#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
typedef vector<int> vint;
int n,m,v;
constexpr int mod=1e9+7;
int fastpow(int x,int y){
	int res=1;
	while(y){
		if(y&1){
			res=(res*x)%mod;
		}
		x=(x*x)%mod;
		y>>=1;
	}
	return res;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
	int t;
	cin >> t;
	while(t--){
		cin >> n >> m >> v;
		map<int,int> s;
		bool flag=true;
		while(m--){
			int c,d;
			cin >> c >> d;
			if(s[c]!=0&&s[c]!=d){
				flag=false;
			}
			s[c]=d;
		}
		if(!flag){
			cout << 0 << endl;
			continue;
		}
		int last_pos=-1,cnt=0,j=0;
		for(auto q:s){
			if(q.first==last_pos+1){
				cnt+=1;
			} else {
				j+=2;
			}
			last_pos=q.first;
		}
		cout << fastpow(v,2*n-j) << endl;
	}
	return 0;
}
