#include <bits/stdc++.h>
using namespace std;
int dcmp(double a, double b) {
  return fabs(a - b) <= 0.0000000001 ? 0 : (a > b) ? 1 : -1;
}
int mem[1000 + 9][1000 + 9], m, n;
vector<vector<int> > res;
map<string, int> mb;
vector<string> v, us, tx;
int fun(int last, int id) {
  if (id == m) {
    return 1;
  }
  int &ret = mem[last][id];
  if (ret != -1) return ret;
  ret = 0;
  for (int i = 0; i < ((int)(res[id]).size()); i++) {
    if (res[id][i] != last) {
      if (fun(res[id][i], id + 1) == 1) {
        us[id] = v[res[id][i]];
        ret = 1;
        return 1;
      }
    }
  }
  return 0;
}
bool check(int i, int j) {
  if (((int)(v[j]).size()) > ((int)(tx[i]).size())) return 0;
  for (int l = 0; l <= ((int)(tx[i]).size()) - ((int)(v[j]).size()); l++) {
    for (int k = 0; k < ((int)(v[j]).size()); k++) {
      if (v[j][k] != tx[i][l + k]) break;
      if (k == ((int)(v[j]).size()) - 1) {
        if (l + k != ((int)(tx[i]).size()) - 1 &&
                (tx[i][l + k + 1] >= 'a' && tx[i][l + k + 1] <= 'z') ||
            (tx[i][l + k + 1] >= 'A' && tx[i][l + k + 1] <= 'Z') ||
            (tx[i][l + k + 1] >= '0' && tx[i][l + k + 1] <= '9'))
          break;
        if (l - 1 >= 0 && (tx[i][l - 1] >= 'a' && tx[i][l - 1] <= 'z') ||
            (tx[i][l - 1] >= 'A' && tx[i][l - 1] <= 'Z') ||
            (tx[i][l - 1] >= '0' && tx[i][l - 1] <= '9'))
          break;
        return 1;
      }
    }
  }
  return 0;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    string st;
    memset(mem, -1, sizeof(mem));
    us.clear();
    v.clear();
    mb.clear();
    tx.clear();
    for (int i = 0; i < n; i++) {
      cin >> st, v.push_back(st);
      mb[st] = i;
    }
    string enter;
    cin >> m;
    getline(cin, enter);
    for (int i = 0; i < m; i++) {
      string text = "", user = "", line;
      getline(cin, line);
      int l;
      for (l = 0; l < ((int)(line).size()); l++) {
        if (line[l] == ':') break;
        user += line[l];
      }
      for (int j = l + 1; j < ((int)(line).size()); j++) {
        text += line[j];
      }
      tx.push_back(text);
      us.push_back(user);
    }
    int fl = 0;
    res.clear();
    res.resize(100 + 9);
    string s = "";
    for (int i = 0; i < m; i++) {
      if (us[i] != "?") {
        res[i].push_back(mb[us[i]]);
        continue;
      }
      for (int j = 0; j < n; j++) {
        if (!check(i, j)) res[i].push_back(j);
      }
    }
    int ans = fun(n + 1, 0);
    if (ans != 1)
      cout << "Impossible\n";
    else {
      for (int i = 0; i < m; i++) {
        cout << us[i] << ":" << tx[i] << endl;
      }
    }
  }
}
