#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
vector<int> solve(int n, const vector<int> &sender,
                  const vector<vector<int> > &mention) {
  int m = sender.size();
  if (0) {
    for (int i = (int)(0); i < (int)(m); i++) {
      cout << sender[i] << ":";
      for (int j = (int)(0); j < (int)(mention[i].size()); j++) {
        cout << " " << mention[i][j];
      }
      cout << endl;
    }
  }
  vector<vector<int> > dp(m + 1, vector<int>(n, 1));
  for (int i = (int)(0); i < (int)(m); i++) {
    if (sender[i] >= 0) {
      for (int j = (int)(0); j < (int)(n); j++) {
        dp[i + 1][j] = 0;
      }
      dp[i + 1][sender[i]] = 1;
    } else {
      for (int j = (int)(0); j < (int)(mention[i].size()); j++) {
        dp[i + 1][mention[i][j]] = 0;
      }
    }
    if (i >= 1) {
      for (int j = (int)(0); j < (int)(n); j++) {
        bool ok = false;
        for (int k = (int)(0); k < (int)(n); k++) {
          if (j == k) continue;
          if (dp[i][k] == 1) {
            ok = true;
            break;
          }
        }
        dp[i + 1][j] &= ok ? 1 : 0;
      }
    }
  }
  bool ok = false;
  int v = -1;
  for (int i = (int)(0); i < (int)(n); i++) {
    if (dp[m][i]) {
      ok = true;
      v = i;
    }
  }
  if (not ok) {
    return vector<int>();
  }
  vector<int> ret(m, -1);
  ret[m - 1] = v;
  for (int i = m - 2; i >= 0; --i) {
    for (int j = (int)(0); j < (int)(n); j++) {
      if (j == ret[i + 1]) continue;
      if (dp[i + 1][j]) {
        ret[i] = j;
      }
    }
  }
  return ret;
}
int main(void) {
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vector<string> name(n);
    map<string, int> name_v;
    for (int i = (int)(0); i < (int)(n); i++) {
      cin >> name[i];
      name_v[name[i]] = i;
    }
    int m;
    cin >> m;
    cin.ignore();
    vector<string> lines(m);
    vector<int> sender(m);
    vector<vector<int> > mention(m);
    for (int i = (int)(0); i < (int)(m); i++) {
      getline(cin, lines[i]);
      stringstream is(lines[i]);
      string t;
      getline(is, t, ':');
      if (t == "?") {
        sender[i] = -1;
      } else {
        assert(name_v.count(t));
        sender[i] = name_v[t];
      }
      vector<int> words;
      string rest;
      getline(is, rest);
      size_t pos;
      size_t prev = 0;
      while ((pos = rest.find_first_of(" ,.?!", prev)) != string::npos) {
        if (pos > prev) {
          string word = rest.substr(prev, pos - prev);
          if (name_v.count(word)) {
            mention[i].push_back(name_v[word]);
          }
        }
        prev = pos + 1;
      }
      if (prev < rest.size()) {
        string word = rest.substr(prev);
        if (name_v.count(word)) {
          mention[i].push_back(name_v[word]);
        }
      }
    }
    vector<int> sol = solve(n, sender, mention);
    if (sol.size() == 0) {
      cout << "Impossible" << endl;
    } else {
      for (int j = (int)(0); j < (int)(m); j++) {
        if (sender[j] == -1) {
          cout << name[sol[j]];
          cout << lines[j].substr(1) << endl;
        } else {
          cout << lines[j] << endl;
        }
      }
    }
  }
}
