#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
string users[MAXN];
vector<int> allowed[MAXN];
bool dp[MAXN][MAXN];
bool contains(const string& s, const string& name) {
  int len = s.length();
  string t;
  for (int i = 0; i < len; i++) {
    if (i + name.length() - 1 < len) {
      t = "";
      for (int j = i; j < i + name.length(); j++) t += s[j];
      if (t != name) continue;
      if (i > 0) {
        if (((s[i - 1] >= 'a') && (s[i - 1] <= 'z')) ||
            ((s[i - 1] >= 'A') && (s[i - 1] <= 'Z')) ||
            ((s[i - 1] >= '0') && (s[i - 1] <= '9')))
          continue;
      }
      if (i + name.length() < len) {
        if (((s[i + name.length()] >= 'a') && (s[i + name.length()] <= 'z')) ||
            ((s[i + name.length()] >= 'A') && (s[i + name.length()] <= 'Z')) ||
            ((s[i + name.length()] >= '0') && (s[i + name.length()] <= '9')))
          continue;
      }
      return true;
    }
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  string s, currUser, t;
  int pos, curr;
  bool flag;
  vector<string> msg;
  int T, n, m;
  cin >> T;
  while (T--) {
    for (int i = 0; i < MAXN; i++) allowed[i].clear();
    msg.clear();
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> users[i];
    cin >> m;
    getline(cin, s);
    for (int i = 0; i < m; i++) {
      getline(cin, s);
      currUser = "";
      for (int j = 0; j < s.length(); j++) {
        if (s[j] != ':')
          currUser += s[j];
        else {
          pos = j;
          break;
        }
      }
      t = "";
      for (int j = pos + 1; j < s.length(); j++) t += s[j];
      msg.push_back(t);
      if (currUser != "?") {
        for (int j = 1; j <= n; j++) {
          if (currUser == users[j]) allowed[i].push_back(j);
        }
        continue;
      } else {
        for (int j = 1; j <= n; j++) {
          if (!contains(msg[i], users[j])) allowed[i].push_back(j);
        }
      }
    }
    memset(dp, false, sizeof(dp));
    for (int i = 0; i < allowed[0].size(); i++) dp[0][allowed[0][i]] = true;
    for (int i = 1; i < m; i++) {
      for (int j = 0; j < allowed[i].size(); j++) {
        for (int jj = 0; jj < MAXN; jj++) {
          if ((jj != allowed[i][j]) && (dp[i - 1][jj])) {
            dp[i][allowed[i][j]] = true;
          }
        }
      }
    }
    flag = false;
    for (int i = 1; i <= n; i++) {
      flag |= dp[m - 1][i];
      if (dp[m - 1][i]) curr = i;
    }
    if (!flag) {
      cout << "Impossible" << endl;
      continue;
    } else {
      for (int i = m - 1; i >= 0; i--) {
        msg[i] = users[curr] + ":" + msg[i];
        if (i > 0) {
          for (int j = 1; j <= n; j++) {
            if ((dp[i - 1][j]) && (j != curr)) {
              curr = j;
              break;
            }
          }
        }
      }
      for (int i = 0; i < m; i++) cout << msg[i] << endl;
    }
  }
  return 0;
}
