#include <bits/stdc++.h>
using namespace std;
int T, n, m, memo[105][105], retuser[105];
string name[105], message[105];
bool dp[105][105];
bool isok(char c) {
  if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||
      (c >= 'A' && c <= 'Z'))
    return true;
  return false;
}
int cal(int id, int bef) {
  if (id > m) return memo[id][bef] = 1;
  int &ret = memo[id][bef];
  if (ret != -1) return ret;
  ret = 0;
  for (int i = 1; i <= n; i++) {
    if (!dp[id][i] && bef != i) {
      ret = max(ret, cal(id + 1, i));
    }
  }
  return ret;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    memset(dp, 0, sizeof(dp));
    scanf("%d", &n);
    map<string, int> mp;
    for (int i = 1; i <= n; i++) {
      cin >> name[i];
      mp[name[i]] = i;
    }
    scanf("%d", &m);
    getchar();
    for (int i = 1; i <= m; i++) {
      getline(cin, message[i]);
      int len = message[i].length();
      string temp = "";
      if (message[i][0] != '?') {
        for (int k = 0; k < len; k++) {
          if (isok(message[i][k]))
            temp += message[i][k];
          else
            break;
        }
        for (int k = 1; k <= n; k++) dp[i][k] = true;
        dp[i][mp[temp]] = false;
      } else {
        for (int k = 0; k < len; k++) {
          if (isok(message[i][k]))
            temp += message[i][k];
          else {
            int id = 0;
            if (mp.count(temp)) id = mp[temp];
            dp[i][id] = true;
            temp.clear();
          }
        }
        int id = 0;
        if (mp.count(temp)) id = mp[temp];
        dp[i][id] = true;
      }
    }
    memset(memo, -1, sizeof(memo));
    int ret = cal(1, 0);
    if (ret != 1)
      puts("Impossible");
    else {
      int user;
      for (int i = 1; i <= n; i++) {
        if (memo[m + 1][i] == 1) {
          user = i;
          break;
        }
      }
      int id = m;
      while (1) {
        retuser[id] = user;
        if (id == 1) break;
        for (int i = 1; i <= n; i++) {
          if (user != i && memo[id][i] == 1) {
            user = i;
            break;
          }
        }
        id--;
      }
      for (int i = 1; i <= m; i++) {
        cout << name[retuser[i]];
        int j;
        for (j = 0; j < message[i].size(); j++)
          if (message[i][j] == ':') break;
        while (j < message[i].size()) putchar(message[i][j++]);
        puts("");
      }
    }
  }
  return 0;
}
