/**
 *    author:  [itmo] enot110
 *    created: 2024-11-10 16:34:22
**/
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define forn(i, n) for(int i = 0 ; (i) < (n) ; ++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(),a.end()
#define pw(x) (1LL<<(x))

using namespace std;

typedef long long ll;
typedef double dbl;
typedef vector<int> vi;
typedef pair<int, int> pi;

const int inf = 1.01e9;
const dbl eps = 1e-9;

/* --- main part --- */


const int N = 2e5 + 10;
const int mod = 998244353;

int mpow(int a, int b)
{
    int res = 1;
    while (b)
    {
        if (b & 1) res = (res * (ll)a) % mod;
        a = (a * (ll)a) % mod;
        b >>= 1;
    }
    return res;
}

pi a[N];
int n, m, k;
int c[N];
int pos;

int calc(int cnt)
{
    return (mpow(k - 1, cnt) + mpow(mod - 1, cnt) * (ll)(k - 1)) % mod;
}

int go(int l, int r)
{
    int cnt = 0;
    int res = 1;
    int last = l;
    int val = mpow(k * (ll)(k - 1) % mod, mod - 2);
    while (pos < m && a[pos].F < r)
    {
        int l2 = a[pos].F;
        int r2 = a[pos].S;
        pos++;
        res = res * (ll)go(l2, r2) % mod * val % mod;
        cnt += l2 - last + 1;
        last = r2;
    }
    cnt += r - last + 1;
    res = res * (ll)calc(cnt) % mod;
    return res;
}

int main()
{
    #ifdef home
        assert(freopen("in", "r", stdin));
        // assert(freopen("out", "w", stdout));
    #endif
    int tn;
    scanf("%d", &tn);
    forn(tt, tn)
    {
        scanf("%d%d%d", &n, &m, &k);
        forn(i, m)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            a[i] = {min(x, y), max(x, y)};
        }
        forn(i, m) a[i].S *= -1;
        sort(a, a + m);
        forn(i, m) a[i].S *= -1;
        pos = 0;
        int res = go(1, n);
        printf("%d\n", res);
    }




    #ifdef home
        eprintf("time = %d ms\n", (int)(clock() * 1000. / CLOCKS_PER_SEC));
    #endif
    return 0;
}