#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
constexpr int N = 100010, MOD = 1000000007;
int T;
int n,m,v;
pair<int,int> asi[N],ass[N];
int ans,cnt;
int qpow(int a,int x)
{
    int ans = 1;
    while(x)
    {
        if(x&1)
        {
            ans = (ans * a)%MOD;
        }
        a = (a*a)%MOD;
        x >>= 1;
    }
    return ans;
}
int mminus(int a)
{
    return -a+(((a-1)/MOD)+1)*MOD;
}
signed main()
{
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>T;
    while(T--)
    {
        ans = 1, cnt = 0;
        cin >> n >> m >> v;
        for(int i = 0; i < m; i++)
        {
            cin >> asi[i].first >> asi[i].second;
        }
        sort(asi, asi+ m);
        bool flag = 0;//crash
        ass[cnt++] = asi[0];
        for(int i = 1; i < m; i++)
        {
            if(asi[i].first == asi[i-1].first)
            {
                if(asi[i].second != asi[i-1].second)
                {
                    flag = true;
                    break;
                }
            }
            else
            {
                ass[cnt++] = asi[i];
            }
        }
        if(flag == true)
        {
            cout << 0 << endl;
            continue;
        }
        ans = (ans * (qpow(v,2*(ass[0].first-1))))%MOD;
        for(int i = 1; i < cnt ; i++)
        {
            int dis = ass[i].first-ass[i-1].first;
            ans=(ans* (qpow(v,2*(dis)) +mminus(qpow(v,dis-1)*(v-1))))%MOD;
        }
        ans = (ans * qpow(v,2*(n-ass[cnt-1].first)))%MOD;
        cout << ans << endl;
    }

    return 0;
}
/*
1
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2

1
10 5 2
2 2
3 2
4 2
7 2
10 2

1
2 1 2
2 2

1
3 2 2
2 2
3 2

1 
3 2 2
1 2 
3 2

1
4 2 2
1 2
4 2

*/