#include <cctype>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <utility>

using namespace std;

string nya( const string &s )
{
  string r = s;
  if ('a' <= r[0] && r[0] <= 'z')
    r[0] = r[0] - 'a' + 'A';
  else if ('A' <= r[0] && r[0] <= 'Z')
    r[0] = r[0] + 'a' - 'A';
  return r;
}

int main()
{
  freopen("wiki.in", "r", stdin);
  freopen("wiki.out", "w", stdout);

  int n;
  cin >> n;
  string *names = new string[n];
  map <string, pair <bool, int> > reference;
  vector <string> *lines = new vector <string> [n];
  for (int i = 0; i < n; i++)
  {
    cin >> names[i];
    int k;
    cin >> k;
    string temp;
    for (int j = 0; j < k; j++)
    {
      cin >> temp;
      reference[temp] = reference[nya(temp)] = make_pair(true, i);
    }
    reference[names[i]] = reference[nya(names[i])] = make_pair(false, i);
    cin >> k;
    getline(cin, temp);
    for (int j = 0; j < k; j++)
    {
      getline(cin, temp);
      lines[i].push_back(temp);
    }
  }
  for (int i = 0; i < n; i++)
  {
    cout << names[i] << endl;
    for (int j = 0; j < (int)lines[i].size(); j++)
    {
      string res = "";
      for (int k = 0, l = 0; k < (int)lines[i][j].length(); k = l)
      {
        if (!isalpha(lines[i][j][l]))
        {
          res += lines[i][j][l++];
          continue;
        }
        string word = "";
        for (; l < (int)lines[i][j].length() && isalpha(lines[i][j][l]); l++)
          word += lines[i][j][l];
        if (reference.count(word) == 0 || reference[word].second == i)
        {
          res += word;
          continue;
        }
        bool form = reference[word].first;
        int article = reference[word].second;
        if (form)
          res += "[[" + names[article] + "|" + word + "]]";
        else
          res += "[[" + word + "]]";
      }
      cout << res << endl;
    }
  }

  delete[] names;
  delete[] lines;
  return 0;
}

