#include "testlib.h"
#include <vector>
#include <string>

using namespace std;

int main(int argc, char* argv[]) {
    setName("GCPC 2026 Attracting Attendees Checker");
    registerTestlibCmd(argc, argv);

    // 1. 读取输入数据 (inf)
    int n = inf.readInt();
    int m = inf.readInt();
    int c = inf.readInt();

    vector<vector<int>> likes(m);
    for (int i = 0; i < m; i++) {
        int k = inf.readInt();
        likes[i].resize(k);
        for (int j = 0; j < k; j++) {
            likes[i][j] = inf.readInt();
        }
    }

    // 2. 读取选手输出 (ouf)
    string result = ouf.readToken();
    if (result == "impossible") {
        // 注意：验证 impossible 是 NP-hard 问题，SPJ 无法真正验证，只能信任选手。
        // 如果输出 impossible，直接判对（前提是数据保证答案确实为 impossible，否则会有漏洞，但这是此类题目的通用做法）。
        quitf(_ok, "impossible accepted");
    }

    if (result != "possible") {
        quitf(_wa, "Expected 'possible' or 'impossible', found '%s'", result.c_str());
    }

    // 读取阵容
    int k = ouf.readInt(0, n, "number of bands");
    if (k == 0) {
        quitf(_wa, "Empty lineup is not allowed (a festival with no bands is a total failure)");
    }

    vector<int> lineup(k);
    vector<bool> selected(n + 1, false);
    for (int i = 0; i < k; i++) {
        int band = ouf.readInt(1, n, "band id");
        if (selected[band]) {
            quitf(_wa, "Duplicate band %d in lineup", band);
        }
        selected[band] = true;
        lineup[i] = band;
    }

    // 检查是否有多余输出
    if (!ouf.seekEof()) {
        quitf(_wa, "Extra tokens found in output");
    }

    // 3. 高效验证（线性扫描）
    vector<int> audience(n + 1, 0);

    for (int i = 0; i < m; i++) {
        int totalLiked = (int)likes[i].size();
        int selectedLiked = 0;

        // 第一遍：数出该观众喜欢的乐队中有几个被选中了
        for (int b : likes[i]) {
            if (selected[b]) selectedLiked++;
        }

        // 如果有至少一半喜欢的乐队被选中，则此人会来参加音乐节
        if (selectedLiked * 2 >= totalLiked) {
            // 第二遍：此人会观看所有他喜欢且被选中的乐队，给这些乐队各 +1 观众
            for (int b : likes[i]) {
                if (selected[b]) {
                    audience[b]++;
                }
            }
        }
    }

    // 4. 检查每个选中乐队的观众数是否达标
    for (int band : lineup) {
        if (audience[band] < c) {
            quitf(_wa, "Band %d has only %d audience, need at least %d", 
                  band, audience[band], c);
        }
    }

    quitf(_ok, "possible, %d bands selected", k);
}