#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 1024 + 5;
const int maxM = 1;
int n;
string code[maxN];
struct hufmtree{
double val;
char data;
int lsn, rsn, root;
int id;
hufmtree(double a = 0, char b = NULL, int c = -1, int d = -1, int e = -1, int f = 0) : val(a), data(b), lsn(c), rsn(d), root(e), id(f) {}
friend bool operator < (hufmtree h1, hufmtree h2) { return h1.val < h2.val; }
}tree[maxN];
multiset<hufmtree>st;
void In()
{
scanf("%d", &n);
for(int i = 0; i < n; i ++ )
{
double dis; scanf("%lf", &dis);
getchar();
char da; scanf("%c", &da);
tree[i] = hufmtree(dis, da, -1, -1, -1, i);
st.insert(tree[i]);
}
}
void creat_hufm()
{
int cnt = 0;
while(!st.empty())
{
hufmtree min_ = *st.begin();
st.erase(st.begin());
hufmtree max_ = *st.begin();
st.erase(st.begin());
tree[n + cnt] = hufmtree(min_.val + max_.val, NULL, min_.id, max_.id, -1, n + cnt);
if(tree[n + cnt].val < 1)
st.insert(tree[n + cnt]);
tree[min_.id].root = n + cnt;
tree[max_.id].root = n + cnt;
cnt ++;
}
}
void HfumCode()
{
for(int i = 0; i < n; i ++)
{
int now = i;
for(int pos = tree[i].root; ~pos ; pos = tree[pos].root)
{
if(tree[pos].lsn == now)
code[i] += '0';
else
code[i] += '1';
now = pos;
}
reverse(code[i].begin(), code[i].end());
printf("%c:", tree[i].data);
cout << code[i] << endl;
}
}
void TransCode(string ts)
{
int m = 2 * n - 1;
int tmp = m - 1;
int pos = 0;
while(ts[pos] != '\0')
{
if(ts[pos] == '0')
tmp = tree[tmp].lsn;
else if(ts[pos] == '1')
tmp = tree[tmp].rsn;
if(tree[tmp].lsn == -1)
{
putchar(tree[tmp].data);
tmp = m - 1;
}
pos ++;
}
if(tree[tmp].lsn != -1 && tmp != m - 1)
printf("\nERROR\n");
else
printf("\n");
}
void pre(int u)
{
printf("%d ", u);
if(tree[u].lsn != -1) pre(tree[u].lsn);
if(tree[u].rsn != -1) pre(tree[u].rsn);
}
void mid(int u)
{
if(tree[u].lsn != -1) mid(tree[u].lsn);
printf("%d ", u);
if(tree[u].rsn != -1) mid(tree[u].rsn);
}
void last(int u)
{
if(tree[u].lsn != -1) last(tree[u].lsn);
if(tree[u].rsn != -1) last(tree[u].rsn);
printf("%d ", u);
}
int main()
{
In();
creat_hufm();
HfumCode();
string ts; cin >> ts;
TransCode(ts);
int rt = 2 * n - 2;
pre(rt); putchar('\n');
mid(rt); putchar('\n');
last(rt); putchar('\n');
return 0;
}
CBEAF