#include <algorithm>
#include <fstream>
#include <vector>
#include <bitset>
#include <string>
#include <array>

#define rgall(arr) (arr).begin(),(arr).end()
#define rgcnt(arr,cnt) (arr).begin(),(arr).begin()+(cnt)
#define rgo1(arr,cnt) (arr).begin()+1,(arr).begin()+(cnt)+1

using namespace std;

ifstream cin("assign.in");
ofstream cout("assign.out");

long long fast_pow(long long a,long long b,long long m)
{
	long long c=1;
	while(b)
	{
		if(b&1)
			c=c*a%m;
		a=a*a%m;
		b>>=1;
	}
	return c;
}
long long inverse(long long a,long long p)
{
	return fast_pow(a,p-2,p);
}

vector<pair<long long,long long>> vals;
const long long moder=1e9+7;

int main(int argc,char* argv[])
{
	ios::sync_with_stdio(false);
	int testcnt;
	cin>>testcnt;
	while(testcnt--)
	{
		vals.clear();
		long long cnt,cntv,maxv;
		cin>>cnt>>cntv>>maxv;
		for(int i=0;i<cntv;i++)
		{
			int a,b;
			cin>>a>>b;
			vals.emplace_back(a,b);
		}
		sort(rgall(vals));
		long long answer=fast_pow(maxv,2*(vals[0].first-1+cnt-vals.back().first),moder);
		for(int i=1;i<vals.size();i++)
		{
			long long a=vals[i].first-vals[i-1].first;
			if(!a)
			{
				if(vals[i].second!=vals[i-1].second)
				{
					answer=0;
					break;
				}
			}
			else
				answer=answer*(fast_pow(maxv,2*a,moder)-(maxv-1ll)*fast_pow(maxv,a-1,moder)%moder+moder)%moder;
		}
		cout<<answer<<'\n';
	}
	return 0;
}
