#include <bits/stdc++.h>
using namespace std;
pair<string, vector<string> > p[105];
string mes[105];
set<string> s;
set<string> can[105];
void solve() {
  s.clear();
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    string ss;
    cin >> ss;
    s.insert(ss);
  }
  int m;
  scanf("%d\n", &m);
  for (int i = 0; i < m; i++) {
    can[i] = s;
    mes[i] = "";
    string name;
    char c;
    while (scanf("%c", &c) && c != ':') {
      name += c;
    }
    string str;
    vector<string> v;
    while (scanf("%c", &c) && c != '\n') {
      mes[i] += c;
      if (c >= 'a' && c <= 'z') {
        str += c;
      } else if (c >= 'A' && c <= 'Z') {
        str += c;
      } else if (c >= '0' && c <= '9') {
        str += c;
      } else {
        v.push_back(str);
        if (can[i].find(str) != can[i].end()) can[i].erase(str);
        str = "";
      }
    }
    v.push_back(str);
    if (can[i].find(str) != can[i].end()) can[i].erase(str);
    p[i] = make_pair(name, v);
  }
  queue<int> q;
  int inq[1005];
  memset(inq, 0, sizeof(inq));
  for (int i = 0; i < m; i++) {
    if (i != 0) {
      if (can[i].find(p[i - 1].first) != can[i].end()) {
        can[i].erase(p[i - 1].first);
      }
    }
    if (i != m - 1) {
      if (can[i].find(p[i + 1].first) != can[i].end()) {
        can[i].erase(p[i + 1].first);
      }
    }
    if (can[i].size() <= 1 && p[i].first == "?") {
      q.push(i);
      inq[i] = 1;
    }
  }
  while (!q.empty()) {
    int first = q.front();
    q.pop();
    if (can[first].size() == 0) {
      printf("Impossible\n");
      return;
    }
    p[first].first = *can[first].begin();
    if (first != 0) {
      if (can[first - 1].find(p[first].first) != can[first - 1].end())
        can[first - 1].erase(p[first].first);
      if (can[first - 1].size() == 1 && p[first - 1].first == "?" &&
          !inq[first - 1])
        q.push(first - 1), inq[first - 1] = 1;
    }
    if (first != m - 1) {
      if (can[first + 1].find(p[first].first) != can[first + 1].end())
        can[first + 1].erase(p[first].first);
      if (can[first + 1].size() == 1 && p[first + 1].first == "?" &&
          !inq[first + 1])
        q.push(first + 1), inq[first + 1] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    if (p[i].first == "?") {
      p[i].first = *can[i].begin();
      if (i != m - 1) {
        if (can[i + 1].find(p[i].first) != can[i + 1].end())
          can[i + 1].erase(p[i].first);
      }
    }
    printf("%s:%s\n", p[i].first.c_str(), mes[i].c_str());
  }
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    solve();
  }
}
