#include <bits/stdc++.h>
int n, m;
std::map<std::string, int> h;
std::string name[(110)], txt[(110)];
bool f[(110)][(110)];
int g[(110)][(110)];
void print(int i, int j) {
  if (i == 0) return;
  print(i - 1, g[i][j]);
  std::cout << name[j] << txt[i - 1] << std::endl;
}
void work() {
  scanf("%d", &n);
  h.clear();
  memset(f, 0, sizeof(f));
  memset(g, 0, sizeof(g));
  for (int i = 0; i < n; ++i) {
    std::cin >> name[i];
    h[name[i]] = i;
  }
  scanf("%d\n", &m);
  f[0][n] = 1;
  for (int i = 0; i < m; ++i) {
    std::string usr, msg;
    getline(std::cin, msg);
    txt[i].clear();
    int p = 0;
    while (p < msg.size() && msg[p] != ':') {
      usr += msg[p++];
    }
    int q = p;
    while (q < msg.size()) {
      txt[i] += msg[q++];
    }
    std::set<int> mention;
    while (p < msg.size()) {
      std::string word;
      while (p < msg.size() && isalnum(msg[p])) {
        word += msg[p++];
      }
      if (h.count(word)) {
        mention.insert(h[word]);
      }
      if (p < msg.size()) ++p;
    }
    if (usr == "?") {
      for (int j = 0; j <= n; ++j) {
        if (!f[i][j]) continue;
        for (int k = 0; k < n; ++k) {
          if (mention.count(k) || k == j) continue;
          f[i + 1][k] = 1;
          g[i + 1][k] = j;
        }
      }
    } else {
      if (!h.count(usr)) {
        puts("Impossible");
        return;
      }
      int id = h[usr];
      for (int j = 0; j <= n; ++j) {
        if (f[i][j] && id != j) {
          f[i + 1][id] = 1;
          g[i + 1][id] = j;
        }
      }
    }
  }
  int x = -1;
  for (int i = 0; i < n; ++i) {
    if (f[m][i]) {
      x = i;
      break;
    }
  }
  if (x == -1) {
    puts("Impossible");
  } else {
    print(m, x);
  }
}
int main() {
  int test;
  scanf("%d", &test);
  while (test--) {
    work();
  }
  return 0;
}
