#include <bits/stdc++.h>
using namespace std;
vector<string> caras;
char temp[1000];
set<string> pode[101];
vector<string> entrada;
void get(int idx) {
  pode[idx].clear();
  scanf(" %[^\n]s", temp);
  entrada.push_back(temp);
  string s = temp;
  for (auto s : caras) pode[idx].insert(s);
  if (s[0] == '?') {
    for (int i = 0; i < s.size(); i++) {
      for (int j = 0; j < caras.size(); j++) {
        int k = 0;
        while (k < caras[j].size() && caras[j][k] == s[i + k]) k++;
        if (i - 1 >= 0 && isalnum(s[i - 1])) continue;
        if (isalnum(s[i + k])) continue;
        if (k == caras[j].size()) pode[idx].erase(caras[j]);
      }
    }
  } else {
    int k = 0;
    pode[idx].clear();
    string nome;
    while (s[k] != ':') nome += s[k++];
    pode[idx].insert(nome);
  }
}
map<pair<int, string>, bool> dp;
int maximo;
vector<string> ans;
bool solve(int i, string s) {
  if (dp.count({i, s})) return dp[{i, s}];
  auto &ret = dp[{i, s}] = false;
  if (i == maximo) return ret = true;
  for (auto s2 : pode[i]) {
    if (s == s2) continue;
    ret |= solve(i + 1, s2);
    if (ret) return ret;
  }
  return ret;
}
void build(int i, string s) {
  if (i == maximo) return;
  for (auto s2 : pode[i]) {
    if (s == s2) continue;
    if (dp[{i + 1, s2}]) {
      ans.push_back(s2);
      build(i + 1, s2);
      return;
    }
  }
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    dp.clear();
    entrada.clear();
    ans.clear();
    int n;
    scanf("%d", &n);
    caras.clear();
    for (int i = 0; i < n; i++) {
      scanf("%s", temp);
      caras.push_back(temp);
    }
    int m;
    scanf("%d", &m);
    maximo = m;
    for (int i = 0; i < m; i++) {
      get(i);
    }
    solve(0, "");
    if (dp[{0, ""}]) {
      build(0, "");
      int k = 0;
      for (auto s : entrada) {
        if (s[0] == '?') s.erase(s.begin()), cout << ans[k];
        cout << s << endl;
        k++;
      }
    } else {
      cout << "Impossible" << endl;
    }
  }
  return 0;
}
