#include <bits/stdc++.h>
using namespace std;
int n, m;
const int N = 110;
string name[N], s;
struct Node {
  string sender, text;
} mem[N];
bool pd[N][N];
bool in(string &name, string &text) {
  int len = text.size(), len1 = name.size();
  bool pd;
  for (int i = 0; i + len1 <= len; i++) {
    pd = true;
    for (int j = 0; pd && j < len1; j++)
      if (name[j] != text[i + j]) pd = false;
    if (i > 0 && (isdigit(text[i - 1]) || isalpha(text[i - 1]))) pd = false;
    if (i + len1 < len && (isdigit(text[i + len1]) || isalpha(text[i + len1])))
      pd = false;
    if (pd) return true;
  }
  return false;
}
bool f[N][N];
int last[N][N];
map<string, int> mp;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    cin >> n;
    mp.clear();
    for (int i = 1; i <= n; i++) cin >> name[i], mp[name[i]] = i;
    cin >> m;
    getchar();
    for (int i = 1; i <= m; i++) {
      getline(cin, s);
      mem[i].sender = "";
      for (int j = 0; j < s.size(); j++)
        if (s[j] != ':')
          mem[i].sender = mem[i].sender + s[j];
        else {
          s.erase(0, j + 1);
          mem[i].text = s;
          break;
        }
    }
    memset(pd, true, sizeof(pd));
    int t;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        if (in(name[i], mem[j].text)) pd[i][j] = false;
    memset(f, false, sizeof(f));
    f[0][0] = true;
    for (int i = 1; i <= m; i++) {
      if (mem[i].sender == "?") {
        for (int j = 1; j <= n; j++) {
          if (pd[j][i])
            for (int k = 0; k <= n; k++)
              if (k != j && f[i - 1][k]) {
                f[i][j] = true;
                last[i][j] = k;
                break;
              }
        }
      } else {
        t = mp[mem[i].sender];
        for (int j = 0; j <= n; j++)
          if (t != j && f[i - 1][j] && pd[t][i]) {
            f[i][t] = true;
            last[i][t] = j;
            break;
          }
      }
    }
    bool ans = false;
    int now;
    for (int i = 1; i <= n; i++)
      if (f[m][i]) ans = true, now = i;
    if (ans) {
      for (int i = m; i > 0; i--) {
        mem[i].sender = name[now];
        now = last[i][now];
      }
      for (int i = 1; i <= m; i++)
        cout << mem[i].sender << ":" << mem[i].text << endl;
    } else
      puts("Impossible");
  }
  return 0;
}
