#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T qmin(const T a, const T b) {
  return a < b ? a : b;
}
template <typename T>
inline T qmax(const T a, const T b) {
  return a > b ? a : b;
}
template <typename T>
inline void getmin(T &a, const T b) {
  if (a > b) a = b;
}
template <typename T>
inline void getmax(T &a, const T b) {
  if (a < b) a = b;
}
inline void fileio(string s) {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}
const int inf = 1e9 + 7;
const long long linf = 1e17 + 7;
const int N = 2e2 + 7;
map<string, int> id;
string name[N], s, tmp;
bool forb[N][N];
bool f[N][N];
int pre[N][N];
string dia[N];
bool flag;
int kase, n, m, a[N];
inline void trace(int i, int j) {
  if (!i) return;
  a[i] = j;
  trace(i - 1, pre[i][j]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> kase;
  while (kase--) {
    memset(forb, 0, sizeof(forb));
    id.clear();
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> tmp;
      id[tmp] = i;
      name[i] = tmp;
    }
    cin >> m;
    cin.ignore();
    for (int i = 1; i <= m; ++i) {
      getline(cin, s);
      int pos = 0;
      while (s[pos] != ':') ++pos;
      dia[i] = s.substr(pos + 1, s.size() - pos - 1);
      for (int i = pos; i < s.size(); ++i) {
        if (!isalpha(s[i]) && !isdigit(s[i])) s[i] = ' ';
      }
      istringstream ss(s);
      ss >> tmp;
      if (a[i] = id[tmp]) continue;
      while (ss >> tmp) {
        if (id.find(tmp) != id.end()) forb[i][id[tmp]] = 1;
      }
    }
    flag = true;
    memset(f, 0, sizeof(f));
    for (int j = 1; j <= n; ++j) f[0][j] = 1;
    for (int i = 1; i <= m && flag; ++i) {
      flag = false;
      if (a[i]) {
        f[i][a[i]] = true;
        if (i == 1) {
          flag = true;
          continue;
        }
        for (int j = 1; j <= n; ++j)
          if (j != a[i] && f[i - 1][j]) {
            pre[i][a[i]] = j;
            flag = true;
            break;
          }
        continue;
      }
      for (int j = 1; j <= n; ++j) {
        if (forb[i][j]) continue;
        for (int k = 1; k <= n; ++k) {
          if (j == k || !f[i - 1][k]) continue;
          f[i][j] = true;
          pre[i][j] = k;
          flag = true;
          break;
        }
      }
    }
    if (!flag) {
      cout << "Impossible" << endl;
      continue;
    }
    for (int i = 1; i <= n; ++i)
      if (f[m][i]) {
        trace(m, i);
        break;
      }
    for (int i = 1; i <= m; ++i) {
      cout << name[a[i]] << ':' << dia[i] << endl;
    }
  }
  return 0;
}
