#include <bits/stdc++.h>
using namespace std;
template <typename T>
void get_int(T &x) {
  char t = getchar();
  bool neg = false;
  x = 0;
  for (; (t > '9' || t < '0') && t != '-'; t = getchar())
    ;
  if (t == '-') neg = true, t = getchar();
  for (; t <= '9' && t >= '0'; t = getchar()) x = x * 10 + t - '0';
  if (neg) x = -x;
}
template <typename T>
void print_int(T x) {
  if (x < 0) putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
const int inf = 0x3f3f3f3f;
const long long Linf = 1ll << 61;
const double pi = acos(-1.0);
map<string, int> id;
string mes[1111], s[111];
int n, m, dp[111][111], pre[111][111], sd[111], cont[111][111];
void path(int x, int y) {
  if (x > 1) {
    path(x - 1, pre[x][y]);
    cout << s[y] << ":" << mes[x - 1] << endl;
  }
}
void solve() {
  id.clear();
  memset(cont, 0, sizeof(cont));
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    id[s[i]] = i;
  }
  cin >> m;
  cin.ignore();
  for (int i = 1; i <= m; i++) getline(cin, mes[i]);
  for (int i = 1; i <= m; i++) {
    int t = -1;
    for (int j = 0; j < (int)mes[i].size(); j++)
      if (mes[i][j] == ':') t = j;
    if (mes[i][0] == '?')
      sd[i] = -1;
    else
      sd[i] = id[mes[i].substr(0, t)];
    if (sd[i] == 0)
      for (;;)
        ;
    mes[i] = mes[i].substr(t + 1) + ' ';
    int last = -1;
    for (int j = 0; j < (int)mes[i].size(); j++) {
      if (mes[i][j] >= 'a' && mes[i][j] <= 'z') continue;
      if (mes[i][j] >= 'A' && mes[i][j] <= 'Z') continue;
      if (mes[i][j] >= '0' && mes[i][j] <= '9') continue;
      if (last < j - 1) {
        int t = id[mes[i].substr(last + 1, j - last - 1)];
        if (t) cont[i][t] = 1;
      }
      last = j;
    }
    mes[i] = mes[i].substr(0, mes[i].size() - 1);
  }
  memset(dp, 0, sizeof(dp));
  dp[1][0] = 1;
  for (int i = 1; i <= m; i++) {
    for (int j = min(1, i - 1); j <= n; j++) {
      if (!dp[i][j]) continue;
      if (sd[i] != -1 && j != sd[i]) {
        dp[i + 1][sd[i]] = 1;
        pre[i + 1][sd[i]] = j;
      } else if (sd[i] == -1) {
        for (int nxt = 1; nxt <= n; nxt++) {
          if (cont[i][nxt] || j == nxt) continue;
          dp[i + 1][nxt] = 1;
          pre[i + 1][nxt] = j;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++)
    if (dp[m + 1][i]) {
      path(m + 1, i);
      return;
    }
  puts("Impossible");
}
int main() {
  int tc;
  cin >> tc;
  while (tc--) solve();
  return 0;
}
