#include "testlib.h"
#include <string>
#include <vector>
using namespace std;

int main(int argc, char* argv[]) {
    registerTestlibCmd(argc, argv);

    int T = inf.readInt();
    for (int tc = 1; tc <= T; ++tc) {
        int N = inf.readInt();
        long long K = inf.readLong();

        // 从标准答案和选手输出各读一个 verdict
        string ans_verdict = ans.readToken();
        string ouf_verdict = ouf.readToken();

        // 若 ans 为 Yes，还需读掉第二行移动序列
        if (ans_verdict == "Yes") {
            ans.readToken();
        }

        if (ouf_verdict != ans_verdict) {
            quitf(_wa, "Test case %d: expected '%s', found '%s'",
                  tc, ans_verdict.c_str(), ouf_verdict.c_str());
        }

        if (ouf_verdict == "Yes") {
            long long total = 1LL * N * N - 1;
            string moves = ouf.readToken();
            if ((long long)moves.size() != total) {
                quitf(_wa, "Test case %d: expected %lld moves, got %d",
                      tc, total, (int)moves.size());
            }

            // 模拟路径，检查合法性
            vector<vector<bool>> vis(N + 1, vector<bool>(N + 1, false));
            int r = 1, c = 1;
            vis[1][1] = true;
            long long cntR = 0;
            for (long long i = 0; i < total; ++i) {
                char ch = moves[i];
                if (ch == 'L') c--;
                else if (ch == 'R') { c++; cntR++; }
                else if (ch == 'U') r--;
                else if (ch == 'D') r++;
                else quitf(_wa, "Test case %d: invalid move '%c' at position %lld",
                           tc, ch, i + 1);

                if (r < 1 || r > N || c < 1 || c > N)
                    quitf(_wa, "Test case %d: out of grid at move %lld", tc, i + 1);
                if (vis[r][c])
                    quitf(_wa, "Test case %d: revisit cell (%d,%d) at move %lld",
                          tc, r, c, i + 1);
                vis[r][c] = true;
            }
            if (r != N || c != N)
                quitf(_wa, "Test case %d: end at (%d,%d), expected (%d,%d)",
                      tc, r, c, N, N);
            if (cntR != K)
                quitf(_wa, "Test case %d: number of R moves = %lld, expected %lld",
                      tc, cntR, K);
        } else if (ouf_verdict == "No") {
            // 与标准答案一致，直接通过
        } else {
            quitf(_wa, "Test case %d: first token must be Yes or No, found '%s'",
                  tc, ouf_verdict.c_str());
        }
    }

    quitf(_ok, "All test cases passed");
    return 0;
}
