#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
int n;
int m;
vector<set<int> > cand;
vector<int> isOnly;
vector<int> discard;
int dp[105][2][105];
int rc[105][2][105];
int solve(int pos, int assigned, int which) {
  if (pos == m) return 1;
  if (dp[pos][assigned][which] != -1) return dp[pos][assigned][which];
  int ans = 0, selected = -1;
  for (set<int>::iterator it = cand[pos].begin(); it != cand[pos].end(); it++) {
    int user = (*it);
    if (assigned && which == user) continue;
    if (solve(pos + 1, 1, user)) {
      selected = user;
      ans = 1;
    }
  }
  rc[pos][assigned][which] = selected;
  return dp[pos][assigned][which] = ans;
}
vector<string> userName;
vector<string> restMsg;
int main() {
  int T;
  string s;
  string line;
  while (cin >> T) {
    while (T--) {
      cin >> n;
      mp.clear();
      userName = vector<string>(n);
      for (int i = 0; i < n; i++) {
        cin >> s;
        mp[s] = i;
        userName[i] = s;
      }
      cin >> m;
      cin.ignore();
      cand = vector<set<int> >(m);
      isOnly = vector<int>(m, 0);
      restMsg = vector<string>(m);
      for (int i = 0; i < m; i++) {
        getline(cin, line);
        restMsg[i] = line;
        for (int j = 0; j < line.length(); j++)
          if (line[j] == ':' || line[j] == '.' || line[j] == ',' ||
              line[j] == '!' || line[j] == '?') {
            line[j] = ' ';
          }
        discard = vector<int>(n, 0);
        stringstream ss(line);
        bool first = true;
        if (restMsg[i][0] == '?') {
          first = false;
          isOnly[i] = 0;
        }
        string aux = "";
        while (ss >> s) {
          if (first) {
            if (s == "?")
              isOnly[i] = 0;
            else {
              isOnly[i] = 1;
              int valid = mp[s];
              for (int j = 0; j < n; j++)
                if (j == valid)
                  discard[j] = 0;
                else
                  discard[j] = 1;
              if (i - 1 >= 0) cand[i - 1].erase(valid);
            }
            first = false;
          } else {
            aux += s;
            if (isOnly[i] == 0 && mp.find(s) != mp.end()) {
              discard[mp[s]] = 1;
            }
          }
        }
        if (i - 1 >= 0 && isOnly[i - 1]) {
          set<int>::iterator it = cand[i - 1].begin();
          discard[*it] = 1;
        }
        for (int j = 0; j < n; j++)
          if (!discard[j]) cand[i].insert(j);
      }
      memset(dp, -1, sizeof dp);
      int posib = solve(0, 0, 0);
      if (posib == 0)
        cout << "Impossible" << '\n';
      else {
        int pos = 0, assigned = 0, which = 0;
        for (int i = 0; i < m; i++) {
          int selected = rc[pos][assigned][which];
          if (restMsg[i][0] == '?')
            cout << userName[selected] + restMsg[i].substr(1) << '\n';
          else
            cout << restMsg[i] << '\n';
          pos++;
          assigned = 1;
          which = selected;
        }
      }
    }
  }
  return 0;
}
