#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 1e2 + 7;
void solve() {
  int n;
  cin >> n;
  vector<string> users(n);
  for (string& user : users) cin >> user;
  sort(users.begin(), users.end());
  auto getIndex = [&](const string& s) {
    int pos = lower_bound(users.begin(), users.end(), s) - users.begin();
    return pos == users.size() || users[pos] != s ? -1 : pos;
  };
  auto splitLine = [](const string& s) {
    int pos = s.find(':');
    return make_pair(s.substr(0, pos), s.substr(pos + 1));
  };
  auto splitMessage = [](const string& s) {
    vector<string> tokens;
    string token;
    for (char c : s) {
      if (isalpha(c) || isdigit(c))
        token += c;
      else {
        if (!token.empty()) tokens.push_back(token);
        token.clear();
      }
    }
    if (!token.empty()) tokens.push_back(token);
    return tokens;
  };
  int m;
  cin >> m;
  vector<int> who(m);
  vector<vector<char> > can(m, vector<char>(n, true));
  vector<string> msg(m);
  string tmp;
  getline(cin, tmp);
  for (int i = 0; i < m; i++) {
    string cur;
    getline(cin, cur);
    pair<string, string> p = splitLine(cur);
    const string& user = p.first;
    const string& message = p.second;
    who[i] = getIndex(user);
    if (who[i] != -1) {
      fill(can[i].begin(), can[i].end(), false);
      can[i][who[i]] = true;
    }
    msg[i] = message;
    vector<string> tokens = splitMessage(message);
    for (const string& s : tokens) {
      int ind = getIndex(s);
      if (ind != -1) {
        can[i][ind] = false;
      }
    }
  }
  vector<vector<int> > dp(m, vector<int>(n, -1));
  for (int i = 0; i < n; i++) {
    if (can[0][i]) dp[0][i] = 0;
  }
  for (int message = 0; message + 1 < m; message++) {
    for (int i = 0; i < n; i++) {
      if (dp[message][i] == -1) continue;
      for (int j = 0; j < n; j++) {
        if (i == j) continue;
        if (can[message + 1][j]) dp[message + 1][j] = i;
      }
    }
  }
  int message = m - 1, pos = -1;
  for (int i = 0; i < n; i++) {
    if (dp[message][i] != -1) {
      pos = i;
      break;
    }
  }
  if (pos == -1) {
    cout << "Impossible" << endl;
  } else {
    while (message > -1) {
      who[message] = pos;
      pos = dp[message--][pos];
    }
    for (int i = 0; i < m; i++) {
      cout << users[who[i]] << ':' << msg[i] << endl;
    }
  }
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
