因为时间, 因此可以考虑按照时间

定义状态表示表示时刻为并且到达位置的方案数

因为当前时刻只能从上一个时刻转移过来, 可以使用滚动数组优化, 注意是一个位置而是一个前缀, 行和列要分开处理

第一次有方案数的时候说明就是最短时间, 对于每个一起累计答案就是最终的方案数

#include <bits/stdc++.h>

#define x first
#define y second
#define all(x) x.begin(), x.end()
#define vec1(T, name, n, val) vector<T> name(n, val)
#define vec2(T, name, n, m, val) vector<vector<T>> name(n, vector<T>(m, val))
#define vec3(T, name, n, m, k, val) vector<vector<vector<T>>> name(n, vector<vector<T>>(m, vector<T>(k, val)))
#define vec4(T, name, n, m, k, p, val) vector<vector<vector<vector<T>>>> name((n), vector<vector<vector<T>>>((m), vector<vector<T>>((k), vector<T>((p), (val)))))

using namespace std;
using i128 = __int128;
using u128 = unsigned __int128;
using LL = long long;
using LD = long double;
using ULL = unsigned long long;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using PLD = pair<LD, LD>;

const int N = 1e5 + 10, MOD = 998244353;
const int INF = 1e9;
const LL LL_INF = 2e18;
const LD EPS = 1e-8;
const int dx4[] = {-1, 0, 1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy8[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int hx[] = {-2, -2, -1, -1, 1, 1, 2, 2}, hy[] = {-1, 1, -2, 2, -2, 2, -1, 1};

istream& operator>>(istream& is, i128& val) {
    string str;
    is >> str;
    val = 0;
    bool flag = false;
    if (str[0] == '-') flag = true, str = str.substr(1);
    for (char& c : str) val = val * 10 + c - '0';
    if (flag) val = -val;
    return is;
}

ostream& operator<<(ostream& os, i128 val) {
    if (val < 0) os << "-", val = -val;
    if (val > 9) os << val / 10;
    os << static_cast<char>(val % 10 + '0');
    return os;
}

bool cmp(LD a, LD b) {
    if (fabs(a - b) < EPS) return 1;
    return 0;
}

LL qpow(LL a, LL b) {
    LL ans = 1;
    a %= MOD;
    while (b) {
        if (b & 1) ans = ans * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return ans;
}

void solve() {
    int n, m, q, k;
    cin >> n >> m >> q >> k;

    vector<vector<PII>> nt(k + 1);

    for (int i = 0; i < q; ++i) {
        int x, y, t;
        cin >> x >> y >> t;
        if (t <= k) nt[t].push_back({x, y});
    }

    vec2(LL, f, n + 1, m + 1, 0);
    f[1][1] = 1;
    vec2(int, vis, n + 1, m + 1, 0);

    LL mint = -1;
    LL tot = 0;
    for (int t = 1; t <= k; ++t) {
        for (auto& [x, y] : nt[t]) {
            vis[x][y] = 1;
            f[x][y] = 0;
        }

        vec2(LL, g, n + 1, m + 1, 0);

        // 分开写前缀和优化, 分别从上方和左侧转移过来
        for (int i = 1; i <= n; ++i) {
            LL pre = 0;
            for (int j = 1; j <= m; ++j) {
                if (vis[i][j]) pre = 0;
                else {
                    g[i][j] = (g[i][j] + pre) % MOD;
                    pre = (pre + f[i][j]) % MOD;
                }
            }
        }

        for (int j = 1; j <= m; ++j) {
            LL pre = 0;
            for (int i = 1; i <= n; ++i) {
                if (vis[i][j]) pre = 0;
                else {
                    g[i][j] = (g[i][j] + pre) % MOD;
                    pre = (pre + f[i][j]) % MOD;
                }
            }
        }

        f = g;

        if (f[n][m] > 0) {
            if (mint == -1) {
                mint = t;
            }
            tot = (tot + f[n][m]) % MOD;
        }
    }

    if (mint == -1) {
        cout << -1 << '\n';
        return;
    }

    cout << tot % MOD << ' ' << mint << '\n';

/**/ #ifdef LOCAL
    cout << flush;
/**/ #endif
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int T;
    cin >> T;
    while (T--) solve();
    cout << fixed << setprecision(15);

    return 0;
}