#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

// Give numerical grade to the user
[[noreturn]] void grade(int result, string feedback="")
{
    cout << '1';
    erase(feedback, '\n');
    if (feedback != "") cout << ' ' << feedback;
    cout << '\n' << result << '\n';
    exit(0);
}
// Indicate that program failed to produce the right answer
[[noreturn]] void fail(string feedback="")
{
    cout << '0';
    erase(feedback, '\n');
    if (feedback != "") cout << ' ' << feedback;
    cout << '\n';
    exit(0);
}
[[noreturn]] void failcase(int t, string feedback) {
    fail("Case #" + to_string(t) + ": " + feedback);
}
// Indicate that the user output was accepted
[[noreturn]] void accept(string feedback="")
{
    cout << '1';
    erase(feedback, '\n');
    if (feedback != "") cout << ' ' << feedback;
    cout << '\n';
    exit(0);
}
#define SAFE_READ(x) \
    do               \
    {                \
        if (!(x))    \
            fail();  \
    } while (false)

int main(int argc, char **argv)
{
    string x1 = argv[1]; // correct output
    string x2 = argv[2]; // user output
    string x3 = argv[3]; // input
    ifstream correct, user, input;
    correct.open(x1);
    user.open(x2);
    input.open(x3);

    // Write grader here
    // Remember to add this to the Makefile, otherwise the default grader will be used
    int t;
    input>>t;
    for (int tt = 0; tt < t; tt++) {
        string cok, uok;
        correct>>cok;
        SAFE_READ(user>>uok);
        if (uok != cok) failcase(tt+1, "Wrong answer \"" + uok + "\", expected " + cok);
        if (uok == "NO") {
            int n, k; // read NO test
            input>>n>>k;
            for (int i = 0; i < n; i++) input>>k;
            continue;
        }
        int n, k;
        input>>n>>k;

        int ci[n];
        int cu[n];
        long long cs[n + 1];
        for (int i = 0; i <= n; i++) {
            cs[i] = 0;
        }
        int cc;
        for (int i = 0; i < n; i++) {
            SAFE_READ(user>>cu[i]);
            SAFE_READ(correct>>cc);
            cs[cu[i]] += 2 * (i + 1) - n - 1;
            input>>ci[i];
        }

        sort(ci, ci + n);
        sort(cu, cu + n);
        for (int i = 0; i < n; i++) {
            if (ci[i] != cu[i]) failcase(tt+1, "Set of blocks is incorrect");
        }

        for (int i = 1; i <= n; i++) {
            if (cs[i]) failcase(tt+1, "Color " + to_string(i) + " is not balanced");
        }
    }

    accept();
}
