This page is still under construction.

Parts of this page are still being built. What you see may change.

Word

Time limit1sMemory limit128 MB

Summary
Apply a cyclic cellular rewriting rule s times to a binary word of length n, then print the lexicographically smallest rotation.
Level

Medium7 of 10

Topics
String, Simulation, Math, Brute force
Solved
No attempts yet

Problem

Dr. Wright's class is studying a modified L-system. Here are the details you need.

Consider words of length nn over the two-letter alphabet {a,b}\{a, b\}. Each word is cyclic: it can be written in any of its nn cyclic-shift forms, and the first and last letters are treated as neighbours.

A rewriting rule replaces the letter at a position ii based on the letters at positions i−2i-2, ii, and i+1i+1 (indices are taken cyclically). In one step, all letters of the word are rewritten simultaneously.

Given a starting word and a set of rewriting rules, determine how the word looks after ss rewriting steps.

Input

The input contains several blocks, each describing one system.

  • The first line holds an integer nn with 2<n<162 < n < 16, the length of the word.
  • The second line holds the starting word, made up only of the lowercase letters a and b.
  • Each of the next eight lines holds four characters c1c2c3c4c_1 c_2 c_3 c_4 describing one rewriting rule: if the letter at position i−2i-2 is c1c_1, the letter at position ii is c2c_2, and the letter at position i+1i+1 is c3c_3, then after rewriting the letter at position ii becomes c4c_4. The eight rules are correct and complete (they cover every combination of c1c2c3c_1 c_2 c_3).
  • The last line of the block holds an integer ss with 0≤s≤20000000000 \le s \le 2000000000.

Process blocks until the end of input.

Output

For each block, print one line containing the word obtained after ss rewriting steps. Because the word is cyclic it can be written in any of its nn shifted forms; print the lexicographically smallest such form, assuming a < b.

Examples2

  1. Example 1

    Input
    5
    aaaaa
    aaab
    aabb
    abab
    abbb
    baab
    babb
    bbab
    bbbb
    1
    
    Expected output
    bbbbb
    
  2. Example 2

    Input
    6
    baaaab
    aaaa
    aaba
    abab
    abbb
    baaa
    baba
    bbab
    bbbb
    0
    
    Expected output
    aaaabb