#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Rev(i,a,b) for(int i=a;i>=b;i--)
#define Fin(file) freopen(file,"r",stdin)
#define Fout(file) freopen(file,"w",stdout)
#define fi first
#define se second
using namespace std; using pii = pair<int,int>;
const int N=2e5+5,mod=1e9+7;
class Int{
    int x;
public:
    explicit Int(int _x=0) : x(_x) {}
    Int& operator+= (Int O) { x=(x+O.x)%mod; return *this; }
    Int& operator-= (Int O) { x=(x-O.x+mod)%mod; return *this; }
    Int& operator*= (Int O) { x=1ll*x*O.x%mod; return *this; }
    friend Int operator+ (Int A,Int B) { return A+=B; }
    friend Int operator- (Int A,Int B) { return A-=B; }
    friend Int operator* (Int A,Int B) { return A*=B; }
    int operator() () const { return x; }
};
Int qpow(Int x,int y){
    Int res(1); while(y) { if(y&1) res*=x;; x*=x; y>>=1; }; return res;
}
int n,m,V; pii a[N];
void Mian(){
    cin>>n>>m>>V; For(i,1,m) cin>>a[i].fi>>a[i].se;; sort(a+1,a+1+m);
    For(i,2,m) if(a[i].fi==a[i-1].fi&&a[i].se!=a[i-1].se) { cout<<"0\n"; return; }
    m=unique(a+1,a+1+m)-(a+1); a[m+1]=pii(n+1,0);
    Int f0(1),f1(0); int p=1;
    For(i,1,m+1){
        if(a[i].fi==1) { f0=Int(0); f1=Int(1); continue; }
        if(a[i].fi!=p+1){
            int k=a[i].fi-p-1; Int x=qpow(Int(V),k);
            Int g1=f1*x, g0=(f0+f1)*x*x-g1;
            f0=g0; f1=g1;
        }
        if(i!=m+1){
            f1 = f0*Int(V)*Int(V) + f1*(Int(V-1)*Int(V)+Int(1)); f0=Int(0);
        }
        p=a[i].fi;
    }
    cout<<(f0+f1)()<<'\n';
}
int main(){
    Fin("assign.in"); Fout("assign.out");
    ios::sync_with_stdio(0); cin.tie(0);
    int Ti; cin>>Ti; while(Ti--) Mian();
    return 0;
}