#include <bits/stdc++.h>
using namespace std;
int colors;
vector<int> possible_colors[100];
int used_colors[100];
bool dfs(int v, int messages) {
  if (v == messages) return true;
  for (int color = 0; color < colors; color++) {
    if (v > 0 && color == used_colors[v - 1]) continue;
    bool found_color = false;
    for (int possible_color : possible_colors[v])
      if (color == possible_color) {
        found_color = true;
        break;
      }
    if (found_color) {
      used_colors[v] = color;
      if (dfs(v + 1, messages)) return true;
    }
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int chats;
  cin >> chats;
  while (chats--) {
    int users;
    cin >> users;
    colors = users;
    vector<string> names(users);
    for (int name = 0; name < users; name++) cin >> names[name];
    int messages_cnt;
    cin >> messages_cnt;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    vector<string> messages(messages_cnt);
    for (int message = 0; message < messages_cnt; message++)
      getline(cin, messages[message]);
    vector<vector<string> > possible_authors;
    for (auto message : messages)
      if (message[0] == '?') {
        vector<string> current_message_authors;
        for (auto name : names) {
          size_t first_character = 0;
          while (first_character != string::npos) {
            first_character = message.find(name, first_character);
            if (first_character == string::npos) break;
            if (!isalnum(message[first_character - 1])) {
              if (first_character + name.size() < message.size() &&
                  !isalnum(message[first_character + name.size()]))
                break;
              if (first_character + name.size() >= message.size()) break;
            }
            first_character++;
          }
          if (first_character == string::npos)
            current_message_authors.push_back(name);
        }
        possible_authors.push_back(current_message_authors);
      } else {
        vector<string> current_message_authors = {
            message.substr(0, message.find(':'))};
        possible_authors.push_back(current_message_authors);
      }
    bool has_zero_authors = false;
    for (int message = 0; message < messages_cnt; message++)
      has_zero_authors |= possible_authors[message].empty();
    if (has_zero_authors)
      cout << "Impossible\n";
    else {
      for (int message = 0; message < messages_cnt; message++)
        for (int name = 0; name < users; name++) {
          bool found = false;
          for (auto author : possible_authors[message])
            if (author == names[name]) {
              found = true;
              break;
            }
          if (found) possible_colors[message].push_back(name);
        }
      if (dfs(0, messages_cnt))
        for (int message = 0; message < messages_cnt; message++)
          cout << names[used_colors[message]]
               << messages[message].substr(messages[message].find(':')) << '\n';
      else
        cout << "Impossible\n";
      for (int message = 0; message < messages_cnt; message++) {
        possible_colors[message].clear();
        used_colors[message] = 0;
      }
    }
  }
}
