#include <bits/stdc++.h>
using namespace std;
char ss[1000], name[100][100], msg[200][200], ts[200];
int ok[150][150], dp[150][150], ans[150], send[150], pre[150][150];
int nt(char ch) {
  return (ch < 'a' || ch > 'z') && (ch < '0' || ch > '9') &&
         (ch < 'A' || ch > 'Z');
}
int main() {
  int T;
  scanf("%d", &T);
  for (int tcnt = 0; tcnt < T; tcnt++) {
    int n, m;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%s", name[i]);
    scanf("%d", &m);
    for (int i = 0; i < m; i++)
      for (int j = 0; j < n; j++) ok[i][j] = 1;
    gets(ss);
    for (int i = 0; i < m; i++) {
      gets(ss);
      if (ss[0] != '?') {
        for (int j = 0; ss[j] != ':'; j++) ts[j] = ss[j], ts[j + 1] = 0;
        for (int j = 0; j < n; j++)
          if (strcmp(ts, name[j]) == 0) {
            send[i] = j;
            break;
          }
      } else {
        send[i] = -1;
      }
      int pt = 0;
      for (; ss[pt] != ':'; pt++)
        ;
      pt++;
      for (int j = 0; ss[pt]; pt++, j++) msg[i][j] = ss[pt], msg[i][j + 1] = 0;
      for (int j = 0; j < n; j++)
        for (int p1 = 0, fg; msg[i][p1]; p1++) {
          fg = 0;
          for (int k = 0; name[j][k]; k++)
            if (msg[i][p1 + k] != name[j][k]) {
              fg = 1;
              break;
            }
          int l = strlen(name[j]);
          if (fg == 0 && (p1 == 0 || nt(msg[i][p1 - 1])) &&
              nt(msg[i][p1 + l])) {
            ok[i][j] = 0;
            break;
          }
        }
    }
    for (int i = 0; i < m; i++)
      for (int j = 0; j < n; j++) dp[i][j] = 0, pre[i][j] = -1;
    if (send[0] == -1)
      for (int i = 0; i < n; i++) dp[0][i] = ok[0][i];
    else
      dp[0][send[0]] = 1;
    for (int i = 0; i < m - 1; i++) {
      if (send[i + 1] > -1) {
        for (int j = 0; j < n; j++)
          if (dp[i][j] && j != send[i + 1]) {
            pre[i + 1][send[i + 1]] = j;
            dp[i + 1][send[i + 1]] = 1;
            break;
          }
        continue;
      }
      for (int j = 0; j < n; j++)
        if (dp[i][j])
          for (int k = 0; k < n; k++)
            if (j != k && ok[i + 1][k]) {
              dp[i + 1][k] = 1;
              pre[i + 1][k] = j;
            }
    }
    int has = 0;
    for (int i = 0; i < n; i++)
      if (dp[m - 1][i]) {
        for (int pt = m - 1, t = i; pt >= 0; pt--) {
          ans[pt] = i;
          i = pre[pt][i];
        }
        has = 1;
        break;
      }
    if (has) {
      for (int i = 0; i < m; i++) printf("%s:%s\n", name[ans[i]], msg[i]);
    } else
      puts("Impossible");
  }
  return 0;
}
