import java.util.*; public class Main { public static void main(String[] args) { // 使用Scanner读取输入 Scanner scanner = new Scanner(System.in); // 读取文档数量 int N = scanner.nextInt(); scanner.nextLine(); // 消耗换行符 // 创建单词到文档列表的映射 Map<String, List<Integer>> wordToDocs = new HashMap<>(); // 处理每篇文档 for (int i = 1; i <= N; i++) { // 读取整行并分割 String[] parts = scanner.nextLine().split(" "); int Li = Integer.parseInt(parts[0]); // 使用集合存储当前文档的单词,避免重复 Set<String> wordsInDoc = new HashSet<>(); for (int j = 1; j <= Li; j++) { wordsInDoc.add(parts[j]); } // 将当前文档编号添加到每个单词的文档列表中 for (String word : wordsInDoc) { // 如果单词不存在则创建新列表,否则使用现有列表 wordToDocs.computeIfAbsent(word, k -> new ArrayList<>()).add(i); } } // 读取查询数量 int M = scanner.nextInt(); scanner.nextLine(); // 消耗换行符 // 处理每个查询 for (int i = 0; i < M; i++) { String word = scanner.nextLine(); List<Integer> docs = wordToDocs.get(word); // 输出结果 if (docs != null) { for (int j = 0; j < docs.size(); j++) { System.out.print(docs.get(j) + " "); } } System.out.println(); } scanner.close(); } }