bool M1;
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define uint unsigned int
#define ll long long
#define ull unsigned int long long
#define db double
#define Pii pair<int,int>
#define Pll pair<ll,ll>
#define fir first
#define sec second
#define pc(x) __builtin_popcount(x)
#define vec vector<int>
#define pb push_back
#define qlr cerr<<"qlr\n"
#define dyh cerr<<"dyh\n"
#define uni(x,y) uniform_int_distribution<int>(x,y)(rng)
#define unl(x,y) uniform_int_distribution<ll>(x,y)(rng)
#define unr(x,y) uniform_real_distribution<db>(x,y)(rng)
#define F(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define UF(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define look_memory cerr<<'\n'<<abs(&M1-&M2)/1024.0/1024<<'\n'
#define look_time cerr<<'\n'<<1.0*clock()/CLOCKS_PER_SEC<<'\n'
mt19937 rng(time(0)^(*new int));
const int INF=0x3f3f3f3f;
const int Mod=1e9+7;
template<typename T>
inline void inc(T &a,T b){
if(b<0) b+=Mod;
a+=b;
if(a>=Mod) a-=Mod;
}
template<typename T>
inline void dec(T &a,T b){
if(b<0) b+=Mod;
a-=b;
if(a<0) a+=Mod;
}
template<typename T>
inline void muc(T &a,T b){
a=a*b%Mod;
}
template<typename T>
inline bool chkmin(T &a,T b){
if(a<=b) return 0;
a=b;
return 1;
}
template<typename T>
inline bool chkmax(T &a,T b){
if(a>=b) return 0;
a=b;
return 1;
}
struct IO{
#define N 1<<22
char buf[N],*p1=buf,*p2=buf;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,N,stdin),p1==p2)?EOF:*p1++)
template<typename T>
void read(T &x){
x=0;char ch;int f=0;
while((ch=gc())<'0'||ch>'9') f|=ch=='-';
while(x=(x<<1)+(x<<3)+(ch^48),(ch=gc())>='0'&&ch<='9');
if(f) x=~x+1;
}
#undef N
}io;
ll T,n,m,K,ans;
Pll a[100010];
ll Pow(ll a,ll b){
a%=Mod;
ll res=1,base=a;
while(b){
if(b&1) res=res*base%Mod;
base=base*base%Mod;
b>>=1;
}
return res;
}
void solve(){
io.read(n),io.read(m),io.read(K);
F(i,1,m) io.read(a[i].fir),io.read(a[i].sec);
sort(a+1,a+m+1);
F(i,2,m) if(a[i].fir==a[i-1].fir&&a[i].sec!=a[i-1].sec) {cout<<"0\n";return;}
ans=Pow(K,2*(a[1].fir-1))*Pow(K,2*(n-a[m].fir))%Mod;
F(i,1,m-1){
ll x=a[i].fir,y=a[i+1].fir;
if(x==y) continue;
muc(ans,(Pow(K,2*(y-x))-Pow(K,y-x-1)*(K-1))%Mod);
}
ans=(ans%Mod+Mod)%Mod;
cout<<ans<<'\n';
}
bool M2;
int main(){
freopen("assign.in","r",stdin);
freopen("assign.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
io.read(T);
while(T--) solve();
look_memory;
look_time;
return 0;
}