#include <bits/stdc++.h>
using namespace std;
bool yes;
int t, n, m, len, l, sender[105];
map<string, int> msi;
map<int, string> mis;
string str, user[105], text[105];
set<int> mention[105];
int ans[105];
int dp[105][105];
char c;
bool solve(int pos, int idx) {
  if (pos == m) return 1;
  if (dp[pos][idx] != -1) return dp[pos][idx];
  if (mention[pos].find(idx) != mention[pos].end()) return 0;
  dp[pos][idx] = 0;
  if (pos + 1 < m && sender[pos + 1] != -1) {
    if (idx == sender[pos + 1]) return 0;
    dp[pos][idx] = solve(pos + 1, sender[pos + 1]);
  } else {
    for (int i = 0; i < n; ++i) {
      if (i != idx) {
        dp[pos][idx] |= solve(pos + 1, i);
        if (dp[pos][idx]) break;
      } else if (pos == m - 1)
        dp[pos][idx] = true;
    }
  }
  if (dp[pos][idx]) ans[pos] = idx;
  return dp[pos][idx];
}
int main() {
  scanf("%d%c", &t, &c);
  while (t--) {
    scanf("%d%c", &n, &c);
    memset(sender, -1, sizeof sender);
    memset(dp, -1, sizeof dp);
    for (int i = 0; i < n; ++i) {
      cin >> user[i];
      msi[user[i]] = i;
      mis[i] = user[i];
    }
    scanf("%d%c", &m, &c);
    for (int i = 0; i < m; ++i) {
      getline(cin, str, ':');
      if (str.compare("?") == 0)
        sender[i] = -1;
      else
        sender[i] = msi[str];
      getline(cin, text[i]);
    }
    for (int i = 0; i < m; ++i) {
      len = text[i].size();
      for (int j = 0; j < n; ++j) {
        l = user[j].size();
        yes = false;
        for (int k = 0; k < len; ++k) {
          if (text[i][k] == user[j][0] && k + l - 1 < len &&
              text[i].compare(k, l, user[j]) == 0) {
            if (((k - 1 >= 0 && text[i][k - 1] != '.' &&
                  text[i][k - 1] != ',' && text[i][k - 1] != '!' &&
                  text[i][k - 1] != '?' && text[i][k - 1] != ' ') ||
                 (k + l < len && text[i][k + l] != '.' &&
                  text[i][k + l] != ',' && text[i][k + l] != '!' &&
                  text[i][k + l] != '?' && text[i][k + l] != ' '))) {
            } else {
              yes = true;
              break;
            }
          }
        }
        if (yes) mention[i].insert(msi[user[j]]);
      }
    }
    l = 0;
    if (sender[0] != -1)
      l |= solve(0, sender[0]);
    else {
      for (int i = 0; i < n; ++i) {
        l |= solve(0, i);
        if (l) break;
      }
    }
    if (!l)
      cout << "Impossible" << endl;
    else {
      for (int i = 0; i < m; ++i) {
        cout << user[ans[i]] << ":" << text[i] << endl;
      }
    }
    msi.clear(), mis.clear();
    for (int i = 0; i < m; ++i) mention[i].clear();
  }
  return 0;
}
