#include <bits/stdc++.h>
using namespace std;
const double PI = 3.141592653589793238463;
int n, m;
vector<vector<char> > dp;
vector<int> said;
set<pair<vector<char>, int> > before;
bool go(int msg) {
  if (msg == m + 1) {
    return true;
  }
  if (said[msg] != -1) {
    return go(msg + 1);
  }
  vector<char> c;
  if (before.find({dp[msg], msg}) != before.end()) {
    return false;
  }
  for (int i = 1; i <= n; ++i) {
    if (dp[msg][i]) {
      said[msg] = i;
      char dummy = dp[msg + 1][i];
      dp[msg + 1][i] = false;
      if (go(msg + 1)) {
        return true;
      }
      said[msg] = -1;
      dp[msg + 1][i] = dummy;
    }
  }
  before.insert({dp[msg], msg});
  return false;
}
vector<string> getnames(string txt) {
  vector<string> names;
  string x = "";
  for (int i = 0; i < txt.size(); ++i) {
    char c = txt[i];
    if (isalnum(c)) {
      x += c;
    } else {
      if (x != "") {
        names.push_back(x);
        x = "";
      }
    }
  }
  if (x != "") {
    names.push_back(x);
    x = "";
  }
  return names;
}
pair<string, string> parse(string message) {
  pair<string, string> dummy;
  int pos = message.find(':');
  dummy.first = message.substr(0, pos);
  dummy.second = message.substr(pos + 1);
  return dummy;
}
void solve() {
  cin >> n;
  vector<string> names(n + 1);
  before.clear();
  map<string, int> id;
  for (int i = 1; i <= n; ++i) {
    cin >> names[i];
    id[names[i]] = i;
  }
  cin >> m;
  getchar();
  string message, user;
  dp.assign(m + 2, vector<char>(n + 1, true));
  said.assign(m + 1, -1);
  vector<string> messages(m + 1);
  for (int i = 1; i <= m; ++i) {
    getline(cin, message);
    auto dummy = parse(message);
    string &user = dummy.first;
    messages[i] = dummy.second;
    vector<string> mentioned = getnames(dummy.second);
    if (user == "?") {
      for (auto xx : mentioned) {
        if (id[xx] != 0) {
          dp[i][id[xx]] = false;
        }
      }
    } else {
      int idx = id[user];
      fill(dp[i].begin(), dp[i].end(), false);
      dp[i][idx] = true;
      dp[i + 1][idx] = false;
      dp[i - 1][idx] = false;
      said[i] = idx;
    }
  }
  if (go(1)) {
    for (int i = 1; i <= m; ++i) {
      cout << names[said[i]] << ":" << messages[i] << endl;
    }
  } else {
    cout << "Impossible" << endl;
  }
  return;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
