#include #include #include #define ll long long using namespace std; template void read(T &x) { x = 0; char c = getchar(); int f = 0; for (; !isdigit(c); c = getchar()) f |= c == '-'; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ '0'); if (f) x = -x; } template void write(T x, char ed = '\n') { if (x < 0) putchar('-'), x = -x; static short st[30], tp; do st[++tp] = x % 10, x /= 10; while (x); while (tp) putchar(st[tp--] | '0'); putchar(ed); } const int P = 998244353; ll inv[205]; int deg[205], ed[205][205], n, m, T; int lim, k; struct mat { ll f[205][205]; mat(void) { memset(f, 0, sizeof(f)); } mat operator * (mat b) { mat t; for (int k = 0;k < lim; ++k) for (int i = 0;i < lim; ++i) for (int j = 0;j < lim; ++j) t.f[i][j] = (t.f[i][j] + f[i][k] * b.f[k][j]) % P; return t; } }; int main() { inv[0] = inv[1] = 1; for (int i = 2;i <= 200; ++i) inv[i] = inv[P % i] * (P - P / i) % P; for (read(T); T; T--) { read(n), read(m), read(k), lim = 2 * n; memset(ed, -1, sizeof(ed)); memset(deg, 0, sizeof(deg)); for (int i = 1, x, y, v;i <= m; ++i) read(x), read(y), read(v), ed[x][y] = ed[y][x] = v, ++deg[x], ++deg[y]; mat X; for (int s = 0;s <= 1; ++s) for (int j = 1;j <= n; ++j) for (int t = 1;t <= n; ++t) if (ed[j][t] != -1) X.f[j + n * s - 1][t + (s ^ ed[j][t]) * n - 1] = inv[deg[j]]; mat res = X; --k; for (; k; k >>= 1, X = X * X) if (k & 1) res = res * X; write(res.f[0][lim - 1]); } return 0; }