//土尔逊Torson 编写于2023/4/21
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <stdlib.h>

using namespace std;

const int MAXN = 1000;
string sample[MAXN];
const int sub1 = 'a' - 'A', sub2 = 'A' - 'a';

bool findBrackets(char c, string t, int left, int right) {
	for (int i = left; i <= right; i++) {
		if (c == t[i] || c == t[i] + sub1 || c == t[i] + sub2) return true;
	}
	return false;
}

int comp0471(string target, string sample) {
	int i = 0, j = 0, k = 0;
	while (k < sample.size() && k + i < sample.size() && j < target.size()) {
			if (target[j] == '[') {
				int left = j, right = j + 1;
				for (right; target[right] != ']'; right++);
				bool flag = findBrackets(sample[k + i], target, left, right);
				if (flag) {
					i++; j = right + 1;
					if (j == target.size())return k;
				}
				else {
					k++; i = 0; j = 0;
				}
			}
			else {
				if (sample[k + i] == target[j] || sample[k + i] == target[j] + sub1 || sample[k + i] == target[j] + sub2)
				{
					i++; j++;
					if (j == target.size())return k;
				}
				else {
					k++; i = 0; j = 0;
				}
			}
	}
	return -1;
}

int main() {
	int n;
	while (cin >> n) {
		for (int i = 0; i < n; ++i)
			cin >> sample[i];
		string target;
		cin >> target;

		for (int i = 0; i < n; ++i) {
			if (comp0471(target, sample[i]) != -1) printf("%d %s\n", i + 1, sample[i].c_str());
		}
	}
	system("pause");
	return EXIT_SUCCESS;
}

// 64 位输出请用 printf("%lld")