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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;

bool algosia = false;
int pt[6] = {2, 3, 4, 6, 7, 8};

vector<LL> fact;

LL po(int b, int a) {
  LL res = 1;
  for (int i=a+1; i<=b; ++i) {
    res *= i;
    res /= i-a;
  }
  return res;
}

void A() {
  LL k; scanf(" %lld", &k);

  vector< vector<int> > m;
  m.push_back({1, 1, 1, 0 ,0, 1, 0, 0, 1, 0});
  m.push_back({1, 1, 0, 1, 1, 0, 0, 1, 0, 0});
  m.push_back({1, 1, 1, 1, 0, 0, 1, 0, 0, 0});
  m.push_back({1, 0, 1, 0, 1, 1, 1, 0, 0, 0});


  for (int pi=0; pi<6; ++pi) {
    m.emplace_back(10, 0);

    int p = pt[pi];
    int q = po(10, p);

    int l = k % q;
    k /= q;
    int s=0;
    for (int i=0; i<p; ++i) {
      for (; s<10; ++s) {
        int r = po(10-s-1, p-i-1);
        if (l < r) {
          m[4+pi][s] = 1;
          ++s;
          break;
        } else {
          l -= r;
        }
      }
    }
  }

  for (int i=0; i<10; ++i) {
    for (int j=0; j<10; ++j) printf("%d", m[i][j]);
    printf("\n");
  }
}

void swapCol(vector< vector<int> >&m, int i, int j) {
  for (int k=0; k<10; ++k) {
    swap(m[k][i], m[k][j]);
  }
}

void dprint(vector< vector<int> >& t, string s) {/*
  printf("%s\n", s.c_str());
  for (int i=0; i<10; ++i) {
    for (int j=0; j<10; ++j) printf("%d", t[i][j]);
    printf("\n");
  }*/
}

void B() {
  char s[12];
  vector< vector<int> > m(10, vector<int>(10));
  for (int i=0; i<10; ++i) { 
    scanf(" %s", s);
    for (int j=0; j<10; ++j) {
      m[i][j] = s[j] - '0';
    }
  }

  {
    vector<int> cnt(10, 0);
    for (int i=0; i<10; ++i) for (int j=0; j<10; ++j) cnt[i] += m[i][j];
    int k = 0;
    for (int i=0; i<10; ++i) if (cnt[i] == 5) {
      swap(m[i], m[k]);
      swap(cnt[i], cnt[k]);
      ++k;
    }
    for (int i=4; i<10; ++i) for (int j=i+1; j<10; ++j) if (cnt[j] < cnt[i]) {
      swap(m[i], m[j]);
      swap(cnt[i], cnt[j]);
    }
    
    vector<int> sums(10, 0);
    for (int i=0; i<10; ++i) {
      for (int j=0; j<4; ++j) sums[i] += m[j][i];
    }
    vector< vector<int> > occ(4, vector<int>(5, 0));
    for (int i=0; i<4; ++i) {
      for (int j=0; j<10; ++j) {
        if (m[i][j]) {
          occ[i][sums[j]]++;
        }
      }
    }

    dprint(m, "4");
    for (int i=0; i<4; ++i) if (occ[i][2] == 3) swap(m[i], m[3]);
    dprint(m, "3");
    for (int i=0; i<3; ++i) if (occ[i][1] == 0) swap(m[i], m[2]);
    dprint(m, "2");
    for (int i=0; i<2; ++i) if (occ[i][3] == 1) swap(m[i], m[1]);

    dprint(m, "10");
    for (int i=0; i<10; ++i) if (cnt[i] == 0) { swapCol(m, i, 9); swap(cnt[i], cnt[9]); break; }
    dprint(m, "9");
    for (int i=0; i<9; ++i) if (cnt[i] == 1 && m[0][i]) { swapCol(m, i, 8); swap(cnt[i], cnt[8]); break; }
    dprint(m, "8");
    for (int i=0; i<8; ++i) if (cnt[i] == 1) { swapCol(m, i, 7); swap(cnt[i], cnt[7]); break; }
    dprint(m, "7");
    for (int i=0; i<7; ++i) if (m[0][i] == 0 && m[1][i] == 0) { swapCol(m, i, 6); swap(occ[i], occ[6]); break; }
    dprint(m, "6");
    for (int i=0; i<6; ++i) if (m[1][i] == 0 && m[2][i] == 0) { swapCol(m, i, 5); break; }
    dprint(m, "5");
    for (int i=0; i<5; ++i) if (m[0][i] == 0 && m[2][i] == 0) { swapCol(m, i, 4); break; }
    dprint(m, "4");
    for (int i=0; i<4; ++i) if (m[0][i] == 0 && m[3][i] == 0) { swapCol(m, i, 3); break; }
    dprint(m, "3");
    for (int i=0; i<3; ++i) if (m[1][i] == 0) { swapCol(m, i, 2); break; }
    dprint(m, "2");
    for (int i=0; i<2; ++i) if (m[3][i] == 0) { swapCol(m, i, 1); break; }
  }

  LL res = 0;
  for (int pi=5; pi>=0; --pi) {
    int p = pt[pi];
    int q = po(10, p);

    res *= q; 

    for (int j=0; p>0; ++j) {
      if (m[pi+4][j] == 0) {
        res += po(10-j-1, p-1);
      } else {
        --p;
      }
    }
  }
  printf("%lld\n", res);

}


void work() {
  if (algosia) A();
  else B();
}

int main() {
  char s[20]; scanf(" %s", s);

  if (strcmp(s, "Algosia") == 0) algosia = true;

  fact.push_back(1);
  for (int i=1; i<15; ++i) fact.push_back(fact.back() * i);

  int n, t; scanf("%d %d", &n, &t);
  while (t--) work();
  return 0;
}