#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <map>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <utility>
#include <cassert>
#include <bitset>
#define fi first
#define se second
using namespace std;
typedef long long LL;

const int inf = 0x3f3f3f3f;

const int maxn = 1000010*2;
int n,m,T;
char name[1000][1000];
int dp[1000][1000]; //第i条信息可以选择谁？
char str[1000][1000];
int check(char x)
{
	if (x <= 'Z' && x >= 'A') return true;
	if (x <= 'z' && x >= 'a') return true;
	if (x <= '9' && x >= '0') return true;
	return false;
}
int main()
{
	// freopen("in.txt","r",stdin);

	scanf("%d",&T);
	while(T--)
	{
		memset(dp,0,sizeof dp);
		map <string, int> mp;
		scanf("%d",&n);
		for (int i = 1; i <= n; i++) scanf("%s",name[i]), mp[name[i]] = i;
		scanf("%d",&m);
		// cout<<m<<endl;
		char p = getchar();
		// cout<<"---"<<endl;
		for (int i = 1; i <= m; i++)
		{
			gets(str[i]);
			// cout<<str[i]<<endl;
			int len = strlen(str[i]);
			if (str[i][0] != '?')
			{
				memset(dp[i],-1,sizeof dp[i]);
				char tmp[1000];
				for (int j = 0; j < len + 1; j++)
				{
					if (check(str[i][j])) tmp[j] = str[i][j];
					else {
						tmp[j] = '\0';
						break;
					}
				}
				dp[i][mp[tmp]] = 1;
			}
			else
			{
				int cnt = 0;
				char tmp[1000];
				for (int j = 0; j < len + 1; j++)
				{
					if (check(str[i][j])) tmp[cnt++] = str[i][j];
					else
					{
						tmp[cnt] = '\0';
						dp[i][mp[tmp]] = -1;
						cnt = 0;
					}
				}
				// cout<<dp[1][1]<<endl;
			}
		}
		// cout<<dp[1][1]<<endl;
		for (int i = 1; i < m; i ++)
		{
			int cnt = 0;
			for (int j = 1; j <= n; j ++)
			{
				if (dp[i][j] == -1) cnt++;
			}
			for (int j = 1; j <= n; j ++)
			{
				if (dp[i][j] == 1) dp[i+1][j] = -1;
				else if (dp[i][j] == 0 && cnt == n-1) dp[i+1][j] = -1;
			}

		}
		int cnt = 0;
		// cout<<dp[1][1]<<endl;
		for (int i = 1; i <= m; i++)
		{
			cnt = 0;
			for (int j = 1; j <= n; j++)
			{
				// cout<<dp[i][j]<<endl;
				if (dp[i][j] == -1) cnt ++;
			}
			if (cnt == n) break;
		}
		if (cnt == n) {
			printf("Impossible\n");
		}
		else
		{
			dp[m+1][0] = 0;
			for (int i = m; i >= 1; i--)
			{
				dp[i][0] = 0;
				for (int j = 1; j <= n; j++)
				{
					if (dp[i][j] != -1 && dp[i + 1][0] != j) {dp[i][0] = j; break;}
				}
				if (dp[i][0] == 0) assert(0);
			}

			for (int i = 1; i <= m; i++)
			{
				if (str[i][0] == '?')
				{
					assert(dp[i][0] != dp[i+1][0]);
					printf("%s%s\n",name[dp[i][0]], &str[i][1]);
				}
				else
				{
					assert(dp[i][0] != dp[i+1][0]);
					int len = strlen(name[dp[i][0]]);
					printf("%s%s\n",name[dp[i][0]],&str[i][len] );
				}
			}
		}

	}

}


