#include <bits/stdc++.h>
using namespace std;
const int MAXI = 100000;
const int MAX = 200;
int n;
string chatter[MAX], message[MAX];
string enter;
int numberOfChatters, numberOfMessages;
int numberOfPossibilities[MAX];
bool notDone;
int mini;
bool result;
int found;
bool zeroFound;
string getName(int k) {
  if (message[k] != "") {
    string result = "";
    int i = 0;
    while (message[k][i] != ':') {
      result += message[k][i];
      ++i;
    }
    return result;
  }
  return "?";
}
bool check(int o, int i, int maxii, int msgNumber) {
  int chatterNameLength = chatter[i].length();
  if (maxii < chatterNameLength + o) {
    return false;
  }
  for (int u = o; u < chatterNameLength + o; ++u) {
    if (message[msgNumber][u] != chatter[i][u - o]) {
      return false;
    }
  }
  if (o > 0) {
    char beginingLetter = message[msgNumber][o - 1];
    if (beginingLetter != ' ' && beginingLetter != '.' &&
        beginingLetter != '!' && beginingLetter != '?' &&
        beginingLetter != ',' && beginingLetter != ':') {
      return false;
    }
  }
  int messageLength = message[msgNumber].length();
  if (o + chatterNameLength < messageLength) {
    char endingLetter = message[msgNumber][o + chatterNameLength];
    if (endingLetter != ' ' && endingLetter != '.' && endingLetter != '!' &&
        endingLetter != '?' && endingLetter != ',' && endingLetter != ':') {
      return false;
    }
  }
  return true;
}
bool find(int k, int i) {
  int lengthOfMessage = message[k].length();
  for (int o = 0; o < lengthOfMessage; ++o) {
    if (check(o, i, lengthOfMessage, k)) {
      return true;
    }
  }
  return false;
}
void findNumberOfPossibilities(int k) {
  if (getName(k) != "?") {
    numberOfPossibilities[k] = -1;
    return;
  }
  int result = 0;
  string namePrev = "", nameNext = "";
  namePrev = getName(k - 1);
  nameNext = getName(k + 1);
  for (int i = 2; i <= numberOfChatters + 1; ++i) {
    if (chatter[i] != namePrev && chatter[i] != nameNext && !find(k, i))
      result++;
  }
  numberOfPossibilities[k] = result;
}
void putChatterName(int k, int i) {
  string result = chatter[i];
  string oldName = getName(k);
  int placeToStartOverWritting = getName(k).length();
  int lengthOfMessage = message[k].length();
  for (int o = placeToStartOverWritting; o < lengthOfMessage; ++o) {
    result += message[k][o];
  }
  message[k] = result;
}
void repair(int k) {
  string namePrev = "", nameNext = "";
  namePrev = getName(k - 1);
  nameNext = getName(k + 1);
  for (int i = 2; i <= numberOfChatters + 1; ++i) {
    if (chatter[i] != namePrev && chatter[i] != nameNext && !find(k, i)) {
      putChatterName(k, i);
      numberOfPossibilities[k] = -1;
      findNumberOfPossibilities(k - 1);
      findNumberOfPossibilities(k + 1);
    }
  }
}
void printAll() {
  for (int i = 2; i <= numberOfMessages + 1; ++i) {
    cout << message[i] << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 2; i <= n + 1; ++i) {
    result = true;
    cin >> numberOfChatters;
    for (int o = 2; o <= numberOfChatters + 1; ++o) {
      cin >> chatter[o];
    }
    cin >> numberOfMessages;
    getline(cin, enter);
    for (int o = 2; o <= numberOfMessages + 1; ++o) {
      getline(cin, message[o]);
      numberOfPossibilities[o] = -1;
    }
    for (int o = 2; o <= numberOfMessages + 1; ++o) {
      if (message[o][0] == '?') {
        findNumberOfPossibilities(o);
      }
    }
    notDone = true;
    while (notDone) {
      mini = MAXI;
      found = -1;
      zeroFound = false;
      for (int o = 2; o <= numberOfMessages + 1; ++o) {
        if (numberOfPossibilities[o] != -1) {
          if (numberOfPossibilities[o] == 0) {
            zeroFound = true;
            break;
          } else if (mini > numberOfPossibilities[o]) {
            mini = numberOfPossibilities[o];
            found = o;
          }
        }
      }
      if (zeroFound) break;
      if (found == -1) {
        notDone = false;
        break;
      }
      repair(found);
    }
    if (notDone) {
      cout << "Impossible" << endl;
    } else {
      printAll();
    }
  }
  return 0;
}
