#include <bits/stdc++.h>
using namespace std;
int t, i, j, k, cur, dp[111][111], who[111], n, m, gg, mm;
char s[111][111], scur[111];
string chat[111];
bool pd[111][111];
int cmp() {
  for (int i = 1; i <= n; i++)
    if (cur == (int)strlen(s[i])) {
      bool pd = true;
      for (int j = 0; j < cur; j++)
        if (s[i][j] != scur[j]) {
          pd = false;
          break;
        }
      if (pd) return i;
    }
  return -1;
}
int main() {
  cin >> t;
  for (int tt = 1; tt <= t; tt++) {
    cin >> n;
    for (i = 1; i <= n; i++) {
      cin >> s[i];
    }
    memset(pd, true, sizeof(pd));
    cin >> m;
    mm += m;
    char c;
    cin >> c;
    for (i = 1; i <= m; i++) {
      getline(cin, chat[i]);
      if ((c <= '9' && c >= '0') || (c >= 'a' && c <= 'z') ||
          (c >= 'A' && c <= 'Z') || c == '?')
        if (i == 1) chat[i] = c + chat[i];
      cur = 0;
      j = 0;
      while (!((chat[i][j] <= '9' && chat[i][j] >= '0') ||
               (chat[i][j] >= 'a' && chat[i][j] <= 'z') ||
               (chat[i][j] >= 'A' && chat[i][j] <= 'Z') || chat[i][j] == '?')) {
        j++;
      }
      while ((chat[i][j] <= '9' && chat[i][j] >= '0') ||
             (chat[i][j] >= 'a' && chat[i][j] <= 'z') ||
             (chat[i][j] >= 'A' && chat[i][j] <= 'Z')) {
        scur[cur++] = chat[i][j];
        j++;
      }
      who[i] = cmp();
      while (chat[i][j] != ':') j++;
      cur = 0;
      while (j < (int)chat[i].length()) {
        if ((chat[i][j] <= '9' && chat[i][j] >= '0') ||
            (chat[i][j] >= 'a' && chat[i][j] <= 'z') ||
            (chat[i][j] >= 'A' && chat[i][j] <= 'Z')) {
          scur[cur++] = chat[i][j];
        } else {
          if (cur) {
            int w = cmp();
            if (w >= 0) pd[i][w] = false;
            cur = 0;
          }
        }
        j++;
      }
      if (cur) {
        int w = cmp();
        if (w >= 0) pd[i][w] = false;
        cur = 0;
      }
    }
    memset(dp, 0, sizeof(dp));
    dp[0][1] = 1;
    for (i = 1; i <= m; i++) {
      for (j = 1; j <= n; j++)
        if (dp[i - 1][j] && (j != who[i] || i == 1)) {
          if (who[i] >= 0) {
            if (!dp[i][who[i]]) dp[i][who[i]] = j;
          } else {
            for (k = 1; k <= n; k++)
              if ((!dp[i][k]) && (j != k || i == 1) && pd[i][k]) {
                dp[i][k] = j;
              }
          }
        }
    }
    bool pdd = false;
    for (i = 1; i <= n; i++)
      if (dp[m][i]) {
        int curwho = i;
        for (j = m; j >= 1; j--) {
          who[j] = curwho;
          curwho = dp[j][curwho];
        }
        pdd = true;
        break;
      }
    if (!pdd) {
      cout << "Impossible" << endl;
      gg += m;
    } else {
      for (i = 1; i <= m; i++) {
        bool pdm = false;
        cout << s[who[i]];
        for (j = 0; j < (int)(chat[i].length()); j++) {
          if (chat[i][j] == ':') pdm = true;
          if (pdm) cout << chat[i][j];
        }
        cout << endl;
        gg++;
      }
    }
  }
}
