#include <bits/stdc++.h>
using namespace std;
int t, n, m, p;
char uname[105][13], msg[105][105], c, tmp[105];
int sender[105];
int mention[105][105];
bool ok(char x) {
  if (x >= '0' && x <= '9') return true;
  if (x >= 'a' && x <= 'z') return true;
  if (x >= 'A' && x <= 'Z') return true;
  return false;
}
int dp[105][105], pv[105][105];
int main() {
  scanf("%d", &t);
  for (int tc = (1); tc <= (t); tc++) {
    scanf("%d", &n);
    for (int j = (1); j <= (n); j++) scanf(" %s", uname[j]);
    scanf("%d\n", &m);
    memset(mention, 0, sizeof(mention));
    for (int j = (1); j <= (m); j++) {
      p = 0;
      while (scanf("%c", &c)) {
        if (c == '?') {
          sender[j] = -1;
          scanf(":");
          break;
        } else if (c == ':') {
          tmp[p++] = 0;
          for (int k = (1); k <= (n); k++)
            if (!strcmp(uname[k], tmp)) {
              sender[j] = k;
              break;
            }
          break;
        } else
          tmp[p++] = c;
      }
      gets(msg[j]);
      int len = strlen(msg[j]);
      for (int i = (0); i <= (len - 1); i++) {
        if (ok(msg[j][i])) {
          p = 0;
          while (i < len && ok(msg[j][i])) {
            tmp[p++] = msg[j][i];
            i++;
          }
          tmp[p++] = 0;
          for (int k = (1); k <= (n); k++)
            if (!strcmp(tmp, uname[k])) {
              mention[j][k] = 1;
            }
        }
      }
    }
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for (int i = (1); i <= (m); i++)
      for (int j = (0); j <= (n); j++)
        if (dp[i - 1][j]) {
          for (int k = (0); k <= (n); k++)
            if (k != j && k > 0) {
              if (sender[i] != -1) {
                if (k == sender[i]) {
                  dp[i][k] = 1;
                  pv[i][k] = j;
                }
              } else if (!mention[i][k]) {
                dp[i][k] = 1;
                pv[i][k] = j;
              }
            }
        }
    int res = -1;
    for (int j = (1); j <= (n); j++)
      if (dp[m][j]) {
        res = j;
        break;
      }
    if (res == -1) {
      printf("Impossible\n");
      continue;
    }
    for (int i = (m); i >= (1); i--) {
      sender[i] = res;
      res = pv[i][res];
    }
    for (int i = (1); i <= (m); i++) {
      printf("%s:", uname[sender[i]]);
      puts(msg[i]);
    }
  }
  return 0;
}
