#include <bits/stdc++.h>
using namespace std;
vector<string> namestore;
unordered_map<string, int> names;
unordered_set<int> messname[104];
vector<long long int> senderid;
int ans[105], n, m, dp[105][105];
int solve(int curr) {
  if (curr == m) return 1;
  if (dp[curr][ans[curr - 1]] != -1) {
    return dp[curr][ans[curr - 1]];
  }
  if (senderid[curr] != -1) {
    ans[curr] = senderid[curr];
    if (curr > 0)
      if (ans[curr] == ans[curr - 1]) return 0;
    return solve(curr + 1);
  }
  for (int i = 0; i < int(n); i++) {
    if (curr > 0)
      if (ans[curr - 1] == i) continue;
    if (messname[curr].find(i) != messname[curr].end()) continue;
    ans[curr] = i;
    if (solve(curr + 1) == 1) {
      dp[curr][ans[curr - 1]] = 1;
      return 1;
    }
  }
  dp[curr][ans[curr - 1]] = 0;
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    names.clear();
    memset(dp, -1, sizeof(dp));
    namestore.clear();
    for (int i = 0; i < int(104); i++) messname[i].clear();
    senderid.clear();
    cin >> n;
    for (int i = 0; i < int(n); i++) {
      string second;
      cin >> second;
      names[second] = i;
      namestore.push_back(second);
    }
    cin >> m;
    string text[105];
    getline(cin, text[0]);
    for (int i = 0; i < int(m); i++) {
      getline(cin, text[i]);
      string sender;
      int trav = 0;
      for (trav = 0; trav < text[i].size(); trav++) {
        if (text[i][trav] != ':')
          sender.push_back(text[i][trav]);
        else
          break;
      }
      if (sender == "?")
        senderid.push_back(-1);
      else
        senderid.push_back(names[sender]);
      while (trav < text[i].size()) {
        string temp;
        while (((text[i][trav] >= 'a' && text[i][trav] <= 'z') ||
                (text[i][trav] >= 'A' && text[i][trav] <= 'Z') ||
                (text[i][trav] >= '0' && text[i][trav] <= '9')) &&
               trav < text[i].size()) {
          temp.push_back(text[i][trav++]);
        }
        if (names.find(temp) != names.end()) {
          if (messname[i].find(names[temp]) == messname[i].end())
            messname[i].insert(names[temp]);
        }
        trav++;
      }
    }
    int flag = solve(0);
    if (flag == 0)
      cout << "Impossible" << '\n';
    else
      for (int i = 0; i < m; i++) {
        int disp = 0;
        cout << namestore[ans[i]] << ":";
        for (disp = 0; disp < text[i].size(); disp++) {
          if (text[i][disp] == ':') {
            disp++;
            break;
          }
        }
        while (disp < text[i].size()) cout << text[i][disp++];
        cout << '\n';
      }
  }
  return 0;
}
