#include <bits/stdc++.h>
using namespace std;
map<string, int> nametoid;
string all[111], names[111];
int can[111][111], use[111][111];
int send[111];
bool check(const char& c) {
  if (c >= 'a' && c <= 'z') return true;
  if (c >= 'A' && c <= 'Z') return true;
  if (c >= '0' && c <= '9') return true;
  return false;
}
int n, m;
bool dfs(int sender, int pos) {
  if (pos == 1)
    return true;
  else if (use[sender][pos])
    return false;
  use[sender][pos] = 1;
  if (send[pos - 1]) {
    for (int i = 1; i <= n; ++i) {
      if (send[pos - 1] != i || can[pos - 1][i]) continue;
      if (i == sender) continue;
      if (dfs(i, pos - 1)) {
        int len = names[i].length();
        cout << names[i] << all[pos - 1].substr(len) << endl;
        return true;
      }
    }
  } else {
    for (int i = 1; i <= n; ++i) {
      if (can[pos - 1][i]) continue;
      if (i == sender) continue;
      if (dfs(i, pos - 1)) {
        cout << names[i] << all[pos - 1].substr(1) << endl;
        return true;
      }
    }
  }
  return false;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    nametoid.clear();
    memset(send, 0, sizeof(send));
    memset(can, 0, sizeof(can));
    memset(use, 0, sizeof(use));
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
      string name;
      cin >> name;
      names[i] = name;
      nametoid[name] = i;
    }
    scanf("%d", &m);
    getchar();
    for (int i = 1; i <= m; ++i) {
      string message, tmp = "";
      getline(cin, message);
      all[i] = message;
      int len = message.length();
      for (int j = 0; j < len; ++j) {
        if (message[j] == ':') {
          if (nametoid[tmp]) send[i] = nametoid[tmp];
          tmp = "";
        } else if (!check(message[j])) {
          if (nametoid[tmp]) can[i][nametoid[tmp]] = 1;
          tmp = "";
        } else if (check(message[j]) && j == len - 1) {
          tmp += message[j];
          if (nametoid[tmp]) can[i][nametoid[tmp]] = 1;
          tmp = "";
        } else
          tmp += message[j];
      }
    }
    if (send[m]) {
      if (!dfs(send[m], m))
        puts("Impossible");
      else
        cout << names[send[m]] << all[m].substr(names[send[m]].length())
             << endl;
    } else {
      int flag = 0;
      for (int i = 1; i <= n; ++i) {
        if (!can[m][i]) flag = dfs(i, m);
        if (flag) {
          cout << names[i] << all[m].substr(1) << endl;
          break;
        }
      }
      if (!flag) puts("Impossible");
    }
  }
}
