#include <iostream>
#include <string>
#include <vector>
using namespace std;

bool cmp(char a1, char a2)
{
    if(a1 == a2 || abs(a1 - a2) == 32) return true;
    return false;
}

int main() {
    int n;
    vector<string> ss;
    string s;

    while(cin >> n)
    {
        for(int i = 1; i <= n; ++i)
        {
            cin >> s;
            ss.push_back(s);
        }
        cin >> s;
        for(int i = 0; i < n; ++i)
        {
            bool flag = true;
            auto it = ss[i].begin();
            for(int j = 0; j < s.size(); ++j)
            {
                if(s[j] != '[')
                {
                    if(!cmp(s[j], *it)) 
                    {
                        flag = false;
                        break;
                    }
                    else ++it;
                }
                else 
                {
                    flag = false; ++j;
                    while (s[j] != ']') 
                    {
                        if(cmp(s[j], *it)) flag = true;
                        ++j;
                    }
                    ++it;
                }
            }
            if(flag) cout << (i+1) << ' ' << ss[i] << endl;
        }
        ss.clear(); s.clear();
    }
}