#include <bits/stdc++.h>
using namespace std;
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
void run() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int n, m;
vector<vector<int>> mem;
vector<set<int>> p;
vector<pair<int, string>> input;
vector<string> v;
int solve(int i, int j) {
  int& rt = mem[i][j];
  if (~rt) return rt;
  if (p[i].find(j) == p[i].end()) return rt = 0;
  if (i + 1 == m) return rt = 1;
  rt = 0;
  for (int k = 1; k <= n; k++) {
    if (j == k) continue;
    rt |= solve(i + 1, k);
  }
  return rt;
}
void build(int i, int j) {
  if (p[i].find(j) == p[i].end()) return;
  cout << v[j - 1] << ':' << input[i].second << '\n';
  if (i + 1 == m) return;
  for (int k = 1; k <= n; k++) {
    if (solve(i + 1, k)) {
      if (j == k) continue;
      build(i + 1, k);
      return;
    }
  }
}
int main() {
  run();
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    map<string, int> id;
    v = vector<string>(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i];
      id[v[i]] = i + 1;
    }
    cin >> m;
    input = vector<pair<int, string>>(m);
    p = vector<set<int>>(m);
    cin.ignore();
    for (int i = 0; i < m; i++) {
      string s[2];
      bool b = 0;
      string line;
      getline(cin, line);
      for (char& ch : line) {
        if (ch == ':' && !b)
          b = 1;
        else
          s[b] += ch;
      }
      input[i] = {id[s[0]], s[1]};
      for (char& ch : s[1])
        if (string(".,!?").find(ch) != string::npos) ch = ' ';
      stringstream ss(s[1]);
      if (input[i].first == 0) {
        for (int j = 1; j <= n; j++) p[i].emplace(j);
        while (ss >> line)
          if (id.find(line) != id.end()) p[i].erase(id[line]);
      } else
        p[i].insert(input[i].first);
    }
    mem = vector<vector<int>>(m, vector<int>(n + 1, -1));
    vector<string> out;
    int b = 0;
    for (int j = 1; j <= n; j++) {
      if (solve(0, j)) {
        build(0, j);
        b = 1;
        break;
      }
    }
    if (!b) cout << "Impossible\n";
  }
}
