#include <bits/stdc++.h>
using namespace std;
vector<string> split(const string& input, const string& reText) {
  regex re(reText);
  sregex_token_iterator first{(input).begin(), (input).end(), re, -1}, last;
  return {first, last};
}
void solve() {
  int n, m;
  cin >> n;
  vector<string> names(n);
  for (auto& a : names) cin >> a;
  cin >> m;
  auto getIdx = [&](string i) {
    int idx = -1;
    auto pos = find((names).begin(), (names).end(), i);
    if (names.end() != pos) idx = distance(names.begin(), pos);
    return idx;
  };
  string tmp;
  getline(cin, tmp);
  vector<string> message(m);
  for (auto& a : message) getline(cin, a);
  vector<vector<bool>> canUse(m, vector<bool>(n, false));
  vector<string> bodies;
  for (int i = (0), i_end_ = (message.size()); i < i_end_; ++i) {
    auto msg = split(message[i], "[:]");
    auto name = msg[0];
    auto body = msg[1];
    auto idx = getIdx(name);
    bodies.push_back(body);
    if (idx >= 0) {
      canUse[i][idx] = true;
      continue;
    } else {
      canUse[i] = vector<bool>(n, true);
      auto token = split(body, "[^A-Za-z0-9]+");
      set<string> tokens(make_move_iterator(token.begin()),
                         make_move_iterator(token.end()));
      for (int k = 0; k < n; k++) {
        if (tokens.find(names[k]) != tokens.end()) canUse[i][k] = false;
      }
    }
  }
  vector<vector<int>> cache(m, vector<int>(n, -1));
  for (int i = (0), i_end_ = (n); i < i_end_; ++i)
    if (canUse[0][i]) cache[0][i] = 0;
  for (int i = (0), i_end_ = (m - 1); i < i_end_; ++i) {
    for (int a = (0), a_end_ = (n); a < a_end_; ++a) {
      if (cache[i][a] == -1) continue;
      for (int b = (0), b_end_ = (n); b < b_end_; ++b) {
        if (a == b) continue;
        if (canUse[i + 1][b]) cache[i + 1][b] = a;
      }
    }
  }
  auto pos = find_if((cache[m - 1]).begin(), (cache[m - 1]).end(),
                     [](int i) { return i != -1; });
  if (pos == cache[m - 1].end()) {
    cout << "Impossible" << endl;
  } else {
    auto idx = distance(cache[m - 1].begin(), pos);
    vector<string> nameout;
    for (int msgIdx = m - 1; msgIdx >= 0; msgIdx--) {
      nameout.push_back(names[idx]);
      idx = cache[msgIdx][idx];
    }
    reverse((nameout).begin(), (nameout).end());
    for (int i = (0), i_end_ = (m); i < i_end_; ++i) {
      cout << nameout[i] << ":" << bodies[i] << endl;
    }
  }
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
