#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
//思路重新整理:从输入数据出发,逐行尽最大努力去让自己被匹配
bool cmp(char ch1, char ch2){
if(ch1>='a' && ch2<='z'){
ch1 -= 32;
}
if(ch2>='a' && ch2<='z'){
ch2 -= 32;
}
if(ch1 == ch2){
return true;
}else{
return false;
}
}
bool pipei(const string &s1, const string &s2){//s2中含有若干[]
int spe = 0;//标记“中括号内”特殊态
int p=0, q=0;
while(p < s1.size()){
if(!spe && !cmp(s1[p],s2[q]) && s2[q] != '[' && s2[q] != ']'){
return false;
}else if(!spe && cmp(s1[p],s2[q])){
p++;q++;
}else if(s2[q] == '['){
q++;
spe = 1;
}else if(s2[q] == ']'){
q++;
spe = 0;
}else if(spe && !cmp(s1[p],s2[q])){
if(s2[q+1] != ']'){
q++;
}else{
return false;
}
}else if(spe && cmp(s1[p],s2[q])){//括号内剩下的没有再比的必要了
while(s2[q] != ']'){
q++;
}
p++;
}
}
return true;
}
void ZFCPP(void){
int n;
vector<string> v;
while(scanf("%d",&n)!=EOF){
string s;
while(n--){
cin >> s;
v.push_back(s);
}
cin >> s;
for(int i=0; i<v.size(); i++){
if(pipei(v[i],s)){
printf("%d %s\n",i+1,v[i].c_str());
}
}
v.clear();
}
return;
}
int main(){
ZFCPP();
return 0;
}