#include <bits/stdc++.h>
using namespace std;
const int N = 110;
string _name[N], text[N];
map<string, int> mp;
int id(string str) {
  if (mp.find(str) == mp.end())
    return -1;
  else
    return mp[str];
}
char temp[N];
int vis[N][N], put[N], n, m;
bool check(char ch) {
  return ch == '.' || ch == ' ' || ch == ',' || ch == '?' || ch == '!';
}
bool sure(int& t, int i) {
  int cnt = 0, _id = -1;
  for (int j = 0; j < n; j++)
    if (vis[i][j])
      cnt++;
    else
      _id = j;
  if (cnt == n) return false;
  if (cnt == n - 1) t = _id;
  return true;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    cin >> n;
    mp.clear();
    memset(put, -1, sizeof put);
    memset(vis, 0, sizeof vis);
    for (int i = 0; i < n; i++) {
      cin >> _name[i];
      mp[_name[i]] = i;
    }
    cin >> m;
    getchar();
    int ok = 1;
    for (int i = 0; i < m; i++) getline(cin, text[i]);
    for (int i = 0; i < m; i++) {
      int c;
      for (c = 0; c < (int)text[i].length() && text[i][c] != ':'; c++)
        temp[c] = text[i][c];
      put[i] = id(string(temp, temp + c));
      int cur = 0;
      for (c++; c < (int)text[i].length(); c++) {
        if (check(text[i][c])) {
          int _id = id(string(temp, temp + cur));
          if (_id != -1) vis[i][_id] = 1;
          cur = 0;
        } else
          temp[cur++] = text[i][c];
      }
      int _id = id(string(temp, temp + cur));
      if (_id != -1) vis[i][_id] = 1;
      if (put[i] != -1 && vis[i][put[i]]) ok = 0;
    }
    queue<int> q;
    for (int i = 0; i < m && ok; i++) {
      if (put[i] != -1)
        q.push(i);
      else {
        int _id = -1;
        if (!sure(_id, i)) ok = 0;
        if (_id != -1) {
          q.push(_id);
          put[i] = _id;
        }
      }
    }
    while (!q.empty() && ok) {
      int u = q.front();
      q.pop();
      if (u > 0) {
        vis[u - 1][put[u]] = 1;
        int _id = -1;
        if (!sure(_id, u - 1)) ok = 0;
        if (_id != -1 && put[u - 1] == -1) {
          put[u - 1] = _id;
          q.push(u - 1);
        }
      }
      if (u + 1 < m) {
        vis[u + 1][put[u]] = 1;
        int _id = -1;
        if (!sure(_id, u + 1)) ok = 0;
        if (_id != -1 && put[u + 1] == -1) {
          put[u + 1] = _id;
          q.push(u + 1);
        }
      }
    }
    if (!ok) {
      puts("Impossible");
      continue;
    }
    for (int i = 0; i < m; i++) {
      if (put[i] == -1) {
        for (int j = 0; j < n; j++)
          if (!vis[i][j]) {
            if (i > 0 && put[i - 1] == j) continue;
            if (i + 1 < m && put[i + 1] == j) continue;
            put[i] = j;
            break;
          }
      }
      if (put[i] == -1)
        while (true)
          ;
      int _id = put[i], cur = 0;
      for (int j = 0; j < (int)_name[_id].length(); j++)
        temp[cur++] = _name[_id][j];
      int c = 0;
      while (text[i][c] != ':') c++;
      while (c < (int)text[i].length()) temp[cur++] = text[i][c++];
      temp[cur] = 0;
      cout << temp << endl;
    }
  }
  return 0;
}
