#include <bits/stdc++.h>
using namespace std;
map<string, int> Id;
char Name[107][15], Str[107][150];
int dp[107][107], last[107][107], record[107][107], vis[107];
void Show(int Step, int k) {
  if (Step - 1) Show(Step - 1, last[Step][k]);
  int pos = 0;
  while (Str[Step][pos] != ':') pos++;
  printf("%s%s\n", Name[k], Str[Step] + pos);
}
bool Judge(char t) {
  if (t >= 'a' && t <= 'z')
    return true;
  else if (t >= 'A' && t <= 'Z')
    return true;
  else if (t >= '0' && t <= '9')
    return true;
  else
    return false;
}
int main() {
  int t, n, m;
  string tmp;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    Id.clear();
    for (int i = 1; i <= n; i++) {
      scanf("%s", Name[i]);
      Id[string(Name[i])] = i;
    }
    memset(record, 0, sizeof(record));
    memset(vis, 0, sizeof(vis));
    scanf("%d", &m);
    getchar();
    for (int i = 1; i <= m; i++) {
      gets(Str[i]);
      int pos = 0;
      if (Str[i][0] != '?') {
        while (Str[i][pos] != ':') pos++;
        int re = Id[string(Str[i], pos)];
        record[i][re] = 1;
        vis[re] = 1;
        continue;
      } else {
        for (int j = 1; j <= n; j++) record[i][j] = 1;
        while (1) {
          while (!Judge(Str[i][pos]) && Str[i][pos]) pos++;
          if (Str[i][pos] == '\0') break;
          int tt = pos;
          while (Judge(Str[i][pos])) pos++;
          int re = Id[string(Str[i], tt, pos - tt)];
          if (re || vis[re]) record[i][re] = 0;
        }
      }
    }
    memset(dp, 0, sizeof(dp));
    memset(last, 0, sizeof(last));
    for (int i = 1; i <= m; i++) {
      if (i == 1) {
        for (int j = 1; j <= n; j++) dp[i][j] = record[i][j];
      } else {
        for (int j = 1; j <= n; j++) {
          if (!record[i][j]) continue;
          for (int k = 1; k <= n; k++) {
            if (j == k || !dp[i - 1][k]) continue;
            last[i][j] = k;
            dp[i][j] = 1;
          }
        }
      }
    }
    int flag = true;
    for (int i = 1; i <= n; i++) {
      if (dp[m][i]) {
        Show(m, i);
        flag = false;
        break;
      }
    }
    if (flag) printf("Impossible\n");
  }
  return 0;
}
