#include <bits/stdc++.h>
using namespace std;
typedef double ld;
typedef unsigned int uint;
const uint ST = 100;
const uint QT = 10000;
const uint iterations = 10;
const ld learing_rate_S = 10.;
const ld learing_rate_Q = 10.;
inline ld sigmoid(ld x) {
return 1./(1.+exp(-x));
}
inline ld rsigmoid(ld x) {
return 1./(1.+exp(x));
}
inline ld min(ld a, ld b) {
return (a<=b? a:b);
}
inline ld max(ld a, ld b) {
return (a>=b? a:b);
}
ld mean(vector<ld> &v) {
ld mean = 0.;
for (auto &i: v) mean+=i;
mean /= ld(v.size());
return mean;
}
ld stdev(vector<ld> &v) {
ld std = 0.;
for (auto &i: v) std+=i*i;
std = sqrt(std/ld(v.size()));
return std;
}
void solve() {
string str;
vector<vector<bool>> A(ST,vector<bool>(QT));
for (auto &s: A) {
cin >> str;
for (uint i = 0; i < QT; i++) {
s[i] = (str[i]=='1');
}
}
vector<ld> S(ST,0.), Q(QT,0.);
//Initial guess for students
for (uint i = 0; i < ST; i++) {
uint cnt = 0;
for (uint j = 0; j < QT; j++) {
if (A[i][j]) {
cnt++;
}
}
ld rateo = ld(cnt)/ld(QT);
S[i] = max(-3.,min(3., log(rateo/(1-rateo))));
}
//Initial guess for question
for (uint j = 0; j < QT; j++) {
uint cnt = 0;
for (uint i = 0; i < ST; i++) {
if (A[i][j]) {
cnt++;
}
}
ld rateo = ld(cnt)/ld(ST);
Q[j] = max(-3.,min(3., log(rateo/(1-rateo))));
}
for(uint k=0; k<iterations;k++) {
// Update questions
for (uint j = 0; j < QT; j++) {
ld loglike = 0.;
for (uint i = 0; i < ST; i++) {
if (A[i][j]) {
loglike -= rsigmoid(S[i]-Q[j]);
} else {
loglike += sigmoid(S[i]-Q[j]);
}
}
loglike /= ld(ST);
Q[j] = max(-3.,min(3., Q[j] + (learing_rate_Q)*loglike));
}
// Update students
for (uint i = 0; i < ST; i++) {
ld loglike = 0.;
for (uint j = 0; j < QT; j++) {
if (A[i][j]) {
loglike += rsigmoid(S[i]-Q[j]);
} else {
loglike -= sigmoid(S[i]-Q[j]);
}
}
loglike /= ld(QT);
S[i] = max(-3.,min(3., S[i] + (learing_rate_S)*loglike));
}
}
ld best_likelihood = -1e10;
uint cheater = 0;
uint cnt;
ld rateo, Sact, likelihood;
for(uint i = 0; i < ST; i++) {
cnt = 0;
for (uint j = 0; j < QT; j++) {
if (A[i][j]) {
cnt++;
}
}
rateo = max(ld(cnt)-ld(QT)/2.,0)/(ld(QT)/2.);
Sact = max(-3.,min(3., log(rateo/(1-rateo))));
likelihood = 0.;
for (uint j = 0; j < QT; j++) {
if (A[i][j]) {
likelihood -= -log(1+exp(Q[j]-S[i]));
likelihood += log((2+exp(Q[j]-Sact))/(1+exp(Q[j]-Sact)));
} else {
likelihood -= -log(1+exp(S[i]-Q[j]));
likelihood += -log(1+exp(Sact-Q[j]));
}
}
//likelihood -= ld(QT)*log(0.5);
if (likelihood > best_likelihood) {
best_likelihood = likelihood;
cheater = i;
}
}
cout << cheater + 1;
}
int main() {
// IO optimizer. Comment if interactive problem
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
uint t;
ld p;
cin >> t >> p;
for (uint i = 0; i < t; i++) {
cout << "Case #" << i+1 << ": ";
solve();
cout<< endl;
}
return 0;
}