#include <bits/stdc++.h>
using namespace std;
inline long long gcd(long long a, long long b) {
  return (b ? gcd(b, a % b) : a);
}
inline long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
int n, m, k;
string s[102], msg[102];
vector<int> adj[102];
map<string, int> mapa;
void nomes(int v[], string str) {
  str.push_back(' ');
  string aux;
  for (int i = 2; i < str.size(); i++) {
    if (str[i] == ' ' || str[i] == '!' || str[i] == '?' || str[i] == '.' ||
        str[i] == ',') {
      if (mapa.count(aux) != 0) v[mapa[aux]] = 1;
      aux.clear();
    } else {
      aux.push_back(str[i]);
    }
  }
}
void trata(int idx, string str) {
  int pos = 0;
  if (str[pos] != '?') {
    while (str[pos] != ':') pos++;
    adj[idx].push_back(mapa[str.substr(0, pos)]);
  } else {
    int v[n];
    memset(v, 0, sizeof v);
    nomes(v, str);
    for (int i = 0; i < n; i++) {
      if (v[i] == 0) adj[idx].push_back(i);
    }
  }
}
int dp[103][103], memo[103][103];
int solve(int a, int b) {
  if (b == 0) return true;
  if (dp[a][b] != -1) return dp[a][b];
  for (int i = 0, x; i < adj[b - 1].size(); i++) {
    x = adj[b - 1][i];
    if (x != a && solve(x, b - 1)) {
      memo[a][b] = x;
      return dp[a][b] = true;
    }
  }
  return dp[a][b] = false;
}
int main() {
  cin >> k;
  while (k--) {
    mapa.clear();
    memset(dp, -1, sizeof dp);
    for (int i = 0; i < m; i++) adj[i].clear();
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      mapa[s[i]] = i;
    }
    cin >> m;
    cin.ignore();
    for (int i = 0; i < m; i++) {
      getline(cin, msg[i]);
      trata(i, msg[i]);
    }
    int ans = -1;
    for (int i = 0; i < adj[m - 1].size(); i++) {
      if (solve(adj[m - 1][i], m - 1)) {
        ans = adj[m - 1][i];
        break;
      }
    }
    if (ans == -1) {
      cout << "Impossible\n";
    } else {
      vector<string> resposta;
      int pos = m - 1;
      while (pos >= 0) {
        if (msg[pos][0] == '?') {
          string aux = s[ans];
          for (int i = 1; i < msg[pos].size(); i++) aux.push_back(msg[pos][i]);
          resposta.push_back(aux);
        } else {
          resposta.push_back(msg[pos]);
        }
        ans = memo[ans][pos];
        pos--;
      }
      for (int i = m - 1; i >= 0; i--) cout << resposta[i] << endl;
    }
  }
}
