#include <bits/stdc++.h>
using namespace std;
int T, f[110][110];
char buf[110];
bool old[110], ok[110];
string user[110], chat[110];
inline bool weird(char c) {
  if ('a' <= c && c <= 'z') return 0;
  if ('A' <= c && c <= 'Z') return 0;
  if ('0' <= c && c <= '9') return 0;
  return 1;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    int N;
    scanf("%d", &N);
    map<string, int> id;
    for (int i = (0); i < (N); i++) {
      scanf(" %s", buf);
      user[i] = string(buf);
      id[user[i]] = i;
    }
    int M;
    scanf("%d\n", &M);
    for (int i = (0); i < (N + 1); i++) ok[i] = 1;
    for (int z = (0); z < (M); z++) {
      for (int i = (0); i < (N + 1); i++) old[i] = ok[i], ok[i] = 0;
      for (int i = (0); i < (N); i++) {
        for (int j = (0); j < (N + 1); j++)
          if (i != j && old[j]) {
            ok[i] = 1;
            f[z][i] = j;
            break;
          }
      }
      string nm, s, o;
      int c = getchar();
      while (c != ':') nm += (char)c, c = getchar();
      while (c != '\n') {
        if (weird((char)c)) {
          if (id.count(o)) ok[id[o]] = 0;
          o.clear();
        } else {
          o += (char)c;
        }
        s += (char)c, c = getchar();
      }
      if (id.count(o)) ok[id[o]] = 0;
      o.clear();
      if (nm != "?") {
        for (int i = (0); i < (N); i++)
          if (user[i] != nm) {
            ok[i] = 0;
          }
      }
      chat[z] = string(s);
    }
    int c = -1;
    for (int i = (0); i < (N); i++)
      if (ok[i]) c = i;
    if (c == -1)
      puts("Impossible");
    else {
      for (int z = M - 1; z >= 0; z--) {
        chat[z] = user[c] + chat[z];
        c = f[z][c];
      }
      for (int z = (0); z < (M); z++) printf("%s\n", chat[z].c_str());
    }
  }
  return 0;
}
