#include <bits/stdc++.h>
using namespace std;
map<string, int> name;
map<int, set<int> > alo;
set<int> all;
int peo[105];
int main() {
  int t, n, m;
  string str, msg[105], names[105];
  scanf("%d", &t);
  while (t--) {
    bool flag = true;
    alo.clear();
    name.clear();
    all.clear();
    name["?"] = -1;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      cin >> str;
      names[i] = str;
      name[str] = i;
      all.insert(i);
    }
    scanf("%d", &m);
    getchar();
    for (int i = 0, j = 0; i < m; i++, j = 0) {
      string who = "";
      getline(cin, msg[i]);
      while (msg[i][j] != ':') who += msg[i][j++];
      msg[i] = msg[i].substr(j + 1);
      peo[i] = name[who];
    }
    for (int i = 0; i < m; i++) {
      if (peo[i] == -1) {
        alo[i] = all;
        int len = msg[i].size();
        string tmp = "";
        for (int j = 0; j < len; j++) {
          if (!(msg[i][j] <= 'z' && msg[i][j] >= 'a') &&
              !(msg[i][j] <= 'Z' && msg[i][j] >= 'A') &&
              !(msg[i][j] >= '0' && msg[i][j] <= '9')) {
            if (name[tmp]) {
              alo[i].erase(name[tmp]);
            }
            tmp = "";
          } else {
            tmp += msg[i][j];
            if (j == len - 1 && name[tmp]) alo[i].erase(name[tmp]);
          }
        }
        if (i > 0) alo[i].erase(peo[i - 1]);
        if (i < m - 1) alo[i].erase(peo[i + 1]);
      }
    }
    while (1) {
      int cnt = -1;
      map<int, set<int> >::iterator iter = alo.begin(), cnter = alo.begin();
      for (; iter != alo.end(); ++iter) {
        if (iter->second.size() <= cnter->second.size()) {
          cnter = iter;
          cnt = iter->first;
        }
      }
      if (cnt == -1) {
        break;
      }
      if (!cnter->second.size()) {
        puts("Impossible");
        flag = false;
        break;
      }
      int ret = *(alo[cnt].begin());
      if (alo.count(cnt - 1)) alo[cnt - 1].erase(ret);
      if (alo.count(cnt + 1)) alo[cnt + 1].erase(ret);
      peo[cnt] = ret;
      alo.erase(cnter);
    }
    if (flag) {
      for (int i = 0; i < m; i++) {
        cout << names[peo[i]] << ":" << msg[i] << endl;
      }
    }
  }
}
