#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int _t;
  cin >> _t;
  while (_t--) {
    int n;
    cin >> n;
    set<string> user;
    for (int i = 0; i < n; i++) {
      string buf;
      cin >> buf;
      user.insert(buf);
    }
    int m;
    cin >> m;
    string mess[m];
    string sender[m];
    getline(cin, mess[0]);
    int cnt = 0;
    for (int i = 0; i < m; i++) {
      getline(cin, mess[i]);
      sender[i] = "";
      if (mess[i][0] != '?') {
        for (int j = 0;; j++) {
          if (mess[i][j] == ':') break;
          sender[i] += mess[i][j];
        }
        cnt++;
      }
    }
    set<string> could[m];
    for (int i = 0; i < m; i++) {
      set<string> cur;
      for (auto u : user) cur.insert(u);
      if (sender[i].empty()) {
        if (i > 0 && !sender[i - 1].empty()) cur.erase(sender[i - 1]);
        if (i + 1 < m && !sender[i + 1].empty() &&
            cur.find(sender[i + 1]) != cur.end())
          cur.erase(sender[i + 1]);
        string buf = "";
        for (int j = 2; j < (int)mess[i].size(); j++) {
          char ch = mess[i][j];
          if (ch == ' ' || ch == '.' || ch == ',' || ch == '!' || ch == '?') {
            if (cur.find(buf) != cur.end()) {
              cur.erase(buf);
            }
            buf = "";
          } else
            buf += ch;
        }
        if (cur.find(buf) != cur.end()) {
          cur.erase(buf);
        }
        for (auto u : cur) could[i].insert(u);
        if (cur.size() != 1) {
          continue;
        }
        sender[i] = *cur.begin();
        cnt++;
      } else
        could[i].insert(sender[i]);
    }
    map<string, bool> dp[m + 1];
    map<string, string> was[m + 1];
    for (auto u : could[0]) dp[0][u] = true;
    for (int i = 0; i < m - 1; i++) {
      if (!sender[i].empty()) {
        if (i > 0 && !dp[i][sender[i]]) break;
        for (auto u : user)
          if (u != sender[i]) {
            dp[i + 1][u] = true;
            was[i + 1][u] = sender[i];
          }
        continue;
      }
      for (auto u : could[i]) {
        if (dp[i][u] || i == 0) {
          for (auto v : user)
            if (u != v) {
              dp[i + 1][v] = true;
              was[i + 1][v] = u;
            }
        }
      }
    }
    bool bad = true;
    for (auto u : could[m - 1])
      if (dp[m - 1][u]) {
        string cur = u;
        int j = m - 1;
        while (j >= 0) {
          sender[j] = cur;
          cur = was[j][cur];
          j--;
        }
        bad = false;
        break;
      }
    if (bad) {
      cout << "Impossible" << '\n';
      continue;
    }
    for (int i = 0; i < m; i++) {
      if (mess[i][0] == '?') {
        cout << sender[i];
        mess[i].erase(mess[i].begin());
        cout << mess[i] << '\n';
      } else
        cout << mess[i] << '\n';
    }
  }
  return 0;
}
