#include <bits/stdc++.h>
using namespace std;
int n, m, dp[100 + 10][100 + 10];
char name[100 + 10][10 + 10], user[100 + 10][10 + 10];
char orgtext[100 + 10][100 + 10], tmp[100 + 10], s[100 + 10 + 10];
set<string> text[100 + 10];
map<string, int> Mp;
void read() {
  scanf("%d", &n);
  Mp.clear();
  for (int i = 1; i <= n; i++) {
    scanf("%s", name[i]);
    Mp[(string)name[i]] = i;
  }
  scanf("%d\n", &m);
  for (int i = 1; i <= m; i++) {
    text[i].clear();
    gets(s);
    int lens = strlen(s), j = 0, orglen = 0;
    for (int p = 0; j < lens; j++) {
      if (s[j] == ':') {
        j++;
        user[i][p] = '\0';
        break;
      }
      user[i][p++] = s[j];
    }
    for (int k = j; k < lens; k++) orgtext[i][orglen++] = s[k];
    orgtext[i][orglen] = '\0';
    for (; j < lens;) {
      if (s[j] != '.' && s[j] != ',' && s[j] != '!' && s[j] != '?' &&
          s[j] != ' ') {
        int k = j, p = 0;
        while (k < lens && s[k] != '.' && s[k] != ',' && s[k] != '!' &&
               s[k] != '?' && s[k] != ' ')
          tmp[p++] = s[k++];
        tmp[p] = '\0';
        text[i].insert((string)tmp);
        j = k;
      } else
        j++;
    }
  }
}
void print(int i, int j) {
  for (; i >= 1; i--) {
    strcpy(user[i], name[j]);
    for (int k = 1; k <= n; k++)
      if (k != j && dp[i - 1][k]) {
        j = k;
        break;
      }
  }
}
void solve() {
  memset(dp, 0, sizeof dp);
  if (!strcmp(user[1], "?")) {
    for (int j = 1; j <= n; j++) {
      if (text[1].find((string)name[j]) != text[1].end()) continue;
      dp[1][j] = 1;
    }
  } else {
    int j = Mp[user[1]];
    if (text[1].find((string)name[j]) == text[1].end()) dp[1][j] = 1;
  }
  for (int i = 2; i <= m; i++) {
    if (!strcmp(user[i], "?")) {
      for (int j = 1; j <= n; j++) {
        if (text[i].find((string)name[j]) != text[i].end()) continue;
        for (int k = 1; k <= n; k++) {
          if (k == j) continue;
          dp[i][j] |= dp[i - 1][k];
        }
      }
    } else {
      int j = Mp[user[i]];
      if (text[i].find((string)name[j]) != text[i].end()) continue;
      for (int k = 1; k <= n; k++) {
        if (k == j) continue;
        dp[i][j] |= dp[i - 1][k];
      }
    }
  }
  int flag = 0;
  for (int i = 1; i <= n; i++) flag |= dp[m][i];
  if (!flag) {
    puts("Impossible");
  } else {
    for (int i = 1; i <= n; i++)
      if (dp[m][i]) {
        print(m, i);
        break;
      }
    for (int i = 1; i <= m; i++) printf("%s:%s\n", user[i], orgtext[i]);
  }
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    read();
    solve();
  }
  return 0;
}