/*************************************************************************
    > File names: c.cpp
    > Author:zhoubo_NLP
    > Mail: zhoubo01@baidu.com
    > Created Time: 四  1/12 19:19:59 2017
    > Usage:
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <vector>

const int N = 200;
const int M = 20;

std::string names[N];
std::string dialogues[N];
std::set<std::string> candidate[N];

void init(int n, int m) {
  for (int i = 0; i < m; ++i) {
    candidate[i].clear();
    for (int j = 0; j < n; ++j) {
      //std::cout << "[init] inset  " << names[j] << std::endl;
      candidate[i].insert(names[j]);
    }
  }
}

void extract(int cur_index, const std::string dialogue) {
  //std::cout << "[extract] " << dialogue << std::endl;
  int len = dialogue.length();
  std::string username;
  int index = 0;
  while(dialogue[index] != ':') username += dialogue[index++];
  // known user
  if (username != "?") {
    candidate[cur_index].clear();
    candidate[cur_index].insert(username);
  }
  std::string word;
  for (int i = index; i < len; ++i) {
    if (dialogue[i] == ' ' || dialogue[i] == ',' || dialogue[i] == '!' || dialogue[i] == '?') {
      if (word.length() != 0) {
        //std::cout << "word " << word << std::endl;
        candidate[cur_index].erase(word);
      }
      word = "";
    } else {
      word += dialogue[i];
    }
  }
  candidate[cur_index].erase(word);
  //std::cout << "[extract end] size: " << candidate[cur_index].size() << std::endl;
}

std::map<std::string, int> to_id;
int dp[N][N], path[N][N];

int main() {
  int t;
  std::cin >> t;
  while(t--) {
    to_id.clear();
    int n, m;
    std::cin >> n;
    for (int i = 0; i < n; ++i) {
      std::cin >> names[i];
      to_id[names[i]] = i;
    }
    std::cin >> m;
    init(n, m);
    std::getline(std::cin, dialogues[0]);
    for (int i = 0; i < m; ++i) {
      std::getline(std::cin, dialogues[i]);
      //std::cout << "get " << dialogues[i] << std::endl;
      extract(i, dialogues[i]);
    }
    while (true) {
      bool update = false;
      for (int i = 0; i < m; ++i) {
        if (candidate[i].size() == 1) {
          const std::string& name = *(candidate[i].begin());
          if (i > 0) {
            int ret = candidate[i - 1].erase(name);
            if (ret) {
              update = true;
            }
          }
          if (i < m - 1) {
            int ret = candidate[i + 1].erase(name);
            if (ret) {
              update = true;
            }
          }
        } // size == 1
      }
      if (!update) {
        break;
      }
    }
    // dp
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        dp[i][j] = false;
      }
    }
    for (int j = 0;j < n; ++j) {
      const std::string name = names[j];
      if (candidate[0].count(name)) {
        dp[0][j] = true;
      }
    } // init end
    //
    bool ans = true;
    for (int i = 1; i < m && ans; ++i) {
      bool t_ans = false;
      for (int j = 0;j < n; ++j) {
        const std::string name_j = names[j];
        if (candidate[i].count(name_j) == 0) {
          continue;
        }
        for (int k = 0; k < n; ++k) {
          if (k == j) continue;
          if (dp[i - 1][k]) {
            dp[i][j] = true;
            path[i][j] = k;
            t_ans = true;
            break;
          }
        }
      }
      if (t_ans == false) {
        ans = false;
      }
    }
    if (!ans) {
      std::cout << "Impossible" << std::endl;
    } else {
      std::vector<std::string> record;
      int cur_pos;
      for (int i = 0; i < n; ++i) {
        if (dp[m - 1][i]) {
          cur_pos = i;
        }
      }
      for (int i = m - 1; i >= 0; --i) {
        record.push_back(names[cur_pos]);
        cur_pos = path[i][cur_pos];
      }
      std::reverse(record.begin(), record.end());
      for (int i = 0; i < m; ++i) {
        if (dialogues[i][0] == '?') {
          std::cout << record[i] << dialogues[i].substr(1) << std::endl;
        } else {
          std::cout << dialogues[i] << std::endl;
        }
      }
    }
  }
  return 0;
}


/*************************************************************************
    > File names: c.cpp
    > Author:zhoubo_NLP
    > Mail: zhoubo01@baidu.com
    > Created Time: 四  1/12 19:19:59 2017
    > Usage:
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <vector>

const int N = 200;
const int M = 20;

std::string names[N];
std::string dialogues[N];
std::set<std::string> candidate[N];

void init(int n, int m) {
  for (int i = 0; i < m; ++i) {
    candidate[i].clear();
    for (int j = 0; j < n; ++j) {
      //std::cout << "[init] inset  " << names[j] << std::endl;
      candidate[i].insert(names[j]);
    }
  }
}

bool is_good(char ch) {
  if (ch >= '0' && ch <= '9') return true;
  if (ch >= 'a' && ch <= 'z') return true;
  if (ch >= 'A' && ch <= 'Z') return true;
  return false;
}

void extract(int cur_index, const std::string dialogue) {
  //std::cout << "[extract] " << dialogue << std::endl;
  int len = dialogue.length();
  std::string username;
  int index = 0;
  while(dialogue[index] != ':') username += dialogue[index++];
  // known user
  if (username != "?") {
    candidate[cur_index].clear();
    candidate[cur_index].insert(username);
  }
  std::string word;
  for (int i = index; i < len; ++i) {
    char ch = dialogue[i];
    if (is_good(ch)) {
      word += ch;
    } else if (!word.empty()) {
      candidate[cur_index].erase(word);
      word = "";
    }
  }
  candidate[cur_index].erase(word);
  //std::cout << "[extract end] size: " << candidate[cur_index].size() << std::endl;
}

std::map<std::string, int> to_id;
int dp[N][N], path[N][N];

int main() {
  int t;
  std::cin >> t;
  while(t--) {
    to_id.clear();
    int n, m;
    std::cin >> n;
    for (int i = 0; i < n; ++i) {
      std::cin >> names[i];
      to_id[names[i]] = i;
    }
    std::cin >> m;
    init(n, m);
    std::getline(std::cin, dialogues[0]);
    for (int i = 0; i < m; ++i) {
      std::getline(std::cin, dialogues[i]);
      //std::cout << "get " << dialogues[i] << std::endl;
      extract(i, dialogues[i]);
    }
    while (true) {
      bool update = false;
      for (int i = 0; i < m; ++i) {
        if (candidate[i].size() == 1) {
          const std::string& name = *(candidate[i].begin());
          if (i > 0) {
            int ret = candidate[i - 1].erase(name);
            if (ret) {
              update = true;
            }
          }
          if (i < m - 1) {
            int ret = candidate[i + 1].erase(name);
            if (ret) {
              update = true;
            }
          }
        } // size == 1
      }
      if (!update) {
        break;
      }
    }
    // dp
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        dp[i][j] = false;
      }
    }
    for (int j = 0;j < n; ++j) {
      const std::string name = names[j];
      if (candidate[0].count(name)) {
        dp[0][j] = true;
      }
    } // init end
    //
    bool ans = true;
    for (int i = 1; i < m && ans; ++i) {
      bool t_ans = false;
      for (int j = 0;j < n; ++j) {
        const std::string name_j = names[j];
        if (candidate[i].count(name_j) == 0) {
          continue;
        }
        for (int k = 0; k < n; ++k) {
          if (k == j) continue;
          if (dp[i - 1][k]) {
            dp[i][j] = true;
            path[i][j] = k;
            t_ans = true;
            break;
          }
        }
      }
      if (t_ans == false) {
        ans = false;
      }
    }
    if (!ans) {
      std::cout << "Impossible" << std::endl;
    } else {
      std::vector<std::string> record;
      int cur_pos;
      for (int i = 0; i < n; ++i) {
        if (dp[m - 1][i]) {
          cur_pos = i;
        }
      }
      for (int i = m - 1; i >= 0; --i) {
        record.push_back(names[cur_pos]);
        cur_pos = path[i][cur_pos];
      }
      std::reverse(record.begin(), record.end());
      for (int i = 0; i < m; ++i) {
        if (dialogues[i][0] == '?') {
          std::cout << record[i] << dialogues[i].substr(1) << std::endl;
        } else {
          std::cout << dialogues[i] << std::endl;
        }
      }
    }
  }
  return 0;
}


/*************************************************************************
    > File names: c.cpp
    > Author:zhoubo_NLP
    > Mail: zhoubo01@baidu.com
    > Created Time: 四  1/12 19:19:59 2017
    > Usage:
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <vector>

const int N = 200;
const int M = 20;

std::string names[N];
std::string dialogues[N];
std::set<std::string> candidate[N];

void init(int n, int m) {
  for (int i = 0; i < m; ++i) {
    candidate[i].clear();
    for (int j = 0; j < n; ++j) {
      //std::cout << "[init] inset  " << names[j] << std::endl;
      candidate[i].insert(names[j]);
    }
  }
}

bool is_good(char ch) {
  if (ch >= '0' && ch <= '9') return true;
  if (ch >= 'a' && ch <= 'z') return true;
  if (ch >= 'A' && ch <= 'Z') return true;
  return false;
}

void extract(int cur_index, const std::string dialogue) {
  //std::cout << "[extract] " << dialogue << std::endl;
  int len = dialogue.length();
  std::string username;
  int index = 0;
  while(dialogue[index] != ':') username += dialogue[index++];
  // known user
  if (username != "?") {
    candidate[cur_index].clear();
    candidate[cur_index].insert(username);
  }
  std::string word;
  for (int i = index; i < len; ++i) {
    char ch = dialogue[i];
    if (is_good(ch)) {
      word += ch;
    } else if (!word.empty()) {
      candidate[cur_index].erase(word);
      word = "";
    }
  }
  candidate[cur_index].erase(word);
  //std::cout << "[extract end] size: " << candidate[cur_index].size() << std::endl;
}

std::map<std::string, int> to_id;
int dp[N][N], path[N][N];

int main() {
  int t;
  std::cin >> t;
  while(t--) {
    to_id.clear();
    int n, m;
    std::cin >> n;
    for (int i = 0; i < n; ++i) {
      std::cin >> names[i];
      to_id[names[i]] = i;
    }
    std::cin >> m;
    init(n, m);
    std::getline(std::cin, dialogues[0]);
    for (int i = 0; i < m; ++i) {
      std::getline(std::cin, dialogues[i]);
      //std::cout << "get " << dialogues[i] << std::endl;
      extract(i, dialogues[i]);
    }
    while (true) {
      bool update = false;
      for (int i = 0; i < m; ++i) {
        if (candidate[i].size() == 1) {
          const std::string& name = *(candidate[i].begin());
          if (i > 0) {
            int ret = candidate[i - 1].erase(name);
            if (ret) {
              update = true;
            }
          }
          if (i < m - 1) {
            int ret = candidate[i + 1].erase(name);
            if (ret) {
              update = true;
            }
          }
        } // size == 1
      }
      if (!update) {
        break;
      }
    }
    // dp
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        dp[i][j] = false;
      }
    }
    bool ans = false;
    for (int j = 0;j < n; ++j) {
      const std::string name = names[j];
      if (candidate[0].count(name)) {
        dp[0][j] = true;
        ans = true;
      }
    } // init end
    if (!ans) {
      std::cout << "Impossible" << std::endl;
      continue;
    }
    //
    for (int i = 1; i < m && ans; ++i) {
      bool t_ans = false;
      for (int j = 0;j < n; ++j) {
        const std::string name_j = names[j];
        if (candidate[i].count(name_j) == 0) {
          continue;
        }
        for (int k = 0; k < n; ++k) {
          if (k == j) continue;
          if (dp[i - 1][k]) {
            dp[i][j] = true;
            path[i][j] = k;
            t_ans = true;
            break;
          }
        }
      }
      if (t_ans == false) {
        ans = false;
      }
    }
    if (!ans) {
      std::cout << "Impossible" << std::endl;
    } else {
      std::vector<std::string> record;
      int cur_pos;
      for (int i = 0; i < n; ++i) {
        if (dp[m - 1][i]) {
          cur_pos = i;
        }
      }
      for (int i = m - 1; i >= 0; --i) {
        record.push_back(names[cur_pos]);
        cur_pos = path[i][cur_pos];
      }
      std::reverse(record.begin(), record.end());
      for (int i = 0; i < m; ++i) {
        if (dialogues[i][0] == '?') {
          std::cout << record[i] << dialogues[i].substr(1) << std::endl;
        } else {
          std::cout << dialogues[i] << std::endl;
        }
      }
    }
  }
  return 0;
}


/*************************************************************************
    > File names: c.cpp
    > Author:zhoubo_NLP
    > Mail: zhoubo01@baidu.com
    > Created Time: 四  1/12 19:19:59 2017
    > Usage:
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <string>
#include <set>

const int N = 200;
const int M = 20;

std::string names[N];
std::string dialogues[N];
std::set<std::string> candidate[N];

void init(int n, int m) {
  for (int i = 0; i < m; ++i) {
    candidate[i].clear();
    for (int j = 0; j < n; ++j) {
      //std::cout << "[init] inset  " << names[j] << std::endl;
      candidate[i].insert(names[j]);
    }
  }
}

void extract(int cur_index, const std::string dialogue) {
  //std::cout << "[extract] " << dialogue << std::endl;
  int len = dialogue.length();
  std::string username;
  int index = 0;
  while(dialogue[index] != ':') username += dialogue[index++];
  // known user
  if (username != "?") {
    candidate[cur_index].clear();
    candidate[cur_index].insert(username);
  }
  std::string word;
  for (int i = index; i < len; ++i) {
    if (dialogue[i] == ' ' || dialogue[i] == ',' || dialogue[i] == '!' || dialogue[i] == '?') {
      if (word.length() != 0) {
        //std::cout << "word " << word << std::endl;
        candidate[cur_index].erase(word);
      }
      word = "";
    } else {
      word += dialogue[i];
    }
  }
  candidate[cur_index].erase(word);
  //std::cout << "[extract end] size: " << candidate[cur_index].size() << std::endl;
}

int main() {
  int t;
  std::cin >> t;
  while(t--) {
    int n, m;
    std::cin >> n;
    for (int i = 0; i < n; ++i) {
      std::cin >> names[i];
    }
    std::cin >> m;
    init(n, m);
    std::getline(std::cin, dialogues[0]);
    for (int i = 0; i < m; ++i) {
      std::getline(std::cin, dialogues[i]);
      //std::cout << "get " << dialogues[i] << std::endl;
      extract(i, dialogues[i]);
    }
    while (true) {
      bool update = false;
      for (int i = 0; i < m; ++i) {
        if (candidate[i].size() == 1) {
          const std::string& name = *(candidate[i].begin());
          if (i > 0) {
            int ret = candidate[i - 1].erase(name);
            if (ret) {
              update = true;
            }
          }
          if (i < m - 1) {
            int ret = candidate[i + 1].erase(name);
            if (ret) {
              update = true;
            }
          }
        } // size == 1
      }
      if (!update) {
        break;
      }
    }

    bool ans = true;
    for (int i = 0; i < m && ans; ++i) {
      //std::cout << dialogues[i] << "---->" << "candidate_size: " << candidate[i].size() << std::endl;
      if (candidate[i].size() != 1) {
        ans = false;
      }
    }
    //printf("n:%d   m:%d   ans:%d\n", n, m, ans);
    if (!ans) {
      std::cout << "Impossible" << std::endl;
    } else {
      for (int i = 0; i < m; ++i) {
        if (dialogues[i][0] == '?') {
          std::cout << *(candidate[i].begin()) << dialogues[i].substr(1) << std::endl;
        } else {
          std::cout << dialogues[i] << std::endl;
        }
      }
    }
  }
  return 0;
}


/*************************************************************************
    > File names: c.cpp
    > Author:zhoubo_NLP
    > Mail: zhoubo01@baidu.com
    > Created Time: 四  1/12 19:19:59 2017
    > Usage:
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <vector>

const int N = 200;
const int M = 20;

std::string names[N];
std::string dialogues[N];
std::set<std::string> candidate[N];

void init(int n, int m) {
  for (int i = 0; i < m; ++i) {
    candidate[i].clear();
    for (int j = 0; j < n; ++j) {
      //std::cout << "[init] inset  " << names[j] << std::endl;
      candidate[i].insert(names[j]);
    }
  }
}

void extract(int cur_index, const std::string dialogue) {
  //std::cout << "[extract] " << dialogue << std::endl;
  int len = dialogue.length();
  std::string username;
  int index = 0;
  while(dialogue[index] != ':') username += dialogue[index++];
  // known user
  if (username != "?") {
    candidate[cur_index].clear();
    candidate[cur_index].insert(username);
  }
  std::string word;
  for (int i = index; i < len; ++i) {
    if (dialogue[i] == ' ' || dialogue[i] == ',' || dialogue[i] == '!' || dialogue[i] == '?') {
      if (word.length() != 0) {
        //std::cout << "word " << word << std::endl;
        candidate[cur_index].erase(word);
      }
      word = "";
    } else {
      word += dialogue[i];
    }
  }
  candidate[cur_index].erase(word);
  //std::cout << "[extract end] size: " << candidate[cur_index].size() << std::endl;
}

std::map<std::string, int> to_id;
int dp[N][N], path[N][N];

int main() {
  int t;
  std::cin >> t;
  while(t--) {
    to_id.clear();
    int n, m;
    std::cin >> n;
    for (int i = 0; i < n; ++i) {
      std::cin >> names[i];
      to_id[names[i]] = i;
    }
    std::cin >> m;
    init(n, m);
    std::getline(std::cin, dialogues[0]);
    for (int i = 0; i < m; ++i) {
      std::getline(std::cin, dialogues[i]);
      //std::cout << "get " << dialogues[i] << std::endl;
      extract(i, dialogues[i]);
    }
    while (true) {
      bool update = false;
      for (int i = 0; i < m; ++i) {
        if (candidate[i].size() == 1) {
          const std::string& name = *(candidate[i].begin());
          if (i > 0) {
            int ret = candidate[i - 1].erase(name);
            if (ret) {
              update = true;
            }
          }
          if (i < m - 1) {
            int ret = candidate[i + 1].erase(name);
            if (ret) {
              update = true;
            }
          }
        } // size == 1
      }
      if (!update) {
        break;
      }
    }
    // dp
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        dp[i][j] = false;
      }
    }
    for (int j = 0;j < n; ++j) {
      const std::string name = names[j];
      if (candidate[0].count(name)) {
        dp[0][j] = true;
      }
    } // init end
    //
    for (int i = 1; i < m; ++i) {
      for (int j = 0;j < n; ++j) {
        const std::string name_j = names[j];
        if (candidate[i].count(name_j) == 0) {
          continue;
        }
        for (int k = 0; k < n; ++k) {
          if (k == j) continue;
          if (dp[i - 1][k]) {
            dp[i][j] = true;
            path[i][j] = k;
            break;
          }
        }
      }
    }
    bool ans = false;
    for (int i = 0; i < n; ++i) {
      if (dp[m - 1][i]) {
        ans = true;
        break;
      }
    }
    if (!ans) {
      std::cout << "Impossible" << std::endl;
    } else {
      std::vector<std::string> record;
      int cur_pos;
      for (int i = 0; i < n; ++i) {
        if (dp[m - 1][i]) {
          cur_pos = i;
        }
      }
      for (int i = m - 1; i >= 0; --i) {
        record.push_back(names[cur_pos]);
        cur_pos = path[i][cur_pos];
      }
      std::reverse(record.begin(), record.end());
      for (int i = 0; i < m; ++i) {
        if (dialogues[i][0] == '?') {
          std::cout << record[i] << dialogues[i].substr(1) << std::endl;
        } else {
          std::cout << dialogues[i] << std::endl;
        }
      }
    }
  }
  return 0;
}


/*************************************************************************
    > File names: c.cpp
    > Author:zhoubo_NLP
    > Mail: zhoubo01@baidu.com
    > Created Time: 四  1/12 19:19:59 2017
    > Usage:
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <vector>

const int N = 200;
const int M = 20;

std::string names[N];
std::string dialogues[N];
std::set<std::string> candidate[N];

void init(int n, int m) {
  for (int i = 0; i < m; ++i) {
    candidate[i].clear();
    for (int j = 0; j < n; ++j) {
      //std::cout << "[init] inset  " << names[j] << std::endl;
      candidate[i].insert(names[j]);
    }
  }
}

void extract(int cur_index, const std::string dialogue) {
  //std::cout << "[extract] " << dialogue << std::endl;
  int len = dialogue.length();
  std::string username;
  int index = 0;
  while(dialogue[index] != ':') username += dialogue[index++];
  // known user
  if (username != "?") {
    candidate[cur_index].clear();
    candidate[cur_index].insert(username);
  }
  std::string word;
  for (int i = index; i < len; ++i) {
    if (dialogue[i] == ' ' || dialogue[i] == ',' || dialogue[i] == '!' || dialogue[i] == '?') {
      if (word.length() != 0) {
        //std::cout << "word " << word << std::endl;
        candidate[cur_index].erase(word);
      }
      word = "";
    } else {
      word += dialogue[i];
    }
  }
  candidate[cur_index].erase(word);
  //std::cout << "[extract end] size: " << candidate[cur_index].size() << std::endl;
}

std::map<std::string, int> to_id;
int dp[N][N], path[N][N];

int main() {
  int t;
  std::cin >> t;
  while(t--) {
    to_id.clear();
    int n, m;
    std::cin >> n;
    for (int i = 0; i < n; ++i) {
      std::cin >> names[i];
      to_id[names[i]] = i;
    }
    std::cin >> m;
    init(n, m);
    std::getline(std::cin, dialogues[0]);
    for (int i = 0; i < m; ++i) {
      std::getline(std::cin, dialogues[i]);
      //std::cout << "get " << dialogues[i] << std::endl;
      extract(i, dialogues[i]);
    }
    while (true) {
      bool update = false;
      for (int i = 0; i < m; ++i) {
        if (candidate[i].size() == 1) {
          const std::string& name = *(candidate[i].begin());
          if (i > 0) {
            int ret = candidate[i - 1].erase(name);
            if (ret) {
              update = true;
            }
          }
          if (i < m - 1) {
            int ret = candidate[i + 1].erase(name);
            if (ret) {
              update = true;
            }
          }
        } // size == 1
      }
      if (!update) {
        break;
      }
    }
    // dp
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        dp[i][j] = false;
      }
    }
    for (int j = 0;j < n; ++j) {
      const std::string name = names[j];
      if (candidate[0].count(name)) {
        dp[0][j] = true;
      }
    } // init end
    //
    for (int i = 1; i < m; ++i) {
      for (int j = 0;j < n; ++j) {
        const std::string name_j = names[j];
        if (candidate[i].count(name_j) == 0) {
        }
        for (int k = 0; k < n; ++k) {
          if (k == j) continue;
          if (dp[i - 1][k]) {
            dp[i][j] = true;
            path[i][j] = k;
            break;
          }
        }
      }
    }
    bool ans = false;
    for (int i = 0; i < n; ++i) {
      if (dp[m - 1][i]) {
        ans = true;
        break;
      }
    }
    if (!ans) {
      std::cout << "Impossible" << std::endl;
    } else {
      std::vector<std::string> record;
      int cur_pos;
      for (int i = 0; i < n; ++i) {
        if (dp[m - 1][i]) {
          cur_pos = i;
        }
      }
      for (int i = m - 1; i >= 0; --i) {
        record.push_back(names[cur_pos]);
        cur_pos = path[i][cur_pos];
      }
      std::reverse(record.begin(), record.end());
      for (int i = 0; i < m; ++i) {
        if (dialogues[i][0] == '?') {
          std::cout << record[i] << dialogues[i].substr(1) << std::endl;
        } else {
          std::cout << dialogues[i] << std::endl;
        }
      }
    }
  }
  return 0;
}


#include <algorithm>
#include <cstdio>
#include <cmath>
#include <iterator>
#include <bitset>
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <deque>
#include <cassert>
#include <string>
#include <cstring>
#include <sstream>
#include <climits>

using namespace std;

#define ll long long
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define fs first
#define sc second

map<string, int> mp;
set<int> possible[102];
string users[102], message[102];
int t, n, m, memo[102][102], result[102], cache[102][102];

int dp(int idx, int last) {

	if(idx==m+1) {
		return 1;
	}
	int &ret = memo[idx][last];
	if(ret!=-1) {
		return ret;
	}
	ret = 0;
	for(auto i : possible[idx]) {
		if(i!=last && dp(idx+1, i) == 1) {
			ret = 1;
			cache[idx][last] = i;
			break;
		}
	}
	return ret;
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin>>t;
	while(t--) {

		cin>>n;
		mp.clear();
		for(int i=1; i<=n; i++) {
			cin>>users[i];
			mp[users[i]] = i;
		}
		cin>>m;
		string line;
		getline(cin,line);

		for(int i=1; i<=m; i++) {
			possible[i].clear();
			for(int j=1; j<=n; j++) {
				possible[i].insert(j);
			}
		}

		for(int i=1; i<=m; i++) {

			getline(cin, line);

			int p = line.find(':');
			if(line[0]!='?') {
				string username = line.substr(0, p);
				possible[i].clear();
				possible[i].insert(mp[username]);
				possible[i-1].erase(mp[username]);
				possible[i+1].erase(mp[username]);
			}

			line = line.substr(p+1);
			message[i] = line;
			if(line[0] == ' ') {
				line.erase(line.begin());
			}
			for(int j=0; j<line.length(); ) {
				if(line[j]=='.' || line[j] ==',' || line[j] == '?' || line[j] == '!') {
					line.erase(line.begin()+j);
				} else {
					j++;
				}
			}

			vector<string> temp;
			string tmp;
			stringstream ss(line);

			while(getline(ss, tmp, ' ')) {
				//cout<<tmp<<endl;
				temp.pb(tmp);
			}

			for(int j=0; j<temp.size(); j++) {
				if(mp.find(temp[j])!=mp.end()) {
					possible[i].erase(mp[temp[j]]);
				}
			}

		}

		memset(memo, -1, sizeof(memo));

		if(dp(1,0)==0) {
			cout<<"Impossible"<<endl;
			continue;
		}
		int cur = 1, last = 0;
		while(cur<=m) {
			result[cur] = cache[cur][last];
			last = result[cur];
			cur++;
		}

		for(int i=1; i<=m; i++) {
			cout<<users[result[i]]<<": "<<message[i]<<endl;
		}

	}

	return 0;
}


#include <bits/stdc++.h>
#include <boost/tokenizer.hpp>

using namespace std;
using namespace boost;

#define ll long long
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define fs first
#define sc second

map<string, int> mp;
set<int> possible[102];
string users[102], message[102];
int t, n, m, memo[102][102], result[102], cache[102][102];

int dp(int idx, int last) {

	if(idx==m+1) {
		return 1;
	}
	int &ret = memo[idx][last];
	if(ret!=-1) {
		return ret;
	}
	ret = 0;
	for(auto i : possible[idx]) {
		if(i!=last && dp(idx+1, i) == 1) {
			ret = 1;
			cache[idx][last] = i;
			break;
		}
	}
	return ret;
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin>>t;
	while(t--) {

		cin>>n;

		for(int i=1; i<=n; i++) {
			cin>>users[i];
			mp[users[i]] = i;
		}
		cin>>m;
		string line;
		getline(cin,line);

		for(int i=1; i<=m; i++) {
			possible[i].clear();
			for(int j=1; j<=n; j++) {
				possible[i].insert(j);
			}
		}

		for(int i=1; i<=m; i++) {

			getline(cin, line);

			int p = line.find(':');
			if(line[0]!='?') {
				string username = line.substr(0, p);
				possible[i].clear();
				possible[i].insert(mp[username]);
				possible[i-1].erase(mp[username]), possible[i+1].erase(mp[username]);
			}

			line = line.substr(p+1);
			message[i] = line;
			tokenizer<> tok(line);
			for(tokenizer<> :: iterator it = tok.begin(); it!= tok.end(); ++it) {
				possible[i].erase(mp[*it]);
			}
			memset(memo, -1, sizeof(memo));

			if(dp(1,0)==0) {
				cout<<"Impossible"<<endl;
				continue;
			}
			int cur = 1, last = 0;
			while(cur<=m) {
				result[cur] = cache[cur][last];
				last = result[cur];
				cur++;
			}

			for(int i=1; i<=m; i++) {
				cout<<users[result[i]]<<":"<<message[i]<<endl;
			}

		}

	}

	return 0;
}


#include <algorithm>
#include <cstdio>
#include <cmath>
#include <iterator>
#include <bitset>
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <deque>
#include <cassert>
#include <string>
#include <cstring>
#include <sstream>
#include <climits>

using namespace std;

#define ll long long
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define fs first
#define sc second

map<string, int> mp;
set<int> possible[102];
string users[102], message[102];
int t, n, m, memo[102][102], result[102], cache[102][102];

int dp(int idx, int last) {

	if(idx==m+1) {
		return 1;
	}
	int &ret = memo[idx][last];
	if(ret!=-1) {
		return ret;
	}
	ret = 0;
	for(auto i : possible[idx]) {
		if(i!=last && dp(idx+1, i) == 1) {
			ret = 1;
			cache[idx][last] = i;
			break;
		}
	}
	return ret;
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin>>t;
	while(t--) {

		cin>>n;
		mp.clear();
		for(int i=1; i<=n; i++) {
			cin>>users[i];
			mp[users[i]] = i;
		}
		cin>>m;
		string line;
		getline(cin,line);

		for(int i=1; i<=m; i++) {
			possible[i].clear();
			for(int j=1; j<=n; j++) {
				possible[i].insert(j);
			}
		}

		for(int i=1; i<=m; i++) {

			getline(cin, line);

			int p = line.find(':');
			if(line[0]!='?') {
				string username = line.substr(0, p);
				possible[i].clear();
				possible[i].insert(mp[username]);
				possible[i-1].erase(mp[username]);
				possible[i+1].erase(mp[username]);
			}

			line = line.substr(p+1);
			message[i] = line;

			for(int j=0; j<line.length(); j++) {
				if(line[j]=='.' || line[j] ==',' || line[j] == '?' || line[j] == '!') {
					line[j] = ' ';
				}
			}

			vector<string> temp;
			string tmp;
			stringstream ss(line);

			while(getline(ss, tmp, ' ')) {
				//cout<<tmp<<endl;
				temp.pb(tmp);
			}

			for(int j=0; j<temp.size(); j++) {
				if(mp.find(temp[j])!=mp.end()) {
					possible[i].erase(mp[temp[j]]);
				}
			}

		}

		memset(memo, -1, sizeof(memo));

		if(dp(1,0)==0) {
			cout<<"Impossible"<<endl;
			continue;
		}
		int cur = 1, last = 0;
		while(cur<=m) {
			result[cur] = cache[cur][last];
			last = result[cur];
			cur++;
		}

		for(int i=1; i<=m; i++) {
			cout<<users[result[i]]<<":"<<message[i]<<endl;
		}

	}

	return 0;
}


#include <algorithm>
#include <cstdio>
#include <cmath>
#include <iterator>
#include <bitset>
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <deque>
#include <cassert>
#include <string>
#include <cstring>
#include <sstream>
#include <climits>

using namespace std;

#define ll long long
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define fs first
#define sc second

map<string, int> mp;
set<int> possible[102];
string users[102], message[102];
int t, n, m, memo[102][102], result[102], cache[102][102];

int dp(int idx, int last) {

	if(idx==m+1) {
		return 1;
	}
	int &ret = memo[idx][last];
	if(ret!=-1) {
		return ret;
	}
	ret = 0;
	for(auto i : possible[idx]) {
		if(i!=last && dp(idx+1, i) == 1) {
			ret = 1;
			cache[idx][last] = i;
			break;
		}
	}
	return ret;
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin>>t;
	while(t--) {

		cin>>n;
		mp.clear();
		for(int i=1; i<=n; i++) {
			cin>>users[i];
			mp[users[i]] = i;
		}
		cin>>m;
		string line;
		getline(cin,line);

		for(int i=1; i<=m; i++) {
			possible[i].clear();
			for(int j=1; j<=n; j++) {
				possible[i].insert(j);
			}
		}

		for(int i=1; i<=m; i++) {

			getline(cin, line);

			int p = line.find(':');
			if(line[0]!='?') {
				string username = line.substr(0, p);
				possible[i].clear();
				possible[i].insert(mp[username]);
				possible[i-1].erase(mp[username]);
				possible[i+1].erase(mp[username]);
			}

			line = line.substr(p+1);
			message[i] = line;
			if(line[0] == ' ') {
				line.erase(line.begin());
			}
			for(int j=0; j<line.length(); ) {
				if(line[j]=='.' || line[j] ==',' || line[j] == '?' || line[j] == '!') {
					line.erase(line.begin()+j);
				} else {
					j++;
				}
			}

			vector<string> temp;
			string tmp;
			stringstream ss(line);

			while(getline(ss, tmp, ' ')) {
				//cout<<tmp<<endl;
				temp.pb(tmp);
			}

			for(int j=0; j<temp.size(); j++) {
				if(mp.find(temp[j])!=mp.end()) {
					possible[i].erase(mp[temp[j]]);
				}
			}

		}

		memset(memo, -1, sizeof(memo));

		if(dp(1,0)==0) {
			cout<<"Impossible"<<endl;
			continue;
		}
		int cur = 1, last = 0;
		while(cur<=m) {
			result[cur] = cache[cur][last];
			last = result[cur];
			cur++;
		}

		for(int i=1; i<=m; i++) {
			cout<<users[result[i]]<<":"<<message[i]<<endl;
		}

	}

	return 0;
}


#include <algorithm>
#include <cstdio>
#include <cmath>
#include <iterator>
#include <bitset>
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <deque>
#include <cassert>
#include <string>
#include <cstring>
#include <sstream>
#include <climits>

using namespace std;

#define ll long long
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define fs first
#define sc second

map<string, int> mp;
set<int> possible[102];
string users[102], message[102];
int t, n, m, memo[102][102], result[102], cache[102][102];

int dp(int idx, int last) {

	if(idx==m+1) {
		return 1;
	}
	int &ret = memo[idx][last];
	if(ret!=-1) {
		return ret;
	}
	ret = 0;
	for(auto i : possible[idx]) {
		if(i!=last && dp(idx+1, i) == 1) {
			ret = 1;
			cache[idx][last] = i;
			break;
		}
	}
	return ret;
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin>>t;
	while(t--) {

		cin>>n;
		mp.clear();
		for(int i=1; i<=n; i++) {
			cin>>users[i];
			mp[users[i]] = i;
		}
		cin>>m;
		string line;
		getline(cin,line);

		for(int i=1; i<=m; i++) {
			possible[i].clear();
			for(int j=1; j<=n; j++) {
				possible[i].insert(j);
			}
		}

		for(int i=1; i<=m; i++) {

			getline(cin, line);

			int p = line.find(':');
			if(line[0]!='?') {
				string username = line.substr(0, p);
				possible[i].clear();
				possible[i].insert(mp[username]);
				possible[i-1].erase(mp[username]);
				possible[i+1].erase(mp[username]);
			}

			line = line.substr(p+2);
			message[i] = line;

			for(int j=0; j<line.length(); ) {
				if(line[j]=='.' || line[j] ==',' || line[j] == '?' || line[j] == '!') {
					line.erase(line.begin()+j);
				} else {
					j++;
				}
			}

			vector<string> temp;
			string tmp;
			stringstream ss(line);

			while(getline(ss, tmp, ' ')) {
				//cout<<tmp<<endl;
				temp.pb(tmp);
			}

			for(int j=0; j<temp.size(); j++) {
				if(mp.find(temp[j])!=mp.end()) {
					possible[i].erase(mp[temp[j]]);
				}
			}

		}

		memset(memo, -1, sizeof(memo));

		if(dp(1,0)==0) {
			cout<<"Impossible"<<endl;
			continue;
		}
		int cur = 1, last = 0;
		while(cur<=m) {
			result[cur] = cache[cur][last];
			last = result[cur];
			cur++;
		}

		for(int i=1; i<=m; i++) {
			cout<<users[result[i]]<<": "<<message[i]<<endl;
		}

	}

	return 0;
}


#include <algorithm>
#include <cstdio>
#include <cmath>
#include <iterator>
#include <bitset>
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <deque>
#include <cassert>
#include <string>
#include <cstring>
#include <sstream>
#include <climits>

using namespace std;

#define ll long long
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define fs first
#define sc second

map<string, int> mp;
set<int> possible[102];
string users[102], message[102];
int t, n, m, memo[102][102], result[102], cache[102][102];

int dp(int idx, int last) {

	if(idx==m+1) {
		return 1;
	}
	int &ret = memo[idx][last];
	if(ret!=-1) {
		return ret;
	}
	ret = 0;
	for(auto i : possible[idx]) {
		if(i!=last && dp(idx+1, i) == 1) {
			ret = 1;
			cache[idx][last] = i;
			break;
		}
	}
	return ret;
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin>>t;
	while(t--) {

		cin>>n;
		mp.clear();
		for(int i=1; i<=n; i++) {
			cin>>users[i];
			mp[users[i]] = i;
		}
		cin>>m;
		string line;
		getline(cin,line);

		for(int i=1; i<=m; i++) {
			possible[i].clear();
			for(int j=1; j<=n; j++) {
				possible[i].insert(j);
			}
		}

		for(int i=1; i<=m; i++) {

			getline(cin, line);

			int p = line.find(':');
			if(line[0]!='?') {
				string username = line.substr(0, p);
				possible[i].clear();
				possible[i].insert(mp[username]);
				possible[i-1].erase(mp[username]);
				possible[i+1].erase(mp[username]);
			}

			line = line.substr(p+1);
			message[i] = line;

			for(int j=0; j<line.length(); j++) {
				if(line[j]=='.' || line[j] ==',' || line[j] == '?' || line[j] == '!') {
					line[j] = ' ';
				}
			}

			vector<string> temp;
			string tmp;
			stringstream ss(line);

			while(getline(ss, tmp, ' ')) {
				temp.pb(tmp);
			}

			for(int j=0; j<temp.size(); j++) {
				possible[i].erase(mp[temp[j]]);

			}

		}

		memset(memo, -1, sizeof(memo));

		if(dp(1,0)==0) {
			cout<<"Impossible"<<endl;
			continue;
		}

		//tracing the path
		int cur = 1, last = 0;
		while(cur<=m) {
			result[cur] = cache[cur][last];
			last = result[cur];
			cur++;
		}

		for(int i=1; i<=m; i++) {
			cout<<users[result[i]]<<":"<<message[i]<<endl;
		}

	}

	return 0;
}


#include <algorithm>
#include <cstdio>
#include <cmath>
#include <iterator>
#include <bitset>
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <deque>
#include <cassert>
#include <string>
#include <cstring>
#include <sstream>
#include <climits>

using namespace std;

#define ll long long
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define fs first
#define sc second

map<string, int> mp;
set<int> possible[102];
string users[102], message[102];
int t, n, m, memo[102][102], result[102], cache[102][102];

int dp(int idx, int last) {

	if(idx==m+1) {
		return 1;
	}
	int &ret = memo[idx][last];
	if(ret!=-1) {
		return ret;
	}
	ret = 0;
	for(auto i : possible[idx]) {
		if(i!=last && dp(idx+1, i) == 1) {
			ret = 1;
			cache[idx][last] = i;
			break;
		}
	}
	return ret;
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin>>t;
	while(t--) {

		cin>>n;
		mp.clear();
		for(int i=1; i<=n; i++) {
			cin>>users[i];
			mp[users[i]] = i;
		}
		cin>>m;
		string line;
		getline(cin,line);

		for(int i=1; i<=m; i++) {
			possible[i].clear();
			for(int j=1; j<=n; j++) {
				possible[i].insert(j);
			}
		}

		for(int i=1; i<=m; i++) {

			getline(cin, line);

			int p = line.find(':');
			if(line[0]!='?') {
				string username = line.substr(0, p);
				possible[i].clear();
				possible[i].insert(mp[username]);
				possible[i-1].erase(mp[username]);
				possible[i+1].erase(mp[username]);
			}

			line = line.substr(p+1);
			message[i] = line;

			for(int j=0; j<line.length(); j++) {
				if(line[j]=='.' || line[j] ==',' || line[j] == '?' || line[j] == '!') {
					line[j] = ' ';
				}
			}

			vector<string> temp;
			string tmp;
			stringstream ss(line);

			while(ss<<tmp) {
				temp.pb(tmp);
			}

			for(int j=0; j<temp.size(); j++) {
				possible[i].erase(mp[temp[j]]);

			}

		}

		memset(memo, -1, sizeof(memo));

		if(dp(1,0)==0) {
			cout<<"Impossible"<<endl;
			continue;
		}

		//tracing the path
		int cur = 1, last = 0;
		while(cur<=m) {
			result[cur] = cache[cur][last];
			last = result[cur];
			cur++;
		}

		for(int i=1; i<=m; i++) {
			cout<<users[result[i]]<<":"<<message[i]<<endl;
		}

	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define INF (int)(1e9+7)
#define eps 1e-9
typedef long long ll;
typedef vector<int> veci;
typedef pair<int,int> pii;
typedef vector<pii> vecii;
#define fi first
#define se second
#define endl '\n'

int n, m;
const string search_str = ".,!? ";
map<string,int> name;
vector<string> str;
string ns;
vector<map<string,int> > cand;
veci vis;

bool dfs(int k) {
    if (k==m)
        return true;

    for (auto it:cand[k]) {
        auto itt = name.find(it.fi);
        if (vis[itt->se]==-1) {
            vis[itt->se] = k;
            if (dfs(k+1))
                return true;
            vis[itt->se] = -1;
        }
    }

    return false;
}

int main() {
    int tcs;
    cin >> tcs;
    while (tcs-->0)
{
//    ios::sync_with_stdio(false);
    cin >> n;
    name.clear();
    for (int i=0; i<n; ++i) {
        cin >> ns;
        name[ns] = 0;
    }

    int p = 0;
    for (auto it=name.begin(); it!=name.end(); ++it, ++p)
        it->se = p;

    cin >> m;
    cin.ignore();
    cand.clear();
    cand.resize(m);
    str.clear();
    str.resize(m);
    int pname;
    bool res = true;
    for (int i=0; i<m; ++i) {
        getline(cin, str[i]);
        pname = str[i].find(':');
        ns = str[i].substr(0, pname);
        str[i] = str[i].substr(pname+1);
        if (ns=="?") {
            map<string,int> ncand;
            string scpy = str[i];
            while (true) {
                pname = scpy.find_first_of(search_str);

                ++ncand[scpy.substr(0, pname)];
                scpy = scpy.substr(pname+1);

                if (pname==(int)string::npos)
                    break;
            }

            for (auto ne:name) {
                if (ncand.find(ne.fi)==ncand.end()) {
                    ++cand[i][ne.fi];
                }
            }
        } else {
            ++cand[i][ns];
        }
    }

    if (res) {
        vis.clear();
        vis.resize(n,-1);
        res = dfs(0);
    }

    if (res) {
        vecii nstr;
        for (int i=0; i<n; ++i)
            nstr.push_back(pii(vis[i],i));
        sort(nstr.begin(), nstr.end());

        for (int i=0; i<m; ++i) {
            auto it = name.begin();
            for (int kk=0; kk<nstr[i].se; ++kk)
                it = next(it);
            cout << it->fi << ":" << str[i] << endl;
        }
    } else {
        cout << "Impossible" << endl;
    }
}}


#include <bits/stdc++.h>
using namespace std;

#define INF (int)(1e9+7)
#define eps 1e-9
typedef long long ll;
typedef vector<int> veci;
typedef pair<int,int> pii;
typedef vector<pii> vecii;
#define fi first
#define se second
#define endl '\n'

int n, m;
const string search_str = ".,!? ";
map<string,int> name;
vector<string> str;
string ns;
vector<map<string,int> > cand;
vector<string> vis;
vector<map<string,int> > dp;

bool dfs(int k) {
    if (k==m)
        return true;

    for (auto it:cand[k]) {
        if (dp[k].find(it.fi)==dp[k].end()) {
            if (k==0 || vis[k-1]!=it.fi) {
                vis[k] = it.fi;
                if (dfs(k+1)) {
                    return true;
                } else {
                    dp[k][it.fi] = 0;
                    vis[k] = "";
                }
            }
        }
    }

    return false;
}

int main() {
    ios::sync_with_stdio(false);
    int tcs;
    cin >> tcs;
    while (tcs-->0)
{
    cin >> n;
    name.clear();
    for (int i=0; i<n; ++i) {
        cin >> ns;
        name[ns] = 0;
    }

    int p = 0;
    for (auto it=name.begin(); it!=name.end(); ++it, ++p)
        it->se = p;

    cin >> m;
    cand.clear();
    cand.resize(m);
    str.clear();
    str.resize(m);
    int pname;
    bool res = true;
    cin.ignore();
    for (int i=0; i<m; ++i) {
        getline(cin, str[i]);
        pname = str[i].find(':');
        ns = str[i].substr(0, pname);
        str[i] = str[i].substr(pname+1);
        if (ns=="?") {
            map<string,int> ncand;
            string scpy = str[i];
            while (true) {
                pname = scpy.find_first_of(search_str);

                ++ncand[scpy.substr(0, pname)];
                scpy = scpy.substr(pname+1);
                if (pname==(int)string::npos)
                    break;
            }

            for (auto ne:name) {
                if (ncand.find(ne.fi)==ncand.end()) {
                    ++cand[i][ne.fi];
                }
            }
        } else {
            ++cand[i][ns];
        }
    }

    vis.clear();
    vis.resize(m,"");
	dp.clear();
    dp.resize(m);
    res = dfs(0);

    if (res) {
        for (int i=0; i<m; ++i) {
            cout << vis[i] << ":" << str[i] << endl;
        }
    } else {
        cout << "Impossible" << endl;
    }
}}


#include <bits/stdc++.h>
using namespace std;

#define INF (int)(1e9+7)
#define eps 1e-9
typedef long long ll;
typedef vector<int> veci;
typedef pair<int,int> pii;
typedef vector<pii> vecii;
#define fi first
#define se second
#define endl '\n'

int n, m;
const string search_str = ".,!? ";
map<string,int> name;
vector<string> str;
string ns;
vector<map<string,int> > cand;
vector<string> vis;

bool dfs(int k) {
    if (k==m)
        return true;

    for (auto it:cand[k]) {
        if (k==0 || vis[k-1]!=it.fi) {
            vis[k] = it.fi;
            if (dfs(k+1))
                return true;
            vis[k] = "";
        }
    }

    return false;
}

int main() {
    ios::sync_with_stdio(false);
    int tcs;
    cin >> tcs;
    while (tcs-->0)
{
    cin >> n;
    name.clear();
    for (int i=0; i<n; ++i) {
        cin >> ns;
        name[ns] = 0;
    }

    int p = 0;
    for (auto it=name.begin(); it!=name.end(); ++it, ++p)
        it->se = p;

    cin >> m;
    cand.clear();
    cand.resize(m);
    str.clear();
    str.resize(m);
    int pname;
    bool res = true;
    cin.ignore();
    for (int i=0; i<m; ++i) {
        getline(cin, str[i]);
        pname = str[i].find(':');
        ns = str[i].substr(0, pname);
        str[i] = str[i].substr(pname+1);
        if (ns=="?") {
            map<string,int> ncand;
            string scpy = str[i];
            while (true) {
                pname = scpy.find_first_of(search_str);

                ++ncand[scpy.substr(0, pname)];
                scpy = scpy.substr(pname+1);
                if (pname==(int)string::npos)
                    break;
            }

            for (auto ne:name) {
                if (ncand.find(ne.fi)==ncand.end()) {
                    ++cand[i][ne.fi];
                }
            }
        } else {
            ++cand[i][ns];
        }
    }

    vis.clear();
    vis.resize(m,"");
    res = dfs(0);

    if (res) {
        for (int i=0; i<m; ++i) {
            cout << vis[i] << ":" << str[i] << endl;
        }
    } else {
        cout << "Impossible" << endl;
    }
}}


#include <bits/stdc++.h>
using namespace std;

#define INF (int)(1e9+7)
#define eps 1e-9
typedef long long ll;
typedef vector<int> veci;
typedef pair<int,int> pii;
typedef vector<pii> vecii;
#define fi first
#define se second
#define endl '\n'

int n, m;
const string search_str = ".,!? \0";
map<string,int> name;
vector<string> str;
string ns;
vector<map<string,int> > cand;
veci vis;

bool dfs(int k) {
    if (k==m)
        return true;

    for (auto it:cand[k]) {
        auto itt = name.find(it.fi);
        if (vis[itt->se]==n) {
            vis[itt->se] = k;
            if (dfs(k+1))
                return true;
            vis[itt->se] = n;
        }
    }

    return false;
}

int main() {
    ios::sync_with_stdio(false);
    int tcs;
    cin >> tcs;
    while (tcs-->0)
{
    cin >> n;
    name.clear();
    for (int i=0; i<n; ++i) {
        cin >> ns;
        name[ns] = 0;
    }

    int p = 0;
    for (auto it=name.begin(); it!=name.end(); ++it, ++p)
        it->se = p;

    cin >> m;
    cin.ignore();
    cand.clear();
    cand.resize(m);
    str.clear();
    str.resize(m);
    int pname;
    bool res = true;
    for (int i=0; i<m; ++i) {
        getline(cin, str[i]);
        pname = str[i].find(':');
        ns = str[i].substr(0, pname);
        str[i] = str[i].substr(pname+1);
        if (ns=="?") {
            map<string,int> ncand;
            string scpy = str[i];
            while (true) {
                pname = scpy.find_first_of(search_str);

                ++ncand[scpy.substr(0, pname)];
                scpy = scpy.substr(pname+1);
                if (pname==(int)string::npos) {
                    ++ncand[scpy];
                    break;
                }
            }

            for (auto ne:name) {
                if (ncand.find(ne.fi)==ncand.end()) {
                    ++cand[i][ne.fi];
                }
            }
        } else {
            ++cand[i][ns];
        }
    }

    vis.clear();
    vis.resize(n,n);
    res = dfs(0);

    if (res) {
        vecii nstr;
        for (int i=0; i<n; ++i)
            nstr.push_back(pii(vis[i],i));
        sort(nstr.begin(), nstr.end());

        for (int i=0; i<m; ++i) {
            auto it = name.begin();
            for (int kk=0; kk<nstr[i].se; ++kk)
                it = next(it);
            cout << it->fi << ":" << str[i] << endl;
        }
    } else {
        cout << "Impossible" << endl;
    }
}}


#include <bits/stdc++.h>
using namespace std;

#define INF (int)(1e9+7)
#define eps 1e-9
typedef long long ll;
typedef vector<int> veci;
typedef pair<int,int> pii;
typedef vector<pii> vecii;
#define fi first
#define se second
#define endl '\n'

int n, m;
const string search_str = ".,!? ";
map<string,int> name;
vector<string> str;
string ns;
vector<map<string,int> > cand;
veci vis;

bool dfs(int k) {
    if (k==m)
        return true;

    for (auto it:cand[k]) {
        auto itt = name.find(it.fi);
        if (vis[itt->se]==n) {
            vis[itt->se] = k;
            if (dfs(k+1))
                return true;
            vis[itt->se] = n;
        }
    }

    return false;
}

int main() {
    int tcs;
    cin >> tcs;
    while (tcs-->0)
{
//    ios::sync_with_stdio(false);
    cin >> n;
    name.clear();
    for (int i=0; i<n; ++i) {
        cin >> ns;
        name[ns] = 0;
    }

    int p = 0;
    for (auto it=name.begin(); it!=name.end(); ++it, ++p)
        it->se = p;

    cin >> m;
    cin.ignore();
    cand.clear();
    cand.resize(m);
    str.clear();
    str.resize(m);
    int pname;
    bool res = true;
    for (int i=0; i<m; ++i) {
        getline(cin, str[i]);
        pname = str[i].find(':');
        ns = str[i].substr(0, pname);
        str[i] = str[i].substr(pname+1);
        if (ns=="?") {
            map<string,int> ncand;
            string scpy = str[i];
            while (true) {
                pname = scpy.find_first_of(search_str);

                ++ncand[scpy.substr(0, pname)];
                scpy = scpy.substr(pname+1);

                if (pname==(int)string::npos)
                    break;
            }

            for (auto ne:name) {
                if (ncand.find(ne.fi)==ncand.end()) {
                    ++cand[i][ne.fi];
                }
            }
        } else {
            ++cand[i][ns];
        }
    }

    if (res) {
        vis.clear();
        vis.resize(n,n);
        res = dfs(0);
    }

    if (res) {
        vecii nstr;
        for (int i=0; i<n; ++i)
            nstr.push_back(pii(vis[i],i));
        sort(nstr.begin(), nstr.end());

        for (int i=0; i<m; ++i) {
            auto it = name.begin();
            for (int kk=0; kk<nstr[i].se; ++kk)
                it = next(it);
            cout << it->fi << ":" << str[i] << endl;
        }
    } else {
        cout << "Impossible" << endl;
    }
}}


#include <bits/stdc++.h>

using namespace std;

string mes[105];
string users[105];
map<string,int> mm;
int imp[105][105];
int us[105],nus[105];
int mem[105][105];
char tmp[200];
bool ok;
int n,m;

bool okk(char ch)
{
    if(('0'<=ch&&ch<='9')||('a'<=ch&&ch<='z')||('A'<=ch&&ch<='Z'))
        return 1;
    return 0;
}

string deln(string s)
{
    int i;
    string ans="";
    for(i=0;i<s.size();i++)
    {
        if(okk(s[i]))
           ans+=s[i];
    }
    return ans;
}

void clear()
{
    mm.clear();
    int i;
    for(i=0;i<max(n,m);i++)
    {
        us[i]=-1;
        nus[i]=-1;
        mes[i]="";
        users[i]="";
    }
    memset(imp,0,sizeof imp);
    memset(mem,-1,sizeof mem);
    ok=0;
}

int go(int ind,int la)
{
    if(ind>=m)
    {
        ok=1;
        return mem[ind][la]=1;
    }
    if(mem[ind][la]!=-1)
        return mem[ind][la];
    if(us[ind]!=-1)
    {
        if(us[ind]==la)
            return mem[ind][la]=0;
        if(imp[ind][us[ind]])
        {
            assert(false);
            return 0;
        }
        return mem[ind][la]=go(ind+1,us[ind]);
    }
    mem[ind][la]=0;
    for(int i=0;i<n;i++)
    {
        if(imp[ind][i])
            continue;
        if(i==la)
            continue;
        mem[ind][la]|=go(ind+1,i);
    }
    return mem[ind][la];
}

void bt(int ind,int la)
{
    if(ind>=m)
        return;
    if(us[ind]!=-1)
    {
        nus[ind]=us[ind];
        bt(ind+1,us[ind]);
    }
    int i;
    for(i=0;i<n;i++)
    {
        if(imp[ind][i])
            continue;
        if(i==la)
            continue;
        if(mem[ind+1][i]==1)
        {
            nus[ind]=i;
            bt(ind+1,i);
            return;
        }
    }
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        clear();
        scanf("%d",&n);
        int i,j;
        for(i=0;i<n;i++)
        {
            char uss[25];
            scanf("%s",&uss);
            users[i]=uss;
            mm[users[i]]=i;
        }
        mm["?"]=-1;
        scanf("%d",&m);
        mes[0]=gets(tmp);
        for(i=0;i<m;i++)
        {
            mes[i]=gets(tmp);
            mes[i]+='!';
            string cur="";
            for(j=0;j<mes[i].size();j++)
            {
                if(mes[i][j]==':')
                    break;
                cur+=mes[i][j];
            }
            //cur=deln(cur);
            if(mm.find(cur)==mm.end())
                assert(1&&false);
            if(cur=="?")
                us[i]=-1;
            else
                us[i]=mm[cur];
            cur="";
            for(j;j<mes[i].size();j++)
            {
                //cout << j << " " << ok2 << endl;
                if(!okk(mes[i][j]))
                {
                        //cout << cur << endl;
                    //cur=deln(cur);
                    if(cur!=""&&mm.find(cur)!=mm.end())
                    {
                        imp[i][mm[cur]]=1;
                    }
                    cur="";
                }
                else
                {
                    cur+=mes[i][j];
                }
            }
        }
        go(0,n+1);
        bt(0,n+1);
        if(!mem[0][n+1])
        {
            printf("Impossible\n");
            continue;
        }
        for(i=0;i<m;i++)
        {
            mes[i]=mes[i].substr(0,mes[i].size()-1);
            //cout << us[i] << " " << nus[i] << endl;
            if(us[i]==-1)
            {
                mes[i]=mes[i].substr(1);
                printf("%s%s\n",users[nus[i]].c_str(),mes[i].c_str());
            }
            else
            {
                printf("%s\n",mes[i].c_str());
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

string mes[105];
string users[105];
unordered_map<string,int> mm;
int imp[105][105];
int us[105],nus[105];
int mem[105][105];
char tmp[200];
bool ok;
int n,m;

bool okk(char ch)
{
    if(('0'<=ch&&ch<='9')||('a'<=ch&&ch<='z')||('A'<=ch&&ch<='Z'))
        return 1;
    return 0;
}

string deln(string s)
{
    int i;
    string ans="";
    for(i=0;i<s.size();i++)
    {
        if(okk(s[i]))
           ans+=s[i];
    }
    return ans;
}

void clear()
{
    mm.clear();
    int i;
    for(i=0;i<max(n,m);i++)
    {
        us[i]=-1;
        nus[i]=-1;
        mes[i]="";
        users[i]="";
    }
    memset(imp,0,sizeof imp);
    memset(mem,-1,sizeof mem);
    ok=0;
}

int go(int ind,int la)
{
    if(ind>=m)
    {
        ok=1;
        return mem[ind][la]=1;
    }
    if(mem[ind][la]!=-1)
        return mem[ind][la];
    if(us[ind]!=-1)
    {
        if(us[ind]==la)
            return mem[ind][la]=0;
        if(imp[ind][us[ind]])
        {
            assert(false);
            return 0;
        }
        return mem[ind][la]=go(ind+1,us[ind]);
    }
    mem[ind][la]=0;
    for(int i=0;i<n;i++)
    {
        if(imp[ind][i])
            continue;
        if(i==la)
            continue;
        mem[ind][la]|=go(ind+1,i);
        if(go(ind+1,i))
        {
            nus[ind]=i;
            return mem[ind][la];
        }
    }
    return mem[ind][la];
}

void bt(int ind,int la)
{
    if(ind>=m)
        return;
    if(us[ind]!=-1)
    {
        nus[ind]=us[ind];
        bt(ind+1,us[ind]);
    }
    int i;
    for(i=0;i<n;i++)
    {
        if(imp[ind][i])
            continue;
        if(i==la)
            continue;
        if(mem[ind+1][i]==1)
        {
            nus[ind]=i;
            bt(ind+1,i);
            return;
        }
    }
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        clear();
        scanf("%d",&n);
        int i,j;
        for(i=0;i<n;i++)
        {
            char uss[25];
            scanf("%s",&uss);
            users[i]=uss;
            mm[users[i]]=i;
        }
        mm["?"]=-1;
        scanf("%d",&m);
        mes[0]=gets(tmp);
        for(i=0;i<m;i++)
        {
            mes[i]=gets(tmp);
            mes[i]+='!';
            string cur="";
            for(j=0;j<mes[i].size();j++)
            {
                if(mes[i][j]==':')
                    break;
                cur+=mes[i][j];
            }
            //cur=deln(cur);
            if(mm.find(cur)==mm.end())
                assert(1&&false);
            if(cur=="?")
                us[i]=-1;
            else
                us[i]=mm[cur];
            cur="";
            for(j;j<mes[i].size();j++)
            {
                //cout << j << " " << ok2 << endl;
                if(!okk(mes[i][j]))
                {
                        //cout << cur << endl;
                    //cur=deln(cur);
                    if(cur!=""&&mm.find(cur)!=mm.end())
                    {
                        imp[i][mm[cur]]=1;
                    }
                    cur="";
                }
                else
                {
                    cur+=mes[i][j];
                }
            }
        }
        go(0,n+1);
        //bt(0,n+1);
        if(!mem[0][n+1])
        {
            printf("Impossible\n");
            continue;
        }
        for(i=0;i<m;i++)
        {
            mes[i]=mes[i].substr(0,mes[i].size()-1);
            //cout << us[i] << " " << nus[i] << endl;
            if(us[i]==-1)
            {
                mes[i]=mes[i].substr(1);
                printf("%s%s\n",users[nus[i]].c_str(),mes[i].c_str());
            }
            else
            {
                printf("%s\n",mes[i].c_str());
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

string mes[105];
string users[105];
map<string,int> mm;
int imp[105][105];
int us[105],nus[105];
int mem[105][105];
char tmp[200];
bool ok;
int n,m;

bool okk(char ch)
{
    if(('0'<=ch&&ch<='9')||('a'<=ch&&ch<='z')||('A'<=ch&&ch<='Z'))
        return 1;
    return 0;
}

string deln(string s)
{
    int i;
    string ans="";
    for(i=0;i<s.size();i++)
    {
        if(okk(s[i]))
           ans+=s[i];
    }
    return ans;
}

void clear()
{
    mm.clear();
    int i;
    for(i=0;i<max(n,m);i++)
    {
        us[i]=-1;
        nus[i]=-1;
        mes[i]="";
        users[i]="";
    }
    memset(imp,0,sizeof imp);
    memset(mem,-1,sizeof mem);
    memset(us,-1,sizeof us);
    ok=0;
}

int go(int ind,int la)
{
    if(ind>=m)
    {
        ok=1;
        return mem[ind][la]=1;
    }
    if(mem[ind][la]!=-1)
        return mem[ind][la];
    if(us[ind]!=-1)
    {
        if(us[ind]==la)
            return mem[ind][la]=0;
        if(imp[ind][us[ind]])
        {
            assert(false);
            return 0;
        }
        return mem[ind][la]=go(ind+1,us[ind]);
    }
    mem[ind][la]=0;
    for(int i=0;i<n;i++)
    {
        if(imp[ind][i])
            continue;
        if(i==la)
            continue;
        mem[ind][la]|=go(ind+1,i);
    }
    return mem[ind][la];
}

void bt(int ind,int la)
{
    if(ind>=m)
        return;
    if(us[ind]!=-1)
    {
        nus[ind]=us[ind];
        bt(ind+1,us[ind]);
    }
    int i;
    for(i=0;i<n;i++)
    {
        if(imp[ind][i])
            continue;
        if(i==la)
            continue;
        if(mem[ind+1][i]==1)
        {
            nus[ind]=i;
            bt(ind+1,i);
            return;
        }
    }
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        clear();
        scanf("%d",&n);
        int i,j;
        for(i=0;i<n;i++)
        {
            char uss[25];
            scanf("%s",&uss);
            users[i]=uss;
            mm[users[i]]=i;
        }
        mm["?"]=-1;
        scanf("%d",&m);
        mes[0]=gets(tmp);
        for(i=0;i<m;i++)
        {
            mes[i]=gets(tmp);
            mes[i]+='!';
            string cur="";
            for(j=0;j<mes[i].size();j++)
            {
                if(mes[i][j]==':')
                    break;
                cur+=mes[i][j];
            }
            //cur=deln(cur);
            if(mm.find(cur)==mm.end())
                assert(1&&false);
            if(cur=="?")
                us[i]=-1;
            else
                us[i]=mm[cur];
            cur="";
            for(j;j<mes[i].size();j++)
            {
                //cout << j << " " << ok2 << endl;
                if(!okk(mes[i][j]))
                {
                        //cout << cur << endl;
                    cur=deln(cur);
                    if(cur!=""&&mm.find(cur)!=mm.end())
                    {
                        imp[i][mm[cur]]=1;
                    }
                    cur="";
                }
                else
                {
                    cur+=mes[i][j];
                }
            }
        }
        go(0,n+1);
        bt(0,n+1);
        if(!mem[0][n+1])
        {
            printf("Impossible\n");
            continue;
        }
        for(i=0;i<m;i++)
        {
            mes[i]=mes[i].substr(0,mes[i].size()-1);
            //cout << us[i] << " " << nus[i] << endl;
            if(us[i]==-1)
            {
                mes[i]=mes[i].substr(1);
                printf("%s%s\n",users[nus[i]].c_str(),mes[i].c_str());
            }
            else
            {
                printf("%s\n",mes[i].c_str());
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

string mes[105];
string users[105];
unordered_map<string,int> mm;
int imp[105][105];
int us[105],nus[105];
int mem[105][105];
char tmp[200];
bool ok;
int n,m;

bool okk(char ch)
{
    if(('0'<=ch&&ch<='9')||('a'<=ch&&ch<='z')||('A'<=ch&&ch<='Z'))
        return 1;
    return 0;
}

string deln(string s)
{
    int i;
    string ans="";
    for(i=0;i<s.size();i++)
    {
        if(okk(s[i]))
           ans+=s[i];
    }
    return ans;
}

void clear()
{
    mm.clear();
    int i;
    for(i=0;i<max(n,m);i++)
    {
        us[i]=-1;
        nus[i]=-1;
        mes[i]="";
        users[i]="";
    }
    memset(imp,0,sizeof imp);
    memset(mem,-1,sizeof mem);
    ok=0;
}

int go(int ind,int la)
{
    if(ind>=m)
    {
        ok=1;
        return mem[ind][la]=1;
    }
    if(mem[ind][la]!=-1)
        return mem[ind][la];
    if(us[ind]!=-1)
    {
        if(us[ind]==la)
            return mem[ind][la]=0;
        if(imp[ind][us[ind]])
        {
            assert(false);
            return 0;
        }
        return mem[ind][la]=go(ind+1,us[ind]);
    }
    mem[ind][la]=0;
    for(int i=0;i<n;i++)
    {
        if(imp[ind][i])
            continue;
        if(i==la)
            continue;
        mem[ind][la]|=go(ind+1,i);
        if(go(ind+1,i))
            nus[ind]=i;
    }
    return mem[ind][la];
}

void bt(int ind,int la)
{
    if(ind>=m)
        return;
    if(us[ind]!=-1)
    {
        nus[ind]=us[ind];
        bt(ind+1,us[ind]);
    }
    int i;
    for(i=0;i<n;i++)
    {
        if(imp[ind][i])
            continue;
        if(i==la)
            continue;
        if(mem[ind+1][i]==1)
        {
            nus[ind]=i;
            bt(ind+1,i);
            return;
        }
    }
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        clear();
        scanf("%d",&n);
        int i,j;
        for(i=0;i<n;i++)
        {
            char uss[25];
            scanf("%s",&uss);
            users[i]=uss;
            mm[users[i]]=i;
        }
        mm["?"]=-1;
        scanf("%d",&m);
        mes[0]=gets(tmp);
        for(i=0;i<m;i++)
        {
            mes[i]=gets(tmp);
            mes[i]+='!';
            string cur="";
            for(j=0;j<mes[i].size();j++)
            {
                if(mes[i][j]==':')
                    break;
                cur+=mes[i][j];
            }
            //cur=deln(cur);
            if(mm.find(cur)==mm.end())
                assert(1&&false);
            if(cur=="?")
                us[i]=-1;
            else
                us[i]=mm[cur];
            cur="";
            for(j;j<mes[i].size();j++)
            {
                //cout << j << " " << ok2 << endl;
                if(!okk(mes[i][j]))
                {
                        //cout << cur << endl;
                    //cur=deln(cur);
                    if(cur!=""&&mm.find(cur)!=mm.end())
                    {
                        imp[i][mm[cur]]=1;
                    }
                    cur="";
                }
                else
                {
                    cur+=mes[i][j];
                }
            }
        }
        go(0,n+1);
        //bt(0,n+1);
        if(!mem[0][n+1])
        {
            printf("Impossible\n");
            continue;
        }
        for(i=0;i<m;i++)
        {
            mes[i]=mes[i].substr(0,mes[i].size()-1);
            //cout << us[i] << " " << nus[i] << endl;
            if(us[i]==-1)
            {
                mes[i]=mes[i].substr(1);
                printf("%s%s\n",users[nus[i]].c_str(),mes[i].c_str());
            }
            else
            {
                printf("%s\n",mes[i].c_str());
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

string mes[105];
string users[105];
unordered_map<string,int> mm;
int imp[105][105];
int us[105],nus[105];
int mem[105][105];
char tmp[200];
bool ok;
int n,m;

bool okk(char ch)
{
    if(('0'<=ch&&ch<='9')||('a'<=ch&&ch<='z')||('A'<=ch&&ch<='Z'))
        return 1;
    return 0;
}

string deln(string s)
{
    int i;
    string ans="";
    for(i=0;i<s.size();i++)
    {
        if(okk(s[i]))
           ans+=s[i];
    }
    return ans;
}

void clear()
{
    mm.clear();
    int i;
    for(i=0;i<max(n,m);i++)
    {
        us[i]=-1;
        nus[i]=-1;
        mes[i]="";
        users[i]="";
    }
    memset(imp,0,sizeof imp);
    memset(mem,-1,sizeof mem);
    ok=0;
}

int go(int ind,int la)
{
    if(ind>=m)
    {
        ok=1;
        return mem[ind][la]=1;
    }
    if(mem[ind][la]!=-1)
        return mem[ind][la];
    if(us[ind]!=-1)
    {
        if(us[ind]==la)
            return mem[ind][la]=0;
        if(imp[ind][us[ind]])
        {
            assert(false);
            return 0;
        }
        return mem[ind][la]=go(ind+1,us[ind]);
    }
    mem[ind][la]=0;
    for(int i=0;i<n;i++)
    {
        if(imp[ind][i])
            continue;
        if(i==la)
            continue;
        mem[ind][la]|=go(ind+1,i);
        if(go(ind+1,i))
            nus[i]=i;
    }
    return mem[ind][la];
}

void bt(int ind,int la)
{
    if(ind>=m)
        return;
    if(us[ind]!=-1)
    {
        nus[ind]=us[ind];
        bt(ind+1,us[ind]);
    }
    int i;
    for(i=0;i<n;i++)
    {
        if(imp[ind][i])
            continue;
        if(i==la)
            continue;
        if(mem[ind+1][i]==1)
        {
            nus[ind]=i;
            bt(ind+1,i);
            return;
        }
    }
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        clear();
        scanf("%d",&n);
        int i,j;
        for(i=0;i<n;i++)
        {
            char uss[25];
            scanf("%s",&uss);
            users[i]=uss;
            mm[users[i]]=i;
        }
        mm["?"]=-1;
        scanf("%d",&m);
        mes[0]=gets(tmp);
        for(i=0;i<m;i++)
        {
            mes[i]=gets(tmp);
            mes[i]+='!';
            string cur="";
            for(j=0;j<mes[i].size();j++)
            {
                if(mes[i][j]==':')
                    break;
                cur+=mes[i][j];
            }
            //cur=deln(cur);
            if(mm.find(cur)==mm.end())
                assert(1&&false);
            if(cur=="?")
                us[i]=-1;
            else
                us[i]=mm[cur];
            cur="";
            for(j;j<mes[i].size();j++)
            {
                //cout << j << " " << ok2 << endl;
                if(!okk(mes[i][j]))
                {
                        //cout << cur << endl;
                    //cur=deln(cur);
                    if(cur!=""&&mm.find(cur)!=mm.end())
                    {
                        imp[i][mm[cur]]=1;
                    }
                    cur="";
                }
                else
                {
                    cur+=mes[i][j];
                }
            }
        }
        go(0,n+1);
        //bt(0,n+1);
        if(!mem[0][n+1])
        {
            printf("Impossible\n");
            continue;
        }
        for(i=0;i<m;i++)
        {
            mes[i]=mes[i].substr(0,mes[i].size()-1);
            //cout << us[i] << " " << nus[i] << endl;
            if(us[i]==-1)
            {
                mes[i]=mes[i].substr(1);
                printf("%s%s\n",users[nus[i]].c_str(),mes[i].c_str());
            }
            else
            {
                printf("%s\n",mes[i].c_str());
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

using namespace __cxx11;

const string nu = ".,?! ";

set<string> split(string s, set<string> q){
    int x = 0;
    for(int i = 0; i < s.size(); i++){
        if(nu.find(s[i]) != string::npos){
            q.erase(s.substr(x, i - x));
            x = i + 1;
        }
    }
    if(x < s.size())q.erase(s.substr(x));
    return q;
}

int main()
{
    bool b;
    int t, n, m;
    string tmp, tmp2;
    string a[100];
    set<string> msg[100];
    set<string> st;
    cin >> t;
    while(t--){
        b = 1;
        cin >> n;
        st.clear();
        for(int i = 0; i < n; i++){
            cin >> tmp;
            st.insert(tmp);
        }
        cin >> m;
        getline(cin, tmp);
        for(int i = 0; i < m; i++){
            getline(cin, a[i]);
            if(a[i][0] == '?'){
                msg[i] = split(a[i].substr(a[i].find(':') + 1), st);
                msg[i].erase(tmp);
                if(msg[i].size() == 1)a[i] = *msg[i].begin() + a[i].substr(1);
            }
            tmp = a[i].substr(0, a[i].find(':'));
        }
        tmp = "";
        for(int i = m - 1; i > -1; i--){
            if(a[i][0] == '?'){
                msg[i].erase(tmp);
                if(!msg[i].empty())a[i] = *msg[i].begin() + a[i].substr(1);
                else{
                    cout << "Impossible\n";
                    b = 0;
                    break;
                }
            }
            tmp2 = a[i].substr(0, a[i].find(':'));
            if(tmp2 == tmp){
                cout << "Impossible\n";
                b = 0;
                break;
            }
            tmp = tmp2;
        }
        if(b){
            for(int i = 0; i < m; i++)cout << a[i] << "\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

using namespace __cxx11;

const string nu = ".,?! ";

set<string> split(string s, set<string> q){
    int x = 0;
    for(int i = 0; i < s.size(); i++){
        if(nu.find(s[i]) != string::npos){
            q.erase(s.substr(x, i - x));
            x = i + 1;
        }
    }
    if(x < s.size())q.erase(s.substr(x));
    return q;
}

int main()
{
    bool b;
    int t, n, m;
    string tmp, tmp2;
    string a[100];
    set<string> msg[100];
    set<string> st;
    cin >> t;
    while(t--){
        b = 1;
        cin >> n;
        st.clear();
        for(int i = 0; i < n; i++){
            cin >> tmp;
            st.insert(tmp);
        }
        cin >> m;
        getline(cin, tmp);
        for(int i = 0; i < m; i++){
            getline(cin, a[i]);
            if(a[i][0] == '?'){
                msg[i] = split(a[i].substr(a[i].find(':') + 1), st);
                msg[i].erase(tmp);
                if(msg[i].size() == 1)a[i] = *msg[i].begin() + a[i].substr(1);
            }
            tmp = a[i].substr(0, a[i].find(':'));
        }
        tmp = "";
        for(int i = m - 1; i > -1; i--){
            if(a[i][0] == '?'){
                msg[i].erase(tmp2);
                if(msg[i].size() == 1)a[i] = *msg[i].begin() + a[i].substr(1);
                else{
                    cout << "Impossible\n";
                    b = 0;
                    break;
                }
            }
            tmp2 = a[i].substr(0, a[i].find(':'));
            if(tmp2 == tmp){
                cout << "Impossible\n";
                b = 0;
                break;
            }
            tmp = tmp2;
        }
        if(b){
            for(int i = 0; i < m; i++)cout << a[i] << "\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

using namespace __cxx11;

const string nu = ".,?! ";

set<string> split(string s, set<string> q){
    int x = 0;
    for(int i = 0; i < s.size(); i++){
        if(nu.find(s[i]) != string::npos){
            q.erase(s.substr(x, i - x));
            x = i + 1;
        }
    }
    if(x < s.size())q.erase(s.substr(x));
    return q;
}

int main()
{
    bool b;
    int t, n, m;
    string tmp, tmp2;
    string a[100];
    set<string> msg[100];
    set<string> st;
    cin >> t;
    while(t--){
        b = 1;
        cin >> n;
        st.clear();
        for(int i = 0; i < n; i++){
            cin >> tmp;
            st.insert(tmp);
        }
        cin >> m;
        getline(cin, tmp);
        for(int i = 0; i < m; i++){
            getline(cin, a[i]);
            if(a[i][0] == '?'){
                msg[i] = split(a[i].substr(a[i].find(':') + 1), st);
                msg[i].erase(tmp);
                if(msg[i].size() == 1)a[i] = *msg[i].begin() + a[i].substr(1);
            }
            tmp = a[i].substr(0, a[i].find(':'));
        }
        tmp = "";
        for(int i = m - 1; i > -1; i--){
            if(a[i][0] == '?'){
                msg[i].erase(tmp);
                if(msg[i].size() == 1)a[i] = *msg[i].begin() + a[i].substr(1);
                else{
                    cout << "Impossible\n";
                    b = 0;
                    break;
                }
            }
            tmp2 = a[i].substr(0, a[i].find(':'));
            if(tmp2 == tmp){
                cout << "Impossible\n";
                b = 0;
                break;
            }
            tmp = tmp2;
        }
        if(b){
            for(int i = 0; i < m; i++)cout << a[i] << "\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

using namespace __cxx11;

const string nu = ".,?! ";

set<string> split(string s, set<string> q){
    int x = 0;
    for(int i = 0; i < s.size(); i++){
        if(nu.find(s[i]) != string::npos){
            q.erase(s.substr(x, i - x));
            x = i + 1;
        }
    }
    if(x < s.size())q.erase(s.substr(x));
    return q;
}

int main()
{
    bool b;
    int t, n, m;
    string tmp, tmp2;
    string a[100];
    set<string> msg[100];
    set<string> st;
    cin >> t;
    while(t--){
        b = 1;
        cin >> n;
        st.clear();
        for(int i = 0; i < n; i++){
            cin >> tmp;
            st.insert(tmp);
        }
        cin >> m;
        getline(cin, tmp);
        for(int i = 0; i < m; i++){
            getline(cin, a[i]);
            if(a[i][0] == '?'){
                msg[i] = split(a[i].substr(a[i].find(':') + 1), st);
                msg[i].erase(tmp);
                if(msg[i].size() == 1)a[i] = *msg[i].begin() + a[i].substr(1);
            }
            tmp = a[i].substr(0, a[i].find(':'));
        }
        tmp = "";
        for(int i = m - 1; i > -1; i--){
            if(a[i][0] == '?'){
                msg[i].erase(tmp);
                a[i] = *msg[i].begin() + a[i].substr(1);
            }
            tmp2 = a[i].substr(0, a[i].find(':'));
            if(tmp2 == tmp){
                cout << "Impossible\n";
                b = 0;
                break;
            }
            tmp = tmp2;
        }
        if(b){
            for(int i = 0; i < m; i++)cout << a[i] << "\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

using namespace __cxx11;

const string nu = ".,?! ";

set<string> split(string s, set<string> q){
    int x = 0;
    for(int i = 0; i < s.size(); i++){
        if(nu.find(s[i]) != string::npos){
            q.erase(s.substr(x, i - x));
            x = i + 1;
        }
    }
    if(x < s.size())q.erase(s.substr(x));
    return q;
}

int main()
{
    bool b;
    int t, n, m;
    string tmp, tmp2;
    string a[100];
    set<string> msg[100];
    set<string> st;
    cin >> t;
    while(t--){
        b = 1;
        cin >> n;
        st.clear();
        for(int i = 0; i < n; i++){
            cin >> tmp;
            st.insert(tmp);
        }
        cin >> m;
        getline(cin, tmp);
        for(int i = 0; i < m; i++){
            getline(cin, a[i]);
            if(a[i][0] == '?'){
                msg[i] = split(a[i].substr(a[i].find(':') + 2), st);
                msg[i].erase(tmp);
                if(msg[i].size() == 1)a[i] = *msg[i].begin() + a[i].substr(1);
            }
            tmp = a[i].substr(0, a[i].find(':'));
        }
        tmp = "";
        for(int i = m - 1; i > -1; i--){
            if(a[i][0] == '?'){
                msg[i].erase(tmp2);
                if(msg[i].size() == 1)a[i] = *msg[i].begin() + a[i].substr(1);
                else{
                    cout << "Impossible\n";
                    b = 0;
                    break;
                }
            }
            tmp2 = a[i].substr(0, a[i].find(':'));
            if(tmp2 == tmp){
                cout << "Impossible\n";
                b = 0;
                break;
            }
            tmp = tmp2;
        }
        if(b){
            for(int i = 0; i < m; i++)cout << a[i] << "\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
int dp[105][105];
string kill = "Impossible";
set<int>got[105];
vector<string>V;
string ro[105] ={};
map<string,int>M;
int m;
int n;
int solve(int idx , int pr){
    if(idx == m)
        return dp[idx][pr]=1;
    int &ret = dp[idx][pr];
    if(ret != -1)
        return 0;
    ret = 0;
    if(ro[idx][0] == '?'){
        for(int i = 0 ;i<n;i++){
            if(i!=pr && got[idx].find(i) == got[idx].end()){
                ret|=solve(idx+1, i);
            }
        }
    }
    else{
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        if(M[cur] - 1 == pr)
            return ret = 0;
        ret = solve(idx+1,M[cur] - 1);
    }
    return ret;
}
void build(int idx, int pr){
    if(idx == m)
        return;
    if(ro[idx][0] == '?'){
        ro[idx].erase(ro[idx].begin());
        for(int i = 0 ;i < n ;i++){
            if(dp[idx+1][i] == 1){
                cout << V[i]<<ro[idx]<<'\n';
                build(idx+1,i);
                break;
            }
        }
    }
    else{
        cout <<ro[idx]<< '\n';
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        build(idx+1, M[cur] - 1);
    }

}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        memset(dp, -1, sizeof(dp));
        cin >> n;
        V.clear();
        V.resize(n);
        M.clear();
        for(int i = 0 ;i<100;i++)
            got[i].clear();
        for(int i = 0 ; i<n;i++)
            cin >> V[i],M[V[i]] = i + 1;
        cin >> m;
        cin.ignore();
        set<int>Sav[105] = {};
        for(int i = 0 ; i<m;i++){
            getline(cin,ro[i]);
            string tmp = "";
            for(int j = 1; j < ro[i].size() ; j++){
                if(isalpha(ro[i][j])||isdigit(ro[i][j]))
                    tmp.push_back(ro[i][j]);
                else{
                    if(M[tmp]){
                        got[i].insert(M[tmp] - 1);
                    }
                    tmp.clear();
                }
                if(j+1==ro[i].size()){
                    if(M[tmp]){
                        got[i].insert(M[tmp] - 1);
                    }
                    tmp.clear();
                }

            }
        }
        int tru = solve(0, 104);
        if(tru == 0){
            cout << kill << '\n';
        }
        else{
            build(0,104);
        }

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
int dp[105][105];
string kill = "Impossible";
set<int>got[105];
vector<string>V;
string ro[105] ={};
map<string,int>M;
int m;
int n;
int solve(int idx , int pr){
    if(idx == m)
        return dp[idx][pr]=1;
    int &ret = dp[idx][pr];
    if(ret != -1)
        return 0;
    ret = 0;
    if(ro[idx][0] == '?'){
        for(int i = 0 ;i<n;i++){
            if(i!=pr && got[idx].find(i) == got[idx].end()){
                ret|=solve(idx+1, i);
            }
        }
    }
    else{
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        if(M[cur] - 1 == pr)
            return ret = 0;
        ret = solve(idx+1,M[cur] - 1);
    }
    return ret;
}
void build(int idx, int pr){
    if(idx == m)
        return;
    if(ro[idx][0] == '?'){
        ro[idx].erase(ro[idx].begin());
        for(int i = 0 ;i < n ;i++){
            if(i == pr || got[idx].find(i)!=got[idx].end())
                continue;
            if(dp[idx+1][i] == 1){
                cout << V[i]<<ro[idx]<<'\n';
                build(idx+1,i);
                break;
            }
        }
    }
    else{
        cout <<ro[idx]<< '\n';
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        build(idx+1, M[cur] - 1);
    }

}
int main(){
    int t;
    cin >> t;
    while(t--){
        memset(dp, -1, sizeof(dp));
        cin >> n;
        V.clear();
        V.resize(n);
        M.clear();
        for(int i = 0 ;i<100;i++)
            got[i].clear();
        for(int i = 0 ; i<n;i++)
            cin >> V[i],M[V[i]] = i + 1;
        cin >> m;
        cin.ignore();
        for(int i = 0 ; i<m;i++){
            getline(cin,ro[i]);
            for(int j =1;j<=10;j++){
                bool flag = false;
                for(int k = 0;k+j <=ro[i].size();k++){
                    if(!flag){
                        if(ro[i][k] == ':')
                            flag = true;
                        continue;
                    }
                    if(M[ro[i].substr(k,j)]){
                        if(((k+j<ro[i].size()) && isalpha(ro[i][k+j])||isdigit(ro[i][k+j]))||((k) && isalpha(ro[i][k-1])||isdigit(ro[i][k-1])))
                            continue;
                        got[i].insert(M[ro[i].substr(k,j)] - 1);
                    }
                }
            }
        }
        int tru = solve(0, 104);
        if(tru == 0){
            cout << kill << '\n';
        }
        else{
            build(0,104);
        }

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
int dp[105][105];
string kill = "Impossible";
set<int>got[105];
vector<string>V;
string ro[105] ={};
map<string,int>M;
int m;
int n;
int solve(int idx , int pr){
    if(idx == m)
        return dp[idx][pr]=1;
    int &ret = dp[idx][pr];
    if(ret != -1)
        return 0;
    ret = 0;
    if(ro[idx][0] == '?'){
        for(int i = 0 ;i<n;i++){
            if(i!=pr && got[idx].find(i) == got[idx].end()){
                ret|=solve(idx+1, i);
            }
        }
    }
    else{
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        if(M[cur] - 1 == pr)
            return ret = 0;
        ret = solve(idx+1,M[cur] - 1);
    }
    return ret;
}
void build(int idx, int pr){
    if(idx == m)
        return;
    if(ro[idx][0] == '?'){
        ro[idx].erase(ro[idx].begin());
        for(int i = 0 ;i < n ;i++){
            if(i == pr || got[idx].find(i)!=got[idx].end())
                continue;
            if(dp[idx+1][i] == 1){
                cout << V[i]<<ro[idx]<<'\n';
                build(idx+1,i);
                break;
            }
        }
    }
    else{
        cout <<ro[idx]<< '\n';
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        build(idx+1, M[cur] - 1);
    }

}
int main(){
    int t;
    cin >> t;
    while(t--){
        memset(dp, -1, sizeof(dp));
        cin >> n;
        V.clear();
        V.resize(n);
        M.clear();
        for(int i = 0 ;i<100;i++)
            got[i].clear();
        for(int i = 0 ; i<n;i++)
            cin >> V[i],M[V[i]] = i + 1;
        cin >> m;
        cin.ignore();
        for(int i = 0 ; i<m;i++){
            getline(cin,ro[i]);
            for(int j =1;j<=10;j++){
                bool flag = false;
                for(int k = x;k+j <=ro[i].size();k++){
                    if(!flag){
                        if(ro[i][k] == ':')
                            flag = true;
                        continue;
                    }
                    if(M[ro[i].substr(k,j)])
                        got[i].insert(M[ro[i].substr(k,j)] - 1);
                }
            }
        }
        int tru = solve(0, 104);
        if(tru == 0){
            cout << kill << '\n';
        }
        else{
            build(0,104);
        }

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
int dp[105][105];
string kill = "Impossible";
set<int>got[105];
vector<string>V;
string ro[105] ={};
map<string,int>M;
int m;
int n;
int solve(int idx , int pr){
    if(idx == m)
        return dp[idx][pr]=1;
    int &ret = dp[idx][pr];
    if(ret != -1)
        return 0;
    ret = 0;
    if(ro[idx][0] == '?'){
        for(int i = 0 ;i<n;i++){
            if(i!=pr && got[idx].find(i) == got[idx].end()){
                ret|=solve(idx+1, i);
            }
        }
    }
    else{
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        if(M[cur] - 1 == pr)
            return ret = 0;
        ret = solve(idx+1,M[cur] - 1);
    }
    return ret;
}
void build(int idx, int pr){
    if(idx == m)
        return;
    if(ro[idx][0] == '?'){
        ro[idx].erase(ro[idx].begin());
        for(int i = 0 ;i < n ;i++){
            if(i == pr || got[idx].find(i)!=got[idx].end())
                continue;
            if(dp[idx+1][i] == 1){
                cout << V[i]<<ro[idx]<<'\n';
                build(idx+1,i);
                break;
            }
        }
    }
    else{
        cout <<ro[idx]<< '\n';
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        build(idx+1, M[cur] - 1);
    }

}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        memset(dp, -1, sizeof(dp));
        cin >> n;
        V.clear();
        V.resize(n);
        M.clear();
        for(int i = 0 ;i<100;i++)
            got[i].clear();
        for(int i = 0 ; i<n;i++)
            cin >> V[i],M[V[i]] = i + 1;
        cin >> m;
        cin.ignore();
        for(int i = 0 ; i<m;i++){
            getline(cin,ro[i]);
            string tmp = "";
            for(int j =1;j<=10;j++){
                int x = ro[i].find(':') + 1;
                for(int k = x;k+j <=ro[i].size();k++){
                    if(M[ro[i].substr(k,j)])
                        got[i].insert(M[ro[i].substr(k,j)] - 1);

                }
            }
        }
        int tru = solve(0, 104);
        if(tru == 0){
            cout << kill << '\n';
        }
        else{
            build(0,104);
        }

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
int dp[105][105];
string kill = "Impossible";
set<int>got[105];
vector<string>V;
string ro[105] ={};
map<string,int>M;
int m;
int n;
int solve(int idx , int pr){
    if(idx == m)
        return dp[idx][pr]=1;
    int &ret = dp[idx][pr];
    if(ret != -1)
        return 0;
    ret = 0;
    if(ro[idx][0] == '?'){
        for(int i = 0 ;i<n;i++){
            if(i!=pr && got[idx].find(i) == got[idx].end()){
                ret|=solve(idx+1, i);
            }
        }
    }
    else{
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        if(M[cur] - 1 == pr)
            return ret = 0;
        ret = solve(idx+1,M[cur] - 1);
    }
    return ret;
}
void build(int idx, int pr){
    if(idx == m)
        return;
    if(ro[idx][0] == '?'){
        ro[idx].erase(ro[idx].begin());
        for(int i = 0 ;i < n ;i++){
            if(i == pr || got[idx].find(i)!=got[idx].end())
                continue;
            if(dp[idx+1][i] == 1){
                cout << V[i]<<ro[idx]<<'\n';
                build(idx+1,i);
                break;
            }
        }
    }
    else{
        cout <<ro[idx]<< '\n';
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        build(idx+1, M[cur] - 1);
    }

}
int main(){
    int t;
    cin >> t;
    while(t--){
        memset(dp, -1, sizeof(dp));
        cin >> n;
        V.clear();
        V.resize(n);
        M.clear();
        for(int i = 0 ;i<100;i++)
            got[i].clear();
        for(int i = 0 ; i<n;i++)
            cin >> V[i],M[V[i]] = i + 1;
        cin >> m;
        cin.ignore();
        for(int i = 0 ; i<m;i++){
            getline(cin,ro[i]);
            for(int j =1;j<=10;j++){
                bool flag = false;
                for(int k = 0;k+j <=ro[i].size();k++){
                    if(!flag){
                        if(ro[i][k] == ':')
                            flag = true;
                        continue;
                    }
                    if(M[ro[i].substr(k,j)]&&(k+j!=ro[i].size() && !isdigit(ro[i][k+1])))
                        got[i].insert(M[ro[i].substr(k,j)] - 1);
                }
            }
        }
        int tru = solve(0, 104);
        if(tru == 0){
            cout << kill << '\n';
        }
        else{
            build(0,104);
        }

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
int dp[105][105];
string kill = "Impossible";
set<int>got[105];
vector<string>V;
string ro[105] ={};
map<string,int>M;
int m;
int n;
int solve(int idx , int pr){
    if(idx == m)
        return dp[idx][pr]=1;
    int &ret = dp[idx][pr];
    if(ret != -1)
        return 0;
    ret = 0;
    if(ro[idx][0] == '?'){
        for(int i = 0 ;i<n;i++){
            if(i!=pr && got[idx].find(i) == got[idx].end()){
                ret|=solve(idx+1, i);
            }
        }
    }
    else{
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        if(M[cur] - 1 == pr)
            return ret = 0;
        ret = solve(idx+1,M[cur] - 1);
    }
    return ret;
}
void build(int idx, int pr){
    if(idx == m)
        return;
    if(ro[idx][0] == '?'){
        ro[idx].erase(ro[idx].begin());
        for(int i = 0 ;i < n ;i++){
            if(i == pr || got[idx].find(i)!=got[idx].end())
                continue;
            if(dp[idx+1][i] == 1){
                cout << V[i]<<ro[idx]<<'\n';
                build(idx+1,i);
                break;
            }
        }
    }
    else{
        cout <<ro[idx]<< '\n';
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        build(idx+1, M[cur] - 1);
    }

}
int main(){
    int t;
    cin >> t;
    while(t--){
        memset(dp, -1, sizeof(dp));
        cin >> n;
        V.clear();
        V.resize(n);
        M.clear();
        for(int i = 0 ;i<100;i++)
            got[i].clear();
        for(int i = 0 ; i<n;i++)
            cin >> V[i],M[V[i]] = i + 1;
        cin >> m;
        cin.ignore();
        for(int i = 0 ; i<m;i++){
            getline(cin,ro[i]);
            for(int j =1;j<=10;j++){
                int x = ro[i].find(':') + 1;
                for(int k = x;k+j <=ro[i].size();k++){
                    if(M[ro[i].substr(k,j)])
                        got[i].insert(M[ro[i].substr(k,j)] - 1);
                }
            }
        }
        int tru = solve(0, 104);
        if(tru == 0){
            cout << kill << '\n';
        }
        else{
            build(0,104);
        }

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
int dp[105][105];
string kill = "Impossible";
set<int>got[105];
vector<string>V;
string ro[105] ={};
map<string,int>M;
int m;
int n;
int solve(int idx , int pr){
    if(idx == m)
        return dp[idx][pr]=1;
    int &ret = dp[idx][pr];
    if(ret != -1)
        return 0;
    ret = 0;
    if(ro[idx][0] == '?'){
        for(int i = 0 ;i<n;i++){
            if(i!=pr && got[idx].find(i) == got[idx].end()){
                ret|=solve(idx+1, i);
            }
        }
    }
    else{
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        if(M[cur] - 1 == pr)
            return ret = 0;
        ret = solve(idx+1,M[cur] - 1);
    }
    return ret;
}
void build(int idx, int pr){
    if(idx == m)
        return;
    if(ro[idx][0] == '?'){
        ro[idx].erase(ro[idx].begin());
        for(int i = 0 ;i < n ;i++){
            if(i == pr || got[idx].find(i)!=got[idx].end())
                continue;
            if(dp[idx+1][i] == 1){
                cout << V[i]<<ro[idx]<<'\n';
                build(idx+1,i);
                break;
            }
        }
    }
    else{
        cout <<ro[idx]<< '\n';
        string cur = "";
        for(int i=0;;i++){
            if(ro[idx][i] == ':')
                break;
            cur.push_back(ro[idx][i]);
        }
        build(idx+1, M[cur] - 1);
    }

}
int main(){
    int t;
    cin >> t;
    while(t--){
        memset(dp, -1, sizeof(dp));
        cin >> n;
        V.clear();
        V.resize(n);
        M.clear();
        for(int i = 0 ;i<100;i++)
            got[i].clear();
        for(int i = 0 ; i<n;i++)
            cin >> V[i],M[V[i]] = i + 1;
        cin >> m;
        cin.ignore();
        for(int i = 0 ; i<m;i++){
            getline(cin,ro[i]);
            for(int j =1;j<=10;j++){
                bool flag = false;
                for(int k = 0;k+j <=ro[i].size();k++){
                    if(!flag){
                        if(ro[i][k] == ':')
                            flag = true;
                        continue;
                    }
                    if(M[ro[i].substr(k,j)]){
                        if(isalpha(ro[i][k+j])||isdigit(ro[i][k+j]))
                            continue;
                        got[i].insert(M[ro[i].substr(k,j)] - 1);
                    }
                }
            }
        }
        int tru = solve(0, 104);
        if(tru == 0){
            cout << kill << '\n';
        }
        else{
            build(0,104);
        }

    }

    return 0;
}