#include <bits/stdc++.h>
using namespace std;
int getId(const vector<string> &name, const string &s) {
  for (int i = 0; i < name.size(); ++i)
    if (name[i] == s) return i;
  return -1;
}
bool contains(const string &msg, const string &name) {
  for (int i = 0; i <= (int)msg.size() - (int)name.size(); ++i)
    if (msg.substr(i, name.size()) == name) {
      bool left = true;
      if (i > 0 && msg[i - 1] >= 'a' && msg[i - 1] <= 'z') left = false;
      if (i > 0 && msg[i - 1] >= 'A' && msg[i - 1] <= 'Z') left = false;
      if (i > 0 && msg[i - 1] >= '0' && msg[i - 1] <= '9') left = false;
      bool right = true;
      int len = (int)name.size();
      if (i < (int)msg.size() - (int)name.size() && msg[i + len] >= 'a' &&
          msg[i + len] <= 'z')
        right = false;
      if (i < (int)msg.size() - (int)name.size() && msg[i + len] >= 'A' &&
          msg[i + len] <= 'Z')
        right = false;
      if (i < (int)msg.size() - (int)name.size() && msg[i + len] >= '0' &&
          msg[i + len] <= '9')
        right = false;
      if (left && right) return true;
    }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  for (int tt = 0; tt < t; ++tt) {
    int n;
    cin >> n;
    vector<string> name(n);
    for (int i = 0; i < n; ++i) {
      cin >> name[i];
    }
    name.push_back("$");
    int m;
    cin >> m;
    vector<string> msg(m);
    getline(cin, msg[0]);
    for (int i = 0; i < m; ++i) getline(cin, msg[i]);
    vector<string> sender(m);
    for (int i = 0; i < m; ++i) {
      sender[i] = msg[i].substr(0, msg[i].find(':'));
      msg[i] = msg[i].substr(msg[i].find(':') + 1, msg[i].length());
    }
    vector<vector<bool> > dp(m + 1, vector<bool>(n + 1, false));
    vector<vector<int> > prev(m + 1, vector<int>(n + 1, -1));
    dp[0][n] = true;
    for (int i = 0; i < m; ++i) {
      for (int current = 0; current <= n; ++current) {
        if (!dp[i][current]) continue;
        if (sender[i] == "?") {
          for (int id = 0; id < n; ++id) {
            if (dp[i + 1][id]) {
              continue;
            }
            if (!contains(msg[i], name[id]) && current != id) {
              dp[i + 1][id] = true;
              prev[i + 1][id] = current;
            }
          }
        } else {
          int id = getId(name, sender[i]);
          if (current != id) {
            dp[i + 1][id] = true;
            prev[i + 1][id] = current;
          }
        }
      }
    }
    int last = -1;
    for (int i = 0; i < n; ++i) {
      if (dp[m][i]) {
        last = i;
        break;
      }
    }
    if (last == -1) {
      cout << "Impossible" << endl;
    } else {
      for (int i = 0; i < m; ++i) {
        sender[m - i - 1] = name[last];
        last = prev[m - i][last];
      }
      for (int i = 0; i < m; ++i) {
        cout << sender[i] << ":" << msg[i] << endl;
      }
    }
  }
}
