1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def encode_seq(val, n=7, x=224):
    A = [[0]*(n+1) for _ in range(x+1)]
    A[0] = [1]*(n+1)
    for i in range(x+1):
        A[i][0] = 1
    #A[:, 0] = 1
    for j in range(1, n+1):
        for i in range(1, x+1):
            A[i][j] = A[i-1][j] + A[i][j-1]
    result = []
    for n_i in range(n,0, -1):
        for x_i in range(x+1):
            if A[x_i][n_i] > val:
                break
        if x_i != 0:
            val -= A[x_i-1][ n_i]
        result.append(x_i)
    return result
    

def decode_seq(seq, n=7, x=224):
    A = [[0]*(n+1) for _ in range(x+1)]
    A[0] = [1]*(n+1)
    for i in range(x+1):
        A[i][0] = 1
    #A[:, 0] = 1
    for j in range(1, n+1):
        for i in range(1, x+1):
            A[i][j] = A[i-1][j] + A[i][j-1]
            
    position = 0
    for n_i in range(0, n):
        if seq[n_i] == 0:
            break
        position += A[seq[n_i]-1][n - n_i]
    return position
    
def is_5pivot(u):
    return u[0] == 0 and u[3] == 1 and u[1] != u[2] and u[8] != u[9] and sum(u[4:8]) == 2


def encode_matrix(val):
    count = 0
    mapping_to_01 = dict()
    mapping_from_01 = dict()
    for u in range(2**10):
        u = tuple(map(int, bin(u)[2:].zfill(10)))
        if sum(u)!= 6 and not is_5pivot(u):
            mapping_to_01[count] = u
            mapping_from_01[u] = count
            count+=1
    matrix = [mapping_to_01[row] for row in encode_seq(val, n=6, x=count-1)] + \
    [(0,0,0,0,1,1,1,1,1,1),
     (0,0,1,1,0,0,1,1,1,1),
     (0,1,0,1,1,1,0,0,1,1),
     (0,0,1,1,0,1,0,1,0,1)]
    return matrix

def argsort(arr):
    return [pos for val,pos in sorted(list(zip(arr, range(len(arr)))))]

def reorder_columns(A, order):
    return [[row[i] for i in order] for row in A]

def decode_matrix(C):
    count = 0
    mapping_to_01 = dict()
    mapping_from_01 = dict()
    for u in range(2**10):
        u = tuple(map(int, bin(u)[2:].zfill(10)))
        if sum(u)!= 6 and not is_5pivot(u):
            mapping_to_01[count] = u
            mapping_from_01[u] = count
            count+=1
    
    P = [row for row in C if sum(row)==6]
    P0 = reorder_columns(P,argsort([x+y+z for x,y,z in zip(P[0],P[1],P[2])]))
    first_row = [row[1]+row[2] for row in P0].index(0)
    P[first_row], P[0] = P[0], P[first_row]
    #P[[first_row,0]] = P[[0,first_row]]
    C1 = reorder_columns(C, argsort([4*x+y+z for x,y,z in zip(P[0],P[1],P[2])]))
    pos_5pivot = 0
    pos_6pivots = []
    for i, x in enumerate(C1):#[P.sum(axis=1)==5]:
        if sum(x) == 6:
            pos_6pivots.append(i)
        if sum(x)==5:
            if is_5pivot(x):
                pos_5pivot = i
    P = [C1[pos] for pos in pos_6pivots + [pos_5pivot]]
    P[first_row], P[0] = P[0], P[first_row]
    if (P[3][1] == 1 and P[2][1] == 1) or (P[3][2] == 1 and P[2][2] == 1):
        P[[1,2]] = P[[2,1]]
    C2 = reorder_columns(C1, argsort([8*x+4*y+2*z+t for x,y,z,t in zip(P[0],P[1],P[2],P[3])]))
    C2 = [C2[x] for x in range(10) if (x not in pos_6pivots) and x!=pos_5pivot ]
    number_seq = tuple(sorted([mapping_from_01[tuple(y)] for y in C2], key=lambda x:-x))
    return decode_seq(list(number_seq), n=6, x=count-1)


side = input("").strip()
upper_bound, cases = map(int, input("").split())

if side == "Algosia":
    for k in range(cases):
        num = int(input("").strip())
        matrix = encode_matrix(num)
        for row in matrix:
            print("".join(map(str, row)), flush=True)
        #sys.stdout.flush()
else:
    for _ in range(cases):
        matrix = []
        for i in range(10):
            matrix.append(list(map(int, input("").strip())))
        print(decode_matrix(matrix), flush=True)
        #sys.stdout.flush()