#include <bits/stdc++.h>
using namespace std;
inline regex make_regex(const string& user) {
  return regex("\\b" + user + "\\b");
}
void reduce_adjacent(int i, vector<pair<set<string>, string>>& messages) {
  if (i > 0 && messages[i - 1].first.erase(*messages[i].first.begin()) == 1 &&
      messages[i - 1].first.size() == 1) {
    reduce_adjacent(i - 1, messages);
  }
  if (i < messages.size() - 1 &&
      messages[i + 1].first.erase(*messages[i].first.begin()) == 1 &&
      messages[i + 1].first.size() == 1) {
    reduce_adjacent(i + 1, messages);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  for (cin >> t; t--;) {
    int n;
    cin >> n;
    vector<string> users(n);
    for (int i = 0; i < n; ++i) {
      cin >> users[i];
    }
    vector<regex> regexes(n);
    transform(users.begin(), users.end(), regexes.begin(), make_regex);
    int m;
    cin >> m;
    cin.ignore();
    vector<pair<set<string>, string>> messages(m);
    for (int i = 0; i < m; ++i) {
      string line;
      getline(cin, line);
      auto sep = line.find(':');
      string name = line.substr(0, sep);
      messages[i] = {(name == "?" ? set<string>(users.begin(), users.end())
                                  : set<string>({name})),
                     line.substr(sep + 1)};
      if (name == "?") {
        for (int j = 0; j < n; ++j) {
          if (regex_search(messages[i].second, regexes[j])) {
            messages[i].first.erase(users[j]);
          }
        }
      }
    }
    for (int i = 0; i < m; ++i) {
      if (messages[i].first.size() == 1) {
        reduce_adjacent(i, messages);
      }
    }
    if (any_of(messages.begin(), messages.end(),
               [](const pair<set<string>, string>& p) {
                 return p.first.empty();
               })) {
      cout << "Impossible\n";
      continue;
    }
    for (int i = 0; i < m; ++i) {
      if (messages[i].first.size() > 1 && i < messages.size() - 1) {
        messages[i + 1].first.erase(*messages[i].first.begin());
      }
      cout << *messages[i].first.begin() << ":" << messages[i].second << endl;
    }
  }
}
