#include<bits/stdc++.h>
#define pb push_back
#define is insert
#define fi first
#define se second
#define mkp make_pair
#define INF INT_MAX
#define mem(a,b) memset(a,b,sizeof a)
#define cpy(a,b) memcpy(a,b,sizeof b)
#define mathmod(a,m) (((a)%(m)+(m))%(m))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int MOD=1e9+7;
int t,n,m,v;
ll ans;
map<int,int> mp;
vector<int> vec;
ll qpow(ll a,ll b,ll m=MOD){
	ll res=1;
	while(b){
		if(b&1) res=res*a%m;
		a=a*a%m,b>>=1;
	}
	return res;
}
void init(){
	ans=1;
	mp.clear();
	vec.resize(0);
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){
		init();
		cin>>n>>m>>v;
		for(int i=1,c,d;i<=m;i++){
			cin>>c>>d;
			if(mp.count(c)&&mp[c]!=d){
				cout<<"0\n";
				goto here;
			}
			else mp[c]=d;
		}
		for(pii p:mp) vec.pb(p.fi);
		ans=ans*qpow(v,(vec.front()-1+n-vec.back())<<1)%MOD;
		for(int i=1;i<vec.size();i++){
			ans=ans*((qpow(v,(vec[i]-vec[i-1])<<1)-qpow(v,vec[i]-vec[i-1]-1)*(v-1)%MOD)%MOD)%MOD;
		}
		cout<<mathmod(ans,MOD)<<"\n";
		here:;
	}
	cout<<flush;
	return 0;
}


