#include <bits/stdc++.h>
using namespace std;
long long readi() {
  long long input = 0;
  char c = ' ';
  while (c < '-') {
    c = getchar();
  }
  bool negative = false;
  if (c == '-') {
    negative = true;
    c = getchar();
  }
  while (c >= '0') {
    input = 10 * input + (c - '0');
    c = getchar();
  }
  if (negative) {
    input = -input;
  }
  return input;
}
string reads() {
  string input = "";
  char c = ' ';
  while (c <= ' ') {
    c = getchar();
  }
  while (c > ' ') {
    input += c;
    c = getchar();
  }
  return input;
}
void printi(long long output) {
  if (output == 0) {
    putchar('0');
    return;
  }
  if (output < 0) {
    putchar('-');
    output = -output;
  }
  vector<char> vout;
  while (output) {
    vout.push_back((output % 10) + '0');
    output /= 10;
  }
  for (int i = vout.size() - 1; i >= 0; i--) {
    putchar(vout[i]);
  }
  return;
}
void prints(string output) {
  for (int i = 0; i < output.length(); i++) {
    putchar(output[i]);
  }
  return;
}
int T;
map<string, int> mp;
int N, Q;
string message[110];
string cmd;
int id[110];
string names[110];
bool mentioned[110][110];
int parent[110][110];
vector<string> v;
int person;
int32_t main() {
  cout << fixed << setprecision(10);
  ios_base::sync_with_stdio(false);
  if (fopen("cf754c.in", "r")) {
    freopen("cf754c.in", "r", stdin);
    freopen("cf754c.out", "w", stdout);
  }
  cin >> T;
  while (T--) {
    mp.clear();
    cin >> N;
    for (int i = 1; i <= N; i++) {
      cin >> names[i];
      mp[names[i]] = i;
    }
    cin >> Q;
    cin.get();
    for (int i = 0; i < Q; i++) {
      for (int j = 0; j <= N; j++) {
        mentioned[i][j] = false;
      }
    }
    for (int i = 0; i < Q; i++) {
      getline(cin, message[i]);
      string user = "";
      for (int j = 0; j < message[i].length(); j++) {
        if (message[i][j] == ':') {
          break;
        }
        user += message[i][j];
      }
      id[i] = mp[user];
      reverse(message[i].begin(), message[i].end());
      for (int j = 0; j <= user.length(); j++) {
        message[i].pop_back();
      }
      reverse(message[i].begin(), message[i].end());
      message[i] += ' ';
      user = "";
      for (int j = 0; j < message[i].length(); j++) {
        if (!isalpha(message[i][j]) && !isdigit(message[i][j])) {
          mentioned[i][mp[user]] = true;
          user = "";
          continue;
        }
        user += message[i][j];
      }
      mentioned[i][0] = true;
    }
    for (int i = 0; i < Q; i++) {
      for (int j = 0; j <= N; j++) {
        parent[i][j] = -1;
      }
    }
    for (int j = 1; j <= N; j++) {
      if (mentioned[0][j]) {
        continue;
      }
      if (id[0] != 0 && id[0] != j) {
        continue;
      }
      parent[0][j] = 0;
    }
    for (int i = 1; i < Q; i++) {
      for (int j = 1; j <= N; j++) {
        if (mentioned[i][j]) {
          continue;
        }
        if (id[i] != 0 && id[i] != j) {
          continue;
        }
        for (int k = 1; k <= N; k++) {
          if (j == k) {
            continue;
          }
          if (parent[i - 1][k] == -1) {
            continue;
          }
          parent[i][j] = k;
        }
      }
    }
    person = -1;
    for (int j = 1; j <= N; j++) {
      if (parent[Q - 1][j] != -1) {
        person = j;
      }
    }
    if (person == -1) {
      cout << "Impossible\n";
      continue;
    }
    v.clear();
    for (int i = Q - 1; i >= 0; i--) {
      v.push_back(names[person] + ':' + message[i]);
      v.back().pop_back();
      person = parent[i][person];
    }
    reverse(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++) {
      cout << v[i] << '\n';
    }
  }
  return 0;
}
