#include <bits/stdc++.h>
using namespace std;
const int N = 100;
bool used_user[N][N];
bool allowed_user[N][N];
string username[N];
string chat_messages[N];
map<string, int> user_id;
int user_chosen_for_msg[N];
void clean() {
  for (int msg = 0; msg < N; msg++)
    for (int user = 0; user < N; user++) used_user[msg][user] = false;
  for (int msg = 0; msg < N; msg++)
    for (int user = 0; user < N; user++) allowed_user[msg][user] = false;
  user_id.clear();
}
bool valid_username_entry(size_t username_entry, size_t username_size,
                          string chat_line) {
  bool valid_left_character = !isalnum(chat_line[username_entry - 1]);
  bool valid_right_character =
      username_entry + username_size == chat_line.size();
  if (!valid_right_character)
    valid_right_character = !isalnum(chat_line[username_entry + username_size]);
  return valid_left_character && valid_right_character;
}
bool user_mentioned_in_message(int user, string chat_line) {
  for (size_t username_entry = 0;; username_entry++) {
    username_entry = chat_line.find(username[user], username_entry);
    if (username_entry == string::npos) return false;
    if (valid_username_entry(username_entry, username[user].size(), chat_line))
      return true;
  }
}
bool dfs(int msg, int messages, int users) {
  if (msg == messages) return true;
  for (int user = 0; user < users; user++) {
    if (!allowed_user[msg][user]) continue;
    if (used_user[msg][user]) continue;
    if (msg > 0 && user_chosen_for_msg[msg - 1] == user) continue;
    used_user[msg][user] = true;
    user_chosen_for_msg[msg] = user;
    if (dfs(msg + 1, messages, users)) return true;
  }
  return false;
}
void solve() {
  int users;
  cin >> users;
  for (int user = 0; user < users; user++) {
    cin >> username[user];
    user_id[username[user]] = user;
  }
  int messages;
  cin >> messages;
  cin.ignore();
  for (int msg = 0; msg < messages; msg++) {
    string chat_line;
    getline(cin, chat_line);
    if (chat_line[0] == '?') {
      for (int user = 0; user < users; user++)
        if (!user_mentioned_in_message(user, chat_line))
          allowed_user[msg][user] = true;
    } else {
      auto name = chat_line.substr(0, chat_line.find(':'));
      allowed_user[msg][user_id[name]] = true;
    }
    chat_messages[msg] = chat_line.substr(chat_line.find(':'));
  }
  if (dfs(0, messages, users))
    for (int msg = 0; msg < messages; msg++)
      cout << username[user_chosen_for_msg[msg]] << chat_messages[msg] << '\n';
  else
    cout << "Impossible\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int chats;
  cin >> chats;
  while (chats--) {
    solve();
    clean();
  }
}
