#include <bits/stdc++.h>
using namespace std;
unordered_set<int> dp;
bool rec(vector<vector<int>> &as, int p, int prev) {
  if (p >= as.size()) {
    return true;
  }
  assert(prev < 101);
  int k = p * 101 + prev;
  if (dp.count(k)) return false;
  for (int i = 0; i < as[p].size(); ++i) {
    bool res = false;
    if (prev != as[p][i]) res = rec(as, p + 1, as[p][i]);
    if (res) {
      int s = as[p][i];
      as[p].clear();
      as[p].push_back(s);
      return true;
    }
  }
  dp.insert(k);
  return false;
}
int main(int argc, const char *argv[]) {
  std::ios::sync_with_stdio(false);
  int T;
  cin >> T;
  for (int t = 0; t < T; ++t) {
    int n;
    cin >> n;
    map<string, int> names;
    map<int, string> ids;
    for (int i = 0; i < n; ++i) {
      string name;
      cin >> name;
      names[name] = i;
      ids[i] = name;
    }
    int m;
    cin >> m;
    vector<vector<int>> as;
    vector<string> msgs;
    string dummy;
    getline(cin, dummy);
    for (int i = 0; i < m; ++i) {
      string msg;
      getline(cin, msg);
      string id = msg.substr(0, msg.find(':'));
      msg = msg.substr(msg.find(':') + 1);
      vector<int> a;
      if (id != "?") {
        assert(names.count(id) == 1);
        a.push_back(names[id]);
      } else {
        string p = msg;
        replace(p.begin(), p.end(), '.', ' ');
        replace(p.begin(), p.end(), ',', ' ');
        replace(p.begin(), p.end(), '!', ' ');
        replace(p.begin(), p.end(), '?', ' ');
        vector<string> tok;
        string b;
        stringstream stream(p);
        while (stream >> b) {
          tok.push_back(b);
        }
        for (auto j = names.begin(); j != names.end(); ++j) {
          bool good = true;
          for (int k = 0; k < tok.size(); ++k) {
            if (tok[k] == j->first) {
              good = false;
              break;
            }
          }
          if (good) {
            a.push_back(j->second);
          }
        }
      }
      msgs.push_back(msg);
      as.push_back(a);
    }
    dp.clear();
    bool res = rec(as, 0, -1);
    if (!res) {
      cout << "Impossible" << endl;
    } else {
      for (int i = 0; i < as.size(); ++i) {
        assert(as[i].size() == 1);
        cout << ids[as[i][0]] << ":" << msgs[i] << endl;
      }
    }
  }
  return 0;
}
