#include <bits/stdc++.h>
using namespace std;
void solution() {
  string S[101];
  int used[101] = {0};
  int n;
  cin >> n;
  map<string, int> mp;
  for (int i = 1; i <= n; i++) {
    cin >> S[i];
    mp[S[i]] = i;
  }
  int m;
  cin >> m;
  int dp[101][101][101] = {0};
  for (int i = 0; i <= 100; i++)
    for (int j = 0; j <= 100; j++)
      if (i != j) dp[0][i][j] = 1;
  vector<string> ans;
  getchar();
  string s;
  for (int i = 1; i <= m; i++) {
    vector<int> authors;
    getline(cin, s);
    if (s[0] == '?') {
      for (auto it : mp) {
        bool f = 0;
        for (int j = 0; j < s.size(); j++) {
          int ch = s.find(it.first, j);
          if (!(ch == string::npos ||
                (ch != string::npos &&
                 (isalnum(s[ch - 1]) || isalnum(s[ch + it.first.size()])))))
            f = 1;
        }
        if (!f) authors.push_back(it.second);
      }
      for (int ii = 1; ii <= n; ii++)
        for (int jj = 1; jj <= n; jj++)
          if (dp[i - 1][ii][jj])
            for (int j = 0; j < authors.size(); j++)
              if (jj != authors[j]) dp[i][jj][authors[j]] = 1;
    } else {
      int x = mp[(s.substr(0, s.find(':')))];
      for (int ii = 1; ii <= n; ii++)
        for (int jj = 1; jj <= n; jj++)
          if (dp[i - 1][ii][jj] && jj != x) dp[i][jj][x] = 1;
    }
    ans.push_back(s.substr(s.find(':')));
  }
  vector<int> anss;
  int x = -1, y, f = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (f && dp[m][i][j]) {
        anss.push_back(j);
        x = i;
        f = 0;
        break;
      }
    }
  }
  if (m == 1) {
    vector<int> vc;
    if (s[0] == '?') {
      for (auto it : mp) {
        bool f = 0;
        for (int j = 0; j < s.size(); j++) {
          int ch = s.find(it.first, j);
          if (!(ch == string::npos ||
                (ch != string::npos &&
                 (isalnum(s[ch - 1]) || isalnum(s[ch + it.first.size()])))))
            f = 1;
        }
        if (!f) vc.push_back(it.second);
      }
    } else {
      vc.push_back(mp[(s.substr(0, s.find(':')))]);
    }
    if (vc.empty()) {
      cout << "Impossible\n";
    } else {
      cout << S[vc[0]] << ans[0] << endl;
    }
    return;
  }
  if (x == -1) {
    cout << "Impossible\n";
    return;
  }
  int mm = m;
  while (--m >= 1) {
    for (int i = 1; i <= n; i++) {
      if (dp[m][i][x] && i != x) {
        anss.push_back(x);
        x = i;
        break;
      }
    }
  }
  reverse((anss).begin(), (anss).end());
  for (int i = 0; i < mm; i++) cout << S[anss[i]] << ans[i] << endl;
}
int main() {
  int q;
  cin >> q;
  while (q--) solution();
}
