#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int MAX_N = 110;
const int MAX_M = 110;
string salvage_sender(string line) {
  int idx = 0;
  while (line[idx] != ':') idx++;
  return line.substr(0, idx);
}
string strip_sender(string line) {
  return line.substr(salvage_sender(line).size() + 1);
}
void skip_noise(int& idx, string message) {
  while (idx < message.size() && !isalnum(message[idx])) idx++;
}
vector<string> salvage_others(string message, vector<string>& names) {
  vector<string> others;
  int idx = 0;
  skip_noise(idx, message);
  while (idx < message.size()) {
    int start_idx = idx;
    while (idx < message.size() && isalnum(message[idx])) idx++;
    string word = message.substr(start_idx, idx - start_idx);
    if (find(names.begin(), names.end(), word) != names.end()) {
      others.push_back(word);
    }
    skip_noise(idx, message);
  }
  return others;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  cin.ignore();
  while (t--) {
    int n, m;
    cin >> n;
    cin.ignore();
    map<string, int> map;
    vector<string> names(n);
    for (int i = 0; i < n; i++) {
      cin >> names[i];
      cin.ignore();
      map[names[i]] = i;
    }
    cin >> m;
    cin.ignore();
    vector<string> senders(m), messages(m);
    for (int i = 0; i < m; i++) {
      string line;
      getline(cin, line);
      senders[i] = salvage_sender(line);
      messages[i] = strip_sender(line);
    }
    bool dp[MAX_M][MAX_N], possible[MAX_M][MAX_N];
    int hist[MAX_M][MAX_N];
    for (int m_i = 0; m_i < m; m_i++) {
      for (int n_i = 0; n_i < n; n_i++) {
        possible[m_i][n_i] = true;
      }
      auto others = salvage_others(messages[m_i], names);
      for (auto o : others) {
        possible[m_i][map[o]] = false;
      }
      if (senders[m_i] == "?") continue;
      for (int n_i = 0; n_i < n; n_i++) {
        if (names[n_i] != senders[m_i]) possible[m_i][n_i] = false;
      }
    }
    for (int m_i = 0; m_i < m; m_i++) {
      for (int n_i = 0; n_i < n; n_i++) {
        if (m_i == 0)
          dp[m_i][n_i] = possible[m_i][n_i];
        else
          dp[m_i][n_i] = false;
        hist[m_i][n_i] = -1;
      }
    }
    for (int m_i = 1; m_i < m; m_i++) {
      for (int n_i = 0; n_i < n; n_i++) {
        for (int pre_n_i = 0; pre_n_i < n; pre_n_i++) {
          if (n_i != pre_n_i && possible[m_i][n_i] && dp[m_i - 1][pre_n_i]) {
            dp[m_i][n_i] = true;
            hist[m_i][n_i] = pre_n_i;
          }
        }
      }
    }
    int cur_m_i = -1, cur_n_i = -1;
    for (int n_i = 0; n_i < n; n_i++) {
      if (dp[m - 1][n_i]) {
        cur_m_i = m - 1;
        cur_n_i = n_i;
        break;
      }
    }
    if (cur_m_i < 0) {
      cout << "Impossible" << endl;
      continue;
    }
    while (cur_m_i >= 0) {
      senders[cur_m_i] = names[cur_n_i];
      cur_n_i = hist[cur_m_i][cur_n_i];
      cur_m_i--;
    }
    for (int i = 0; i < m; i++) {
      cout << senders[i] << ":" << messages[i] << endl;
    }
  }
  return 0;
}
