#include <bits/stdc++.h>
using namespace std;
int t, n, k, i, ii, z[205][2];
char x[205][205], y[205][205];
map<string, int> m;
map<string, int>::iterator it;
string str;
set<int> s[205];
bool o;
bool f1(char a) {
  if (a >= 'A' && a <= 'Z') return 1;
  if (a >= 'a' && a <= 'z') return 1;
  if (a >= '0' && a <= '9') return 1;
  return 0;
}
int main() {
  scanf("%d", &t);
  while (t--) {
    m.clear();
    memset(x, 0, sizeof(x));
    memset(y, 0, sizeof(y));
    memset(z, 0, sizeof(z));
    o = 1;
    scanf("%d", &n);
    for (int a = (1); a <= (n); a++) {
      scanf("%s", x[a]);
      m[string(x[a])] = a;
    }
    scanf("%d\n", &k);
    for (int a = (0); a < (k); a++) {
      s[a].clear();
      gets(y[a]);
      i = 0;
      while (y[a][i] != ':') i++;
      if (y[a][0] == '?') {
        str = "";
        i++;
        ii = i;
        while (y[a][i]) {
          if (f1(y[a][i]))
            str += y[a][i];
          else {
            it = m.find(str);
            if (it != m.end()) s[a].insert(it->second);
            str = "";
          }
          i++;
        }
        it = m.find(str);
        if (it != m.end()) s[a].insert(it->second);
        i = 0;
        for (int b = (1); b <= (n); b++)
          if ((!a || z[a - 1][0] != b || (z[a - 1][1] != b && z[a - 1][1])) &&
              !s[a].count(b)) {
            z[a][i++] = b;
            if (i == 2) break;
          }
        if (i == 0) o = 0;
      } else {
        z[a][0] = m[string(y[a], i)];
        if (a && z[a][0] == z[a - 1][0] && !z[a - 1][1]) o = 0;
      }
    }
    if (o) {
      for (int a = (k - 2); a >= (0); a--)
        if (z[a][0] == z[a + 1][0]) swap(z[a][0], z[a][1]);
      for (int a = (0); a < (k); a++)
        if (y[a][0] == '?')
          printf("%s:%s\n", x[z[a][0]], y[a] + 2);
        else
          printf("%s\n", y[a]);
    } else
      printf("Impossible\n");
  }
  return 0;
}
