#include <bits/stdc++.h>
using namespace std;
const int N = 110;
char str[110];
int v[N];
char *pch;
int adj[N][N];
int pos[N], m, n;
int dp[N][N];
int solve(int at, int last) {
  if (at == m) return dp[at][last] = 1;
  int &ret = dp[at][last];
  if (ret != -1) return ret;
  if (v[at]) {
    if (last == pos[at])
      return ret = 0;
    else
      return ret = solve(at + 1, pos[at]);
  }
  for (int i = 0; i < n; i++) {
    if (last == i) continue;
    if (adj[at][i]) continue;
    if (solve(at + 1, i)) return ret = 1;
  }
  return ret = 0;
}
vector<string> names;
vector<string> words;
void build(int at, int last) {
  if (at == m) return;
  if (v[at]) {
    cout << words[at] << endl;
    build(at + 1, pos[at]);
  } else {
    for (int i = 0; i < n; i++) {
      if (last == i) continue;
      if (adj[at][i]) continue;
      if (dp[at + 1][i]) {
        cout << names[i];
        cout << words[at].substr(1, words[at].size() - 1) << endl;
        build(at + 1, i);
        return;
      }
    }
  }
}
int main(void) {
  int t;
  cin >> t;
  while (t--) {
    names.clear();
    words.clear();
    memset(dp, -1, sizeof dp);
    memset(adj, 0, sizeof adj);
    cin >> n;
    map<string, int> mapa;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      mapa[s] = i;
      names.push_back(s);
    }
    cin >> m;
    getchar();
    for (int i = 0; i < m; i++) {
      string s;
      getline(cin, s);
      for (int j = 0; j < s.size(); j++) str[j] = s[j];
      str[s.size()] = '\0';
      words.push_back(s);
      if (s[0] == '?')
        v[i] = 0;
      else
        v[i] = 1;
      pch = strtok(str, " .,!?:");
      while (pch != NULL) {
        string ss = pch;
        pch = strtok(NULL, " .,!?:");
        if (v[i] == 1) {
          pos[i] = mapa[ss];
          break;
        }
        if (mapa.count(ss)) adj[i][mapa[ss]] = 1;
      }
    }
    int r = -1;
    if (v[0]) {
      r = solve(1, pos[0]);
      if (!r)
        printf("Impossible\n");
      else {
        cout << words[0] << endl;
        build(1, pos[0]);
      }
    } else {
      for (int i = 0; i < n; i++) {
        if (!adj[0][i] && solve(1, i)) {
          r = i;
          break;
        }
      }
      if (r == -1)
        printf("Impossible\n");
      else {
        cout << names[r];
        cout << words[0].substr(1, words[0].size() - 1) << endl;
        build(1, r);
      }
    }
  }
  return 0;
}
