#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 150;
map<string, int> mp;
int dp[N][N];
int memo[N][N];
int res[N];
int flag;
int n, m;
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() {
  int T;
  scanf("%d", &T);
  while (T--) {
    mp.clear();
    memset(dp, 0, sizeof(dp));
    memset(memo, -1, sizeof(memo));
    scanf("%d", &n);
    getchar();
    string name[N];
    for (int i = 0; i < n; i++) {
      cin >> name[i + 1];
      mp[name[i + 1]] = i + 1;
    }
    scanf("%d", &m);
    getchar();
    char s[N];
    string rec[N];
    flag = 1;
    for (int i = 1; i <= m; i++) {
      gets(s);
      int len = strlen(s);
      int j;
      string a;
      for (j = 0; j < len; j++) {
        if (s[j] == ':') break;
        a += s[j];
      }
      int begin = j;
      for (int j = begin; j < len; j++) rec[i] += s[j];
      if (a == "?") {
        for (int k = 1; k <= n; k++) dp[i][k] = 1;
        for (j = begin; j < len; j++) {
          if (!isalnum(s[j])) continue;
          string b;
          int k;
          for (k = j; k < len; k++) {
            if (!isalnum(s[k])) break;
            b += s[k];
          }
          j = k;
          if (mp[b]) dp[i][mp[b]] = 0;
        }
      } else {
        int id = mp[a];
        dp[i][id] = 1;
        for (j = begin; j < len; j++) {
          if (!isalnum(s[j])) continue;
          string b;
          int k;
          for (k = j; k < len; k++) {
            if (!isalnum(s[k])) break;
            b += s[k];
          }
          j = k;
          if (b == a) {
            flag = 0;
            break;
          }
        }
      }
    }
    if (!flag)
      puts("Impossible");
    else {
      int ans = cal(1, 0);
      if (!ans)
        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) {
          res[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[res[i]] << rec[i] << endl;
      }
    }
  }
  return 0;
}
