#include <iostream>
#include <cstdio>
#include "testlib.h"

long long countSmaller(const std::string& T, const std::string& S) {
    long long count = 0;
    int nT = T.length();
    int nS = S.length();

    for (int i = 0; i < nT; ++i) {
        int checkLen = std::min(nS, nT - i);
        std::string subPrefix = T.substr(i, checkLen);

        if (subPrefix < S) {
            count += (nT - i);
        } else if (subPrefix > S) {
            for (int len = 1; len < checkLen; ++len) {
                if (T.substr(i, len) < S) count++;
            }
        } else {
            for (int len = 1; len < static_cast<int>(subPrefix.length()); ++len) {
                if (T.substr(i, len) < S) count++;
            }
        }
    }
    return count;
}
signed main(int argc, char *argv[]) {
    registerTestlibCmd(argc, argv);
    int c, t; c = inf.readInt(), t = inf.readInt();
    while (t--) {
        int n, K; n = inf.readInt(), K = inf.readInt();
        std::string S; S = inf.readToken();
        std::string pOut; pOut = ouf.readToken();
        std::string jOut; jOut = ans.readToken();

        bool jImp = (jOut == "Impossible");
        bool pImp = (pOut == "Impossible");

        if (pImp) {
            if (jImp) continue;
            else quitf(_wa, "Participant could not find a solution, but one exists.");
        }

        for (char c : pOut) {
            if (c != '0' && c != '1') {
                quitf(_wa, "Output contains invalid characters. Only '0' and '1' allowed.");
            }
        }

        if (pOut.find(S) == std::string::npos) {
            quitf(_wa, "S is not a substring of the constructed T.");
        }

        long long actualK = countSmaller(pOut, S);
        if (actualK != K) {
            quitf(_wa, ("Expected " + std::to_string(K) + " substrings smaller than S, but found " + std::to_string(actualK) + ".").c_str());
        }

        if (jImp) {
            quitf(_fail, "Participant found a valid solution, but Jury said Impossible!");
        }

        int pLen = pOut.length();
        int jLen = jOut.length();
        
        if (pLen > jLen) {
            quitf(_wa, "Output length is not minimized.");
        }

        if (pLen < jLen) quitf(_wa, ("Participant found a solution strictly shorter than Jury's (" + std::to_string(pLen) + " < " + std::to_string(jLen) + ")!").c_str());
    }
    quitf(_ok, "Correct.");
    return 0;
}