Under construction

Still being built. Things may change

Digits on the Scoreboard

Time limit1.5sMemory limit512 MB

Summary
Given a 7-row scoreboard of seven-segment digits, output the scoreboard for the next lexicographic permutation of its digits, or print The End if none exists.
Level

Medium4 of 10

Topics
Implementation, String, Sorting
Solved
No attempts yet

Problem

Shinhyeong's school cafeteria has NN distinct digits written in front of it, and a rectangular scoreboard that is 7 cells tall and 6×N6 \times N cells wide. Each cell holds one light bulb. Each digit is shown with a block of 7 rows by 6 columns of bulbs. The figure below shows a scoreboard with the digits 0 through 9 in order. A black cell is a lit bulb, and an empty cell is an unlit bulb. We assume there is no other way to show the same digit.

Reading the digits on the scoreboard from left to right gives a permutation PP. Shinhyeong wondered which permutation comes right after PP in lexicographic order among the permutations that can be made by reordering the elements of PP. He also wanted to know what the scoreboard looks like for that permutation.

For example, reading the digits in the figure above gives {1, 4, 2}. Listing the permutations of these digits in lexicographic order gives the following.

{1, 2, 4}, {1, 4, 2}, {2, 1, 4}, {2, 4, 1}, {4, 1, 2}, {4, 2, 1}

The permutation after {1, 4, 2} is {2, 1, 4}, so the one Shinhyeong wants is {2, 1, 4}. The scoreboard for it is shown above. Given the scoreboard in front of the cafeteria, write a program that prints the scoreboard for the permutation Shinhyeong is curious about.

Input

The scoreboard is given on 7 lines, each with 6×N6 \times N characters, for NN digits. A 1 means a lit bulb, and a 0 means an unlit bulb. NN satisfies 1≤N≤101 \le N \le 10.

Output

Given the scoreboard in front of the cafeteria, print the scoreboard for the next permutation in lexicographic order after the permutations of its digits. If there is no next permutation, print The End.

Examples2

  1. Example 1

    Input
    000000000000000000
    000100000100011110
    001100001100000010
    000100010100011110
    000100111110010000
    000100000100011110
    000000000000000000
    
    Expected output
    000000000000000000
    011110000100000100
    000010001100001100
    011110000100010100
    010000000100111110
    011110000100000100
    000000000000000000
    
  2. Example 2

    Input
    000000
    011100
    000010
    000100
    000010
    011100
    000000
    
    Expected output
    The End