#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int main() {
int N;
cin >> N;
char a, b;
cin >> a >> b;
int bin_nums[1005];
int dec_nums[1005] = { 0 };
for (int i = 0;i < N;i++) {
cin >> bin_nums[i];
int j = 0, rem;
while (bin_nums[i] != 0) {
rem = bin_nums[i] % 10;
bin_nums[i] /= 10;
dec_nums[i] += rem * pow(2, j);
++j;
}
}
char table_lets[1005];
for (int i = 0;i < N;i++) {
cin >> table_lets[i];
}
if (a == 'A' && b == 'A') {
sort(dec_nums, dec_nums + N);
sort(table_lets, table_lets + N);
for (int i = 0;i < N;i++) { cout << table_lets[i] << dec_nums[i]<< " " << endl; }
}
else if (a == 'A' && b == 'D') {
sort(dec_nums, dec_nums + N);
reverse(table_lets, table_lets + N);
for (int i = 0;i < N;i++) { cout << table_lets[i] << dec_nums[i] << " " << endl; }
}
else if (a == 'D' && b == 'D') {
reverse(dec_nums, dec_nums + N);
reverse(table_lets, table_lets + N);
for (int i = 0;i < N;i++) { cout << table_lets[i] << dec_nums[i] << " " << endl; }
}
else if (a == 'D' && b == 'A') {
reverse(dec_nums, dec_nums + N);
sort(table_lets, table_lets + N);
for (int i = 0;i < N;i++) { cout << table_lets[i] << dec_nums[i] << " " << endl; }
}
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPGFsZ29yaXRobT4KI2luY2x1ZGU8bWF0aC5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CgoJaW50IE47CgljaW4gPj4gTjsKCgljaGFyIGEsIGI7CgljaW4gPj4gYSA+PiBiOwoKCWludCBiaW5fbnVtc1sxMDA1XTsKCWludCBkZWNfbnVtc1sxMDA1XSA9IHsgMCB9OwoKCWZvciAoaW50IGkgPSAwO2kgPCBOO2krKykgewoJCWNpbiA+PiBiaW5fbnVtc1tpXTsKCgkJaW50IGogPSAwLCByZW07CgkJd2hpbGUgKGJpbl9udW1zW2ldICE9IDApIHsKCQkJcmVtID0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