#include <bits/stdc++.h>
using namespace std;
vector<string> mes;
vector<string> names;
map<string, int> mp;
vector<int> send;
int a[101][101];
inline bool is_splt(char c) {
  if (c == '.') {
    return true;
  }
  if (c == ',') {
    return true;
  }
  if (c == '!') {
    return true;
  }
  if (c == ':') {
    return true;
  }
  if (c == ' ') {
    return true;
  }
  if (c == '?') {
    return true;
  }
  if (c == '@') {
    return true;
  }
  return false;
}
inline void ch(int x, int st) {
  string tmp = "";
  int m = mes[x].size();
  for (int i = st; i <= m; i++) {
    if (i == m || is_splt(mes[x][i])) {
      if (mp.count(tmp) != 0) {
        a[x][mp[tmp]] = -1;
      }
      tmp = "";
    } else {
      tmp += mes[x][i];
    }
  }
  mes[x] = mes[x].substr(st, mes[x].size() - st);
}
void solve() {
  for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 101; j++) {
      a[i][j] = 0;
    }
  }
  mp.clear();
  send.clear();
  names.clear();
  mes.clear();
  int n = 0;
  cin >> n;
  names.assign(n, "");
  for (int i = 0; i < n; i++) {
    cin >> names[i];
    mp[names[i]] = i;
  }
  int m = 0;
  cin >> m;
  string tmp15431543 = "";
  getline(cin, tmp15431543);
  mes.assign(m, "");
  send.assign(m, 0);
  for (int i = 0; i < m; i++) {
    getline(cin, mes[i]);
    int p = mes[i].find(":");
    string nm = mes[i].substr(0, p);
    if (nm != "?") {
      for (int j = 0; j < n; j++) {
        if (names[j] != nm) {
          a[i][j] = -1;
        } else {
          a[i][j] = 1;
          send[i] = j;
        }
      }
    } else {
      send[i] = -1;
    }
    ch(i, p);
  }
  for (int ttt = 0; ttt < 103; ttt++) {
    for (int i = 0; i < m - 1; i++) {
      if (send[i] != -1) {
        a[i + 1][send[i]] = -1;
      }
    }
    for (int i = m - 1; i >= 1; i--) {
      if (send[i] != -1) {
        a[i - 1][send[i]] = -1;
      }
    }
    for (int i = 0; i < m; i++) {
      if (send[i] == -1) {
        int cnt = 0;
        for (int j = 0; j < n; j++) {
          if (-1 == a[i][j]) {
            cnt++;
          }
        }
        if (cnt == n - 1) {
          for (int j = 0; j < n; j++) {
            if (-1 != a[i][j]) {
              send[i] = j;
              a[i][j] = 1;
            }
          }
        }
      }
    }
  }
  for (int i = 0; i < m; i++) {
    if (send[i] == -1) {
      for (int j = 0; j < n; j++) {
        if (a[i][j] == 0 && a[i - 1][j] != 1 && a[i + 1][j] != 1) {
          a[i][j] = 1;
          send[i] = j;
          break;
        }
      }
    }
    if (send[i] == -1) {
      cout << "Impossible\n";
      return;
    }
  }
  for (int i = 1; i < m - 1; i++) {
    if (send[i] == send[i + 1] || send[i] == send[i - 1]) {
      cout << "Impossible\n";
      return;
    }
  }
  for (int i = 0; i < m; i++) {
    cout << names[send[i]] << mes[i] << "\n";
  }
}
int main() {
  int t = 0;
  cin >> t;
  for (int i = 0; i < t; i++) {
    solve();
  }
  return 0;
}
