#include <bits/stdc++.h>
using namespace std;
int main() {
vector <string> card(14);
// 輸入牌面順序
for (int i = 0; i < 14; i++) {
cin >> card[i];
}
int n;
cin >> n;
// 洗牌
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
swap(card[a], card[b]);
}
// 紀錄每位學生拿到哪張編號的牌
vector <int> classmates(14);
for (int i = 0; i < 14; i++) {
cin >> classmates[i];
}
// 輸出最後洗牌順序
for (const string& str : card) {
cout << str << " ";
}
cout << endl;
// 從洗完牌的順序中,找 Joker 是哪一張牌
int JokerNum;
for (int i = 0; i < 14; i++) {
if (card[i] == "Joker") { // 0號牌
JokerNum = i;
break;
}
}
// 從每位學生拿到的牌中,比對誰拿到 Joker
for (int i = 0; i < 14; i++) {
if (classmates[i] == JokerNum) {
cout << i+1 << endl;
}
}
return 0;
}