#include <bits/stdc++.h>
using namespace std;
bool isDigitOrLetter(char ch) {
  if ('a' <= ch && ch <= 'z') return true;
  if ('A' <= ch && ch <= 'Z') return true;
  if ('0' <= ch && ch <= '9') return true;
  return false;
}
void insertProhibits(string &s, set<string> &prohibit, vector<string> &users) {
  string token;
  vector<string>::iterator it;
  for (char c : s) {
    if (isDigitOrLetter(c))
      token += c;
    else {
      if (!token.empty()) {
        it = lower_bound(users.begin(), users.end(), token);
        if (it != users.end() && *it == token) prohibit.insert(token);
      }
      token.clear();
    }
  }
  if (!token.empty()) {
    it = lower_bound(users.begin(), users.end(), token);
    if (it != users.end() && *it == token) prohibit.insert(token);
  }
}
void solve() {
  int N;
  cin >> N;
  vector<string> all(N);
  string s;
  for (int i = 0; i < N; i++) {
    cin >> s;
    all[i] = s;
  }
  sort(all.begin(), all.end());
  int M;
  cin >> M;
  getline(cin, s);
  vector<string> user(M);
  vector<string> message(M);
  for (int i = 0; i < M; i++) {
    getline(cin, s);
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == ':') {
        user[i] = s.substr(0, j);
        message[i] = s.substr(j + 1);
        break;
      }
    }
  }
  vector<set<string> > prohibit(M);
  for (int i = 0; i < M; i++) {
    if (user[i] != "?") continue;
    if (i > 0 && user[i - 1] != "?") prohibit[i].insert(user[i - 1]);
    if (i < M - 1 && user[i + 1] != "?") prohibit[i].insert(user[i + 1]);
    insertProhibits(message[i], prohibit[i], all);
  }
  set<int> seen;
  for (int i = 0; i < M; i++)
    if (user[i] != "?") seen.insert(i);
  while (true) {
    int max_p = -1, pos = -1;
    for (int i = 0; i < M; i++) {
      if (seen.find(i) != seen.end()) continue;
      if ((int)prohibit[i].size() > max_p) {
        max_p = prohibit[i].size();
        pos = i;
      }
    }
    seen.insert(pos);
    if (pos == -1) break;
    if (max_p == N) {
      cout << "Impossible\n";
      return;
    }
    for (int i = 0; i < N; i++) {
      if (prohibit[pos].find(all[i]) == prohibit[pos].end()) {
        user[pos] = all[i];
        if (pos > 0) prohibit[pos - 1].insert(user[pos]);
        if (pos < M - 1) prohibit[pos + 1].insert(user[pos]);
        break;
      }
    }
  }
  for (int i = 0; i < M; i++) cout << user[i] << ":" << message[i] << "\n";
}
int main() {
  int T;
  cin >> T;
  while (T) {
    T--;
    solve();
  }
  return 0;
}
