#include <bits/stdc++.h>
#include <limits>
#include "testlib.h"

using namespace std;

using u32 = uint32_t;
constexpr static u32 HASH_BASE = 653;
constexpr static u32 SECRET_SEED = 02'11'2025;
struct Secret {
    std::mt19937 gen;
    u32 hash;

    Secret() : gen(SECRET_SEED), hash(0u) {}

    u32 Protect(u32 x) {
        x ^= gen();
        hash = hash * HASH_BASE + x;
        return x;
    }
};

enum class RunResult {
    Success,
    IncorrectArgument,
    TooManyQueries,
};

void EnsureProtocol(bool cond) {
    quitif(!cond, _wa, "protocol violation");
}

bool Check(const int n, vector<int> p, vector<int> q) {
    // cout << "check n=" << n << endl;
    for (int x : p) cout << x << " "; cout << endl;
    for (int x : q) cout << x << " "; cout << endl;

    const int k = n/2;
    auto norm = [&](vector<int>& v) {
        auto it = find(v.begin(), v.end(), 1);
        rotate(v.begin(), it, v.end());
        assert(v[0] == 1);
        rotate(v.begin(), v.begin()+k+1, v.end());
        assert(v[k] == 1);
        if (v[k-1] > v[k+1]) {
            reverse(v.begin(), v.end());
        }
    };

    norm(p), norm(q);
    // cout << "norm" << endl;
    // for (int x : p) cout << x << " "; cout << endl;
    // for (int x : q) cout << x << " "; cout << endl;

    for (int i = 1; i + 1 < n; ++i) {
        if (p[i] != q[i]) return false;
    }

    auto query = [&](const vector<int>& v, int xval, int yval) {
        int i = find(v.begin(), v.end(), xval) - v.begin();
        int j = find(v.begin(), v.end(), yval) - v.begin();
        if (i > j) swap(i, j);

        int res = n;
        if (j - i < n - j + i) {
            for (int u = i; u <= j; ++u) res = min(res, v[u]);
        } else {
            for (int u = 0; u <= i; ++u) res = min(res, v[u]);
            for (int u = j; u < n; ++u) res = min(res, v[u]);
        }

        // cout << "ask " << xval << " " << yval << " -> " << res << endl;

        return res;
    };

    const int x = p[0], y = p[n-1];
    const int s = p[1], t = p[n-2];

    if (query(p, x, s) != query(q, x, s)) return false;
    if (query(p, x, t) != query(q, x, t)) return false;
    if (query(p, y, s) != query(q, y, s)) return false;
    if (query(p, y, t) != query(q, y, t)) return false;

    return true;
}

int main(int argc, char** argv) {
    ios::sync_with_stdio(0), cin.tie(0);
    registerTestlibCmd(argc, argv);

    int t = inf.readInt();
    const int q = inf.readInt();

    Secret secret_input, secret_output, secret_check;

    for (int testid = 1; testid <= t; ++testid) {
        setTestCase(testid);

        int n = inf.readInt();
        vector<int> p(n);
        for (int i = 0; i < n; ++i) {
            u32 x = inf.readUnsignedLong();
            p[i] = secret_input.Protect(x);
        }

        auto res = static_cast<RunResult>(ouf.readInt());
        if (res == RunResult::IncorrectArgument)
            quitf(_wa, "get_min: incorrect aguments");
        if (res == RunResult::TooManyQueries)
            quitf(_wa, "too many queries");
        EnsureProtocol(res == RunResult::Success);

        vector<long long> w = ouf.readLongs(n, 0, (long long)numeric_limits<u32>::max(), "w");
        vector<int> q;
        for (u32 x : w) {
            q.push_back(static_cast<int>(secret_output.Protect(x)));
            secret_check.Protect(q.back());
        }
            
        vector<int> used(n+1);
        for (int i = 0; i < n; ++i) {
            if (q[i] < 1 || q[i] > n || used[q[i]])
                quitf(_wa, "returned not a permutation");
            used[q[i]] = 1;
        }

        if (!Check(n, p, q)) {
            quitf(_wa, "incorrect answer");
        }
    }

    u32 queries = secret_output.Protect(ouf.readLong());
    secret_check.Protect(queries);
    EnsureProtocol(queries <= q); // RunResult::TooManyQueries

    u32 input_hash = ouf.readLong();
    u32 output_hash = ouf.readLong();

    EnsureProtocol(input_hash == secret_input.hash);
    EnsureProtocol(output_hash == secret_check.hash);

    if (q != 1'000'000) { // constant q for last group
        quitf(_ok, "you're awesome :3");
    }

    int score = min(60, (95 * (1'000'000 - (int) queries)) / 1'000'000);
    quitp(score);
}