#include <bits/stdc++.h>
using namespace std;
char sp[8] = {',', '.', '!', ' ', '$', '?'};
string usr[205];
map<string, int> mp;
char mes[205];
int n;
int m;
int len;
vector<int> ans[205];
string usr_now;
int vis[205];
int check(char x) {
  for (int i = 0; i < 6; i++)
    if (x == sp[i]) {
      return 1;
    }
  return 0;
}
void work(int k, int whe) {
  memset(vis, 0, sizeof(vis));
  for (int i = whe; i < len; i++)
    for (int j = i; j < len; j++) {
      if (check(mes[i]) && check(mes[j])) {
        int l = i + 1;
        int r = j - 1;
        if (l > r) continue;
        usr_now.clear();
        for (int k = l; k <= r; k++) usr_now += mes[k];
        int flag = 1;
        for (int h = 0; h < usr_now.length(); h++)
          if (check(usr_now[h])) {
            flag = 0;
            break;
          }
        if (usr_now[0] >= '0' && usr_now[0] <= '9') flag = 0;
        if (flag == 0) break;
        if (mp.count(usr_now)) {
          vis[mp[usr_now]] = 1;
        }
      }
    }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) ans[k].push_back(i);
}
int a[205];
int tot;
int fff;
string sen[205];
void get_ans() {
  for (int i = 1; i <= tot; i++) {
    cout << usr[a[i]] << ":";
    cout << sen[i] << endl;
  }
  fff = 1;
}
int ffft;
void dfs(int x, int last) {
  if (fff) return;
  if (ffft) return;
  if (x > m) {
    get_ans();
    return;
  }
  if (ans[x].size() == 0) ffft = 1;
  for (int i = 0; i < ans[x].size(); i++) {
    if (ans[x][i] != last) {
      a[++tot] = ans[x][i];
      dfs(x + 1, ans[x][i]);
      tot--;
    }
  }
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    mp.clear();
    cin >> n;
    for (int i = 1; i <= m; i++) ans[i].clear();
    for (int i = 1; i <= m; i++) sen[i].clear();
    for (int i = 1; i <= n; i++) {
      cin >> usr[i];
      mp[usr[i]] = i;
    }
    cin >> m;
    for (int k = 1; k <= m; k++) {
      memset(mes, 0, sizeof(mes));
      if (k == 1) cin.get();
      cin.getline(mes, 200);
      len = strlen(mes);
      mes[len] = '$';
      len++;
      usr_now.clear();
      int whe = 0;
      for (int i = 0; i < len; i++) {
        if (mes[i] != ':') usr_now += mes[i];
        if (mes[i] == ':') {
          whe = i;
          mes[whe] = '$';
          break;
        }
      }
      for (int i = whe + 1; i < len - 1; i++) {
        sen[k] += mes[i];
      }
      if (usr_now[0] != '?') {
        ans[k].push_back(mp[usr_now]);
      } else
        work(k, whe);
    }
    memset(a, 0, sizeof(a));
    fff = 0;
    ffft = 0;
    tot = 0;
    dfs(1, 0);
    if (!fff) {
      cout << "Impossible" << endl;
    }
  }
  return 0;
}
