#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void chmin(T &l, T r) {
  l = min(l, r);
}
template <typename T>
inline void chmax(T &l, T r) {
  l = max(l, r);
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v) is >> it;
  return is;
}
void solve() {
  int n;
  cin >> n;
  vector<string> users(n);
  cin >> users;
  map<string, int> z;
  for (int i = (0); i < (n); i++) z[users[i]] = i;
  int m;
  cin >> m;
  vector<string> U(m), S(m);
  string txt;
  getline(cin, txt);
  for (int i = (0); i < (m); i++) {
    getline(cin, txt);
    int cnm = 0;
    for (int j = (0); j < (txt.size()); j++)
      if (txt[j] == ':') {
        cnm = j;
        break;
      }
    U[i] = txt.substr(0, cnm);
    S[i] = txt.substr(cnm + 1, txt.size() - U[i].size() - 1);
  }
  vector<vector<int>> dp(m + 1, vector<int>(n, -1));
  for (int i = (0); i < (n); i++) dp[0][i] = -2;
  for (int i = (0); i < (m); i++) {
    vector<int> exist(n, 0);
    for (int j = (0); j < (n); j++) {
      int sz = users[j].size();
      for (int k = 0; k + sz <= S[i].size(); k++) {
        bool ok = true;
        for (int kk = 0; kk < sz; kk++) {
          if (S[i][k + kk] != users[j][kk]) {
            ok = false;
            break;
          }
        }
        if (ok)
          if (k + sz == S[i].size() || S[i][k + sz] == ' ' ||
              S[i][k + sz] == '.' || S[i][k + sz] == ',' ||
              S[i][k + sz] == '!' || S[i][k + sz] == '?' ||
              S[i][k + sz] == '\r' || S[i][k + sz] == '\n')
            if (k == 0 || S[i][k - 1] == ' ' || S[i][k - 1] == '.' ||
                S[i][k - 1] == ',' || S[i][k - 1] == '!' ||
                S[i][k - 1] == '?' || S[i][k - 1] == '\r' ||
                S[i][k - 1] == '\n')
              exist[j] = 1;
      }
    }
    if (U[i] == "?") {
      for (int j = (0); j < (n); j++)
        if (dp[i][j] != -1) {
          for (int k = (0); k < (n); k++)
            if (((n == 1 && i == 0) || j != k) && exist[k] == 0) {
              dp[i + 1][k] = j;
            }
        }
    } else {
      int id = z[U[i]];
      if (exist[id] == 1) {
        cout << "Impossible" << endl;
        return;
      }
      for (int j = (0); j < (n); j++)
        if (((n == 1 && i == 0) || id != j) && dp[i][j] != -1) {
          dp[i + 1][id] = j;
          break;
        }
    }
  }
  for (int i = (0); i < (n); i++) {
    if (dp[m][i] != -1) {
      int id = i;
      for (int j = m; j > 0; j--) {
        U[j - 1] = users[id];
        id = dp[j][id];
      }
      for (int j = (0); j < (m); j++) {
        cout << U[j] << ":";
        cout << S[j] << endl;
      }
      return;
    }
  }
  cout << "Impossible" << endl;
  return;
}
int main() {
  int t;
  cin >> t;
  for (int i = (0); i < (t); i++) {
    solve();
  }
  return 0;
}
