#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e3 + 1;
int n, m;
string user[MAX];
int cntcan[MAX];
bool canbe[MAX][MAX];
bool ans;
bool garbage(char c) {
  return c == '.' || c == ',' || c == '!' || c == '?' || c == ' ';
}
int main() {
  ios::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) {
    char in[MAX][MAX];
    ans = true;
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> user[i];
    cin >> m;
    cin.getline(in[MAX - 1], MAX, '\n');
    for (int i = 0; i < m; ++i) {
      int now = 0;
      cin.getline(in[i], MAX, '\n');
      if (in[i][0] == '?') {
        now = 2;
        for (int j = 0; j < n; ++j) canbe[i][j] = true;
        cntcan[i] = n;
        int len = strlen(in[i]);
        while (now < len) {
          string tmp;
          while (garbage(in[i][now]) && now < len) ++now;
          while (!garbage(in[i][now]) && now < len) tmp += in[i][now++];
          for (int j = 0; j < n; ++j)
            if (user[j] == tmp && canbe[i][j]) canbe[i][j] = false, --cntcan[i];
        }
      } else {
        for (int j = 0; j < n; ++j) canbe[i][j] = false;
        string tmp;
        while (in[i][now] != ':') tmp += in[i][now++];
        for (int j = 0; j < n; ++j)
          if (user[j] == tmp) canbe[i][j] = true;
        cntcan[i] = 1;
      }
    }
    for (int k = 0; k < m; ++k) {
      for (int i = 0; i < m; ++i) {
        if (i > 0) {
          if (cntcan[i - 1] == 1) {
            for (int j = 0; j < n; ++j)
              if (canbe[i - 1][j] && canbe[i][j])
                canbe[i][j] = false, --cntcan[i];
          }
        }
        if (i < m - 1) {
          if (cntcan[i + 1] == 1) {
            for (int j = 0; j < n; ++j)
              if (canbe[i + 1][j] && canbe[i][j])
                canbe[i][j] = false, --cntcan[i];
          }
        }
        if (cntcan[i] == 0) ans = false;
      }
    }
    if (!ans)
      cout << "Impossible\n";
    else {
      for (int i = 0; i < m; ++i) {
        if (i > 0) {
          for (int j = 0; j < n; ++j)
            if (canbe[i - 1][j] && canbe[i][j])
              canbe[i][j] = false, --cntcan[i];
        }
        int sender;
        for (int j = 0; j < n; ++j)
          if (canbe[i][j]) {
            if (cntcan[i] > 1) {
              canbe[i][j] = false;
              --cntcan[i];
            } else
              sender = j;
          }
        if (in[i][0] == '?') {
          int len = strlen(in[i]);
          cout << user[sender];
          for (int j = 1; j < len; ++j) cout << in[i][j];
        } else {
          int len = strlen(in[i]);
          for (int j = 0; j < len; ++j) cout << in[i][j];
        }
        cout << endl;
      }
    }
  }
  return 0;
}
