#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const long long LLINF = LLONG_MAX;
const double EPS = 1.0e-9;
const double PI = atan(1.0) * 4.0;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int TESTS;
  cin >> TESTS;
  while (TESTS--) {
    int n, m;
    cin >> n;
    vector<string> nicks(n);
    map<string, int> nick_id;
    for (int i = 0; i < n; i++) {
      cin >> nicks[i];
      nick_id[nicks[i]] = i;
    }
    cin >> m;
    vector<string> mails(m);
    vector<vector<int>> prob(m);
    getline(cin, mails[0]);
    for (int i = 0; i < m; i++) {
      getline(cin, mails[i]);
      assert(mails[i].back() != '\n');
      mails[i] = mails[i] + " ";
      if (mails[i][0] == '?') {
        string word = "";
        set<int> may;
        for (int j = 0; j < n; j++) may.insert(j);
        for (int j = 2; j < mails[i].length(); j++) {
          if (isalpha(mails[i][j]) || isdigit(mails[i][j])) {
            word += mails[i][j];
          } else {
            if (nick_id.count(word) == 1) {
              may.erase(nick_id[word]);
            }
            word = "";
          }
        }
        for (const int &id : may) prob[i].push_back(id);
      } else {
        string author = mails[i].substr(0, mails[i].find(':'));
        assert(nick_id.count(author) == 1);
        prob[i].push_back(nick_id[author]);
      }
    }
    bool dp[m][n];
    int prev[m][n];
    for (int i = 0; i < n; i++) {
      dp[0][i] = false;
      for (int j = 0; j < m; j++) prev[j][i] = -1;
    }
    for (const int &id : prob[0]) dp[0][id] = true;
    for (int i = 1; i < m; i++) {
      for (int j = 0; j < n; j++) {
        dp[i][j] = false;
      }
      for (const int &id : prob[i]) {
        for (const int &pid : prob[i - 1]) {
          if (pid != id && dp[i - 1][pid]) {
            prev[i][id] = pid;
            dp[i][id] = true;
            break;
          }
        }
      }
    }
    vector<int> ans(m);
    int cur = m - 1, last = -1;
    for (int i = 0; i < n; i++) {
      if (dp[m - 1][i]) {
        last = i;
        break;
      }
    }
    if (last == -1) {
      cout << "Impossible\n";
    } else {
      bool answer_exists = true;
      while (cur > -1) {
        ans[cur] = last;
        if (prev[cur][last] == -1 && cur > 0) {
          answer_exists = false;
          break;
        } else {
          last = prev[cur][last];
        }
        cur--;
      }
      if (!answer_exists) {
        cout << "Impossible\n";
      } else {
        for (int i = 0; i < m; i++) {
          string text = mails[i].substr(mails[i].find(':'));
          cout << nicks[ans[i]] << text.substr(0, text.length() - 1) << '\n';
          if (mails[i][0] == '?' &&
              mails[i].find(nicks[ans[i]]) != string::npos) {
            int pos = mails[i].find(nicks[ans[i]]);
            assert(pos > mails[i].find(':'));
            char l = mails[i][pos - 1];
            char r = mails[i][pos + nicks[ans[i]].length()];
            assert(isalpha(l) || isalpha(r) || isdigit(l) || isdigit(r));
          }
        }
      }
    }
  }
  return 0;
}
