#include<bits/stdc++.h>
#define ll long long
#define il inline
#define vd void
#define bl bool
#define fr(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout)
using namespace std;
const ll N=1e5+4,P=998244353;
ll T,n,m,v,i,x,y,s,ans;
ll a[N];
// ll f[N][2];
bl bz;
il ll R()
{
    ll n=0,f=1;
    char c=getchar();
    for(;(c<'0'||c>'9')&&c!='-';c=getchar());
    if(c=='-') f=-1,c=getchar();
    for(;c>='0'&&c<='9';c=getchar()) n=(n<<1)+(n<<3)+c-'0';
    return n*f;
}
il vd W(ll x)
{
    if(x<0) putchar('-'),x=-x;
    if(x>9) W(x/10);
    putchar(x%10+'0');
}
il ll qpow(ll x,ll y)
{
    ll s=1;
    while(y)
    {
        y&1&&(s=s*x%P);
        x=x*x%P;
        y>>=1;
    }
    return s;
}
il vd sv(ll x,ll t)
{
    if(x==n)
    {
        if(a[x]) (!t||t==a[x])&&++ans;
        else ++ans;
        return;
    }
    ll i,j,k;
    if(!a[x])
    {
        for(i=1;i<=v;++i)
        {
            for(j=1;j<=v;++j)
            {
                for(k=1;k<=v;++k)
                {
                    if(!t||t==k)
                    {
                        if(i==k) sv(x+1,j);
                        else sv(x+1,0);
                    }
                }
            }
        }
        return;
    }
    if(t&&t!=a[x]) return;
    for(i=1;i<=v;++i)
    {
        for(j=1;j<=v;++j)
        {
            if(i==a[x]) sv(x+1,j);
            else sv(x+1,0);
        }
    }
}
signed main()
{
    fr(assign);
    for(T=R();T--;)
    {
        n=R(),m=R(),v=R();
        memset(a,0,sizeof(a));
        // memset(f,0,sizeof(f));
        for(bz=0,i=1;i<=m;++i)
        {
            x=R(),y=R();
            if(a[x]) bz=1;
            a[x]=y;
        }
        if(bz)
        {
            W(0ll);
            putchar('\n');
            continue;
        }
        ans=0;
        sv(1,0);
        W(ans%P);
        putchar('\n');
    }
    return 0;
}