#include <bits/stdc++.h>
using namespace std;
const bool DEBUG = false;
const int IINF = INT_MAX;
const string FAIL = "Impossible";
bool ok[200][200];
vector<int> G[200];
void get_terms(string s, vector<string> &terms) {
  if (s[0] == '?') {
    terms.push_back("?");
    s[0] = s[1] = ' ';
  }
  for (int i = 0; i < (int)s.size(); i++) {
    char c = s[i];
    if (!(('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ||
          ('0' <= c && c <= '9'))) {
      s[i] = ' ';
    }
  }
  stringstream ss;
  ss << s;
  while (ss >> s) terms.push_back(s);
}
set<pair<int, int>> S;
bool dfs(int cur, int depth, deque<int> &path, int n, int m) {
  if (depth >= m) return true;
  if (S.count(pair<int, int>(cur, depth))) return false;
  S.insert(pair<int, int>(cur, depth));
  for (int i = 0; i < (int)G[depth].size(); i++) {
    int next = G[depth][i];
    if (next == cur) continue;
    path.push_back(next);
    if (dfs(next, depth + 1, path, n, m)) return true;
    path.pop_back();
  }
  return false;
}
void compute(vector<string> &users, vector<string> &texts) {
  memset(ok, false, sizeof ok);
  unordered_map<string, int> toIndex;
  S.clear();
  int n = (int)users.size();
  for (int i = 0; i < n; i++) toIndex[users[i]] = i;
  vector<string> mtexts = texts;
  int m = (int)texts.size();
  vector<vector<string>> vec;
  for (int i = 0; i < m; i++) {
    if (DEBUG) {
      cout << "mtexts[" << i << "] = " << mtexts[i] << endl;
    }
    vector<string> terms;
    get_terms(mtexts[i], terms);
    if (DEBUG) {
      cout << "the term => ";
      for (int j = 0; j < (int)terms.size(); j++) cout << terms[j] << " ";
      puts("");
    }
    vec.push_back(terms);
  }
  for (int i = 0; i < m; i++) {
    if (vec[i][0] != "?") {
      ok[i][toIndex[vec[i][0]]] = true;
    } else {
      memset(ok[i], true, sizeof ok[i]);
      for (int j = 0; j < (int)vec[i].size(); j++) {
        if (toIndex.count(vec[i][j])) {
          ok[i][toIndex[vec[i][j]]] = false;
        }
      }
    }
  }
  for (int i = 0; i < m; i++) {
    G[i].clear();
    for (int j = 0; j < n; j++) {
      if (ok[i][j]) G[i].push_back(j);
    }
    if (G[i].empty()) {
      cout << FAIL << endl;
      return;
    }
  }
  if (DEBUG) {
    puts("--- BIT MAP ---");
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        cout << ok[i][j] << " ";
      }
      puts("");
    }
    puts("");
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < (int)G[i].size(); j++) {
        cout << G[i][j] << " ";
      }
      puts("");
    }
  }
  for (int i = 0; i < n; i++) {
    if (ok[0][i]) {
      deque<int> path;
      path.push_back(i);
      S.clear();
      if (dfs(i, 1, path, n, m)) {
        assert((int)path.size() == m);
        for (int j = 0; j < m; j++) {
          string ans = users[path[j]];
          int k;
          for (k = 0; k < (int)texts[j].size(); ++k) {
            if (texts[j][k] == ':') break;
          }
          for (int l = k; l < (int)texts[j].size(); l++) {
            ans += texts[j][l];
          }
          cout << ans << endl;
        }
        return;
      }
    }
  }
  cout << FAIL << endl;
}
char in[200];
int main() {
  if (0) {
    vector<string> u;
    for (int i = 0; i < 26; i++) u.push_back(string(9, 'a' + i));
    for (int i = 0; i < 26; i++) u.push_back(string(9, 'A' + i));
    for (int i = 0; i < 26; i++) u.push_back(string(10, 'a' + i));
    for (int i = 0; i < 22; i++) u.push_back(string(10, 'A' + i));
    cout << 10 << endl;
    for (int _ = 0; _ < 10; _++) {
      cout << 100 << endl;
      for (int i = 0; i < 100; i++) cout << u[i] << " ";
      puts("");
      cout << 100 << endl;
      for (int i = 0; i < 100; i++) {
        cout << "?: ";
        for (int j = 0; j < 97; j++) {
          if (j & 1)
            cout << ' ';
          else
            cout << (char)('a' + j % 26);
        }
        puts("");
      }
    }
    return 0;
  }
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    vector<string> users(n);
    for (int i = 0; i < n; i++) {
      scanf(" %s", &in[0]);
      users[i] = string(in);
    }
    int m;
    cin >> m;
    vector<string> texts(m);
    cin.ignore();
    for (int i = 0; i < m; i++) getline(cin, texts[i]);
    compute(users, texts);
  }
  return 0;
}
