#include <bits/stdc++.h>
using namespace std;
int m;
vector<string> users;
vector<string> msgs;
vector<string> owners(101);
map<string, int> mp;
bool vis[1001][1001];
int mem[1001][1001];
void printResults() {
  for (int i = 0; i < msgs.size(); i++) {
    cout << owners[i] << msgs[i].substr(msgs[i].find(":")) << endl;
  }
}
int getSender(string message) {
  if (message[0] == '?') return -1;
  string s = "";
  for (int i = 0; i < message.size(); i++) {
    if (message[i] == ':') break;
    s += message[i];
  }
  if (mp.find(s) == mp.end()) return -1;
  return mp[s];
}
int findUser(int u, int ms) {
  if (mem[u][ms] != -1) return mem[u][ms];
  string message = msgs[ms];
  string user = users[u];
  for (int i = 0; i < message.size(); i++) {
    if (i + user.size() - 1 >= message.size()) break;
    int cnt = 0;
    for (int j = 0; j < user.size(); j++) {
      if (message[i + j] == user[j])
        cnt++;
      else
        break;
    }
    if (cnt == user.size()) {
      if ((i - 1 >= 0) && !(message[i - 1] == ' ' || message[i - 1] == '!' ||
                            message[i - 1] == '.' || message[i - 1] == '?' ||
                            message[i - 1] == ',' || message[i - 1] == ':'))
        continue;
      if ((i + user.size()) < message.size() &&
          !(message[i + user.size()] == ' ' ||
            message[i + user.size()] == '!' ||
            message[i + user.size()] == '.' ||
            message[i + user.size()] == '?' ||
            message[i + user.size()] == ',' || message[i + user.size()] == ':'))
        continue;
      return (mem[u][ms] = 1);
    }
  }
  return (mem[u][ms] = 0);
}
bool solve(int i, int j) {
  if (i >= m) {
    printResults();
    return true;
  }
  if (vis[i][j]) return false;
  vis[i][j] = true;
  int sender = getSender(msgs[i]);
  if (sender != -1 && sender == j)
    return false;
  else if (sender != -1) {
    owners[i] = users[sender];
    return solve(i + 1, sender);
  }
  for (int a = 0; a < users.size(); a++) {
    if (!findUser(a, i) && a != j) {
      owners[i] = users[a];
      if (solve(i + 1, a)) return true;
    }
  }
  return false;
}
int main() {
  cin.sync_with_stdio();
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    users.clear();
    msgs.clear();
    mp.clear();
    memset(vis, 0, sizeof vis);
    memset(mem, -1, sizeof mem);
    int t;
    cin >> t;
    string s;
    for (int j = 0; j < t; j++) {
      cin >> s;
      users.push_back(s);
      mp[s] = j;
    }
    cin >> m;
    for (int j = 0; j < m + 1; j++) {
      string s;
      getline(cin, s);
      msgs.push_back(s);
    }
    if (msgs[0].size() == 0) msgs.erase(msgs.begin());
    int sender = getSender(msgs[0]);
    if (sender != -1) {
      owners[0] = users[sender];
      if (!solve(1, sender)) cout << "Impossible" << endl;
    } else {
      bool flag = false;
      for (int j = 0; j < users.size(); j++) {
        if (!findUser(j, 0)) {
          owners[0] = users[j];
          if (solve(1, j)) {
            flag = true;
            break;
          }
        }
      }
      if (!flag) cout << "Impossible" << endl;
    }
  }
}
