#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
namespace io{
template<typename T>
void read(T &x)
{
	x=0;
	int f=1;
	char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-')
		{
			f=-1;
		}
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=(x<<1)+(x<<3)+int(c-'0');
		c=getchar();
	}
	x*=f;
}
template<typename T>
void write(T x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>=10)
	{
		write(x/10);
	}
	putchar('0'+int(x%10));
}}
namespace cyrxdzj{
//#define debug
//#define check_memory
#ifdef check_memory
bool check_memory_st;
#endif
const int MAXM=2e5;
const long long MOD=1e9+7;
int tcnt;
long long n;
int almt_cnt;
long long val_lmt;
struct ALmt
{
	long long pos;
	long long val;
}almt[MAXM+5];
bool operator ==(ALmt a,ALmt b)
{
	return (a.pos==b.pos)&&(a.val==b.val);
}
bool cmp_almt(ALmt a,ALmt b)
{
	if(a.pos==b.pos)
	{
		return a.val<b.val;
	}
	return a.pos<b.pos;
}
#ifdef check_memory
bool check_memory_ed;
#endif
long long qpow(long long a,long long b)
{
	long long res=1;
	while(b)
	{
		if(b&1)
		{
			res=res*a%MOD;
		}
		a=a*a%MOD;
		b>>=1;
	}
	return res;
}
int main()
{
	#ifdef check_memory
	printf("check_memory %lf\n",((double)((&check_memory_st)-(&check_memory_ed)))/1024/1024);
	#endif
	freopen("assign.in","r",stdin);
	#ifndef debug
	freopen("assign.out","w",stdout);
	#endif
	io::read(tcnt);
	while(tcnt--)
	{
		io::read(n);
		io::read(almt_cnt);
		io::read(val_lmt);
		for(int i=1;i<=almt_cnt;i++)
		{
			io::read(almt[i].pos);
			io::read(almt[i].val);
		}
		sort(almt+1,almt+1+almt_cnt,cmp_almt);
		long long ans=1;
		for(int i=1;i<almt_cnt;i++)
		{
			if(almt[i].pos==almt[i+1].pos)
			{
				if(almt[i].val!=almt[i+1].val)
				{
					ans=0;
					break;
				}
				continue;
			}
			long long diff_pos=almt[i+1].pos-almt[i].pos;
			long long full=qpow(val_lmt,2*diff_pos);
			long long minus=qpow(val_lmt,diff_pos-1)*(val_lmt-1)%MOD;
			#ifdef debug
			printf("diff_pos %lld full %lld minus %lld\n",diff_pos,full,minus);
			#endif
			ans=ans*((full-minus+2*MOD)%MOD)%MOD;
		}
		ans=ans*qpow(val_lmt,2*(almt[1].pos-1))%MOD;
		ans=ans*qpow(val_lmt,2*(n-almt[almt_cnt].pos))%MOD;
		io::write(ans);
		putchar('\n');
	}
	return 0;
}}
int main()
{
	return cyrxdzj::main();
}
