char str[20], c[10][5]; int n, s; map<string, int> m;
stringNext(string x){ string res = x; for (int i = 0; i < 8; ++i) { for (int j = 0; j < n; ++j) { bool ok = true; for (int k = 0; k < 3; ++k) { int p = k == 0 ? -2 : -1; p = (n + j + k + p) % n; if (x[p] != c[i][k]) { ok = false; break; } } if (ok) { res[j] = c[i][3]; } } } return res; }
intmain(){ while (~scanf("%d", &n)) { m.clear(); scanf("%s", str); for (int i = 0; i < 8; ++i) { scanf("%s", c[i]); } scanf("%d", &s); string tmp = str; m[tmp] = 0; int cnt = 0, cnt2 = 0; for (;;) { ++cnt; tmp = Next(tmp); if (m.count(tmp)) { cnt2 = m[tmp]; break; } m[tmp] = cnt; } if (s >= cnt) { s -= cnt; s %= (cnt - cnt2); } else { tmp = str; } while (s--) { tmp = Next(tmp); } string ans = tmp; tmp += tmp; for (int i = 0; i < n; ++i) { if (tmp.substr(i, n) < ans) { ans = tmp.substr(i, n); } } puts(ans.c_str()); } return0; }