#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)1e3 + 10;
const int inf = (int)2e9;
const int mod = (int)1e9 + 7;
int t, m, n;
string a[maxn];
string b[maxn], ans[maxn];
vector<string> name[maxn];
int from[maxn][maxn];
int dp[maxn][maxn];
string sum;
int ch, chh, ts;
char c;
map<string, int> used, was;
int main() {
  scanf("%d", &t);
  while (t--) {
    for (int i = 1; i <= m; i++) {
      for (int j = 0; j < (int)(name[i].size()); j++) {
        dp[i][j] = 0;
        from[i][j] = -1;
      }
    }
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    scanf("%d\n", &m);
    for (int i = 1; i <= m; i++) {
      getline(cin, b[i]);
    }
    for (int i = 1; i <= m; i++) {
      name[i].clear();
    }
    for (int i = 1; i <= m; i++) {
      ch = 0;
      if (b[i][0] == '?') {
        ts = 2;
        ch = 1;
      } else {
        ts = 0;
      }
      chh = 0;
      b[i] += '.';
      sum.clear();
      for (int j = ts; j < (int)(b[i].size()); j++) {
        c = b[i][j];
        if (c == ':') {
          name[i].push_back(sum);
          chh = 1;
        }
        if (!chh && !ch) {
          sum += c;
        } else {
          if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
              (c >= '0' && c <= '9')) {
            sum += c;
          } else {
            if (sum[0] < '0' || sum[0] > '9') was[sum] = 1;
            sum = "";
          }
        }
      }
      if (ch) {
        for (int j = 1; j <= n; j++) {
          if (!was[a[j]]) {
            name[i].push_back(a[j]);
          }
        }
      }
      for (int j = ts; j < (int)(b[i].size()); j++) {
        c = b[i][j];
        if (c == ':') {
          chh = 1;
        }
        if (!chh && !ch) {
        } else {
          if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
              (c >= '0' && c <= '9')) {
            sum += c;
          } else {
            was[sum] = 0;
            sum = "";
          }
        }
      }
    }
    ch = 0;
    for (int j = 0; j < (int)(name[1].size()); j++) {
      dp[1][j] = 1;
    }
    for (int i = 2; i <= m; i++) {
      for (int j = 0; j < (int)(name[i].size()); j++) {
        for (int k = 0; k < (int)(name[i - 1].size()); k++) {
          if (name[i][j] != name[i - 1][k]) {
            if (dp[i - 1][k] == 1) {
              dp[i][j] = 1;
              from[i][j] = k;
            }
          }
        }
      }
    }
    long long v;
    for (int j = 0; j < (int)(name[m].size()); j++) {
      if (dp[m][j] == 1) ch = 1, v = j;
    }
    if (!ch) {
      printf("Impossible\n");
      continue;
    }
    for (int i = m; i >= 1; i--) {
      ans[i] = name[i][v];
      v = from[i][v];
    }
    for (int i = 1; i <= m; i++) {
      cout << ans[i] << ':';
      ch = 0;
      for (int j = 0; j < (int)(b[i].size()) - 1; j++) {
        c = b[i][j];
        if (c == ':') {
          ch = 1;
        } else if (ch) {
          cout << c;
        }
      }
      printf("\n");
    }
  }
  return 0;
}
