#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int T = 0; T < t; T++) {
    int n, m;
    cin >> n;
    vector<string> users(n);
    for (int i = 0; i < n; i++) cin >> users[i];
    cin >> m;
    string curr;
    vector<string> messages(m);
    vector<vector<int>> memo(m, vector<int>(n, -1));
    for (int i = 0; i < m; i++) {
      getline(cin, curr);
      if (curr.empty()) getline(cin, curr);
      string speaker;
      set<string> other;
      int j = 0;
      while (curr[j] != ':') speaker += curr[j++];
      messages[i] = curr.substr(speaker.size());
      string temp;
      while (++j < curr.size()) {
        if (curr[j] == '.' or curr[j] == ',' or curr[j] == '!' or
            curr[j] == '?' or curr[j] == ' ') {
          other.insert(temp);
          temp.clear();
        } else
          temp += curr[j];
      }
      other.insert(temp);
      for (int j = 0; j < n; j++) {
        if (speaker == "?" and other.count(users[j]))
          continue;
        else if (speaker != "?" and speaker != users[j])
          continue;
        if (i == 0) {
          memo[i][j] = j;
          continue;
        }
        for (int k = 0; k < n; k++) {
          if (k != j and memo[i - 1][k] != -1) {
            memo[i][j] = k;
            break;
          }
        }
      }
    }
    vector<string> result(m);
    int cU = -1;
    for (int j = 0; j < n; j++) {
      if (memo[m - 1][j] != -1) {
        cU = j;
        break;
      }
    }
    if (cU == -1) {
      cout << "Impossible" << endl;
    } else {
      result[m - 1] = users[cU] + messages[m - 1];
      for (int i = m - 2; i >= 0; i--) {
        cU = memo[i + 1][cU];
        result[i] = users[cU] + messages[i];
      }
      for (int i = 0; i < m; i++) cout << result[i] << endl;
    }
  }
}
