Problem:
Jigsaw Puzzle and Detective
問題
$T$ 個のテストケースについて、以下の問題に答えてください。
いくつかの正方形状のピースを、重なりや隙間がなく全体が長方形の形になるように並べたジグゾーパズルがあります。各ピースの各辺は普通の辺、凸な辺、凹な辺のいずれかです。凸な辺と凹な辺は互いに接しなければならず、普通の辺はどの辺とも接してはなりません。なお、ピースは
回したり裏返したり
して使うことができます。
ある日、怪盗 kotamanegi 三世にピースを $1$ 枚を盗まれてしまい、さらに揃っていたジグゾーパズルもバラバラにされてしまいました。探偵であるあなたは、残された $N$ 枚のピースからどんなピースが盗まれたかを推理するように依頼されました。kotamanegi 三世はとても優しいので、盗んだピースの形状が点対称かどうかを教えてくれました。
盗まれたピースの形状を答えてください。
制約
$1$ つの入力に含まれるテストケースについて $N$ の総和は $2 \times 10^5$ 以下である
$1 \leq N$
$S$ は $0$ または $1$ で、$S=0$ なら盗まれたピースは点対称ではなく、$S=1$ なら点対称である
$1 \leq A_{i, 1}, A_{i, 2}, A_{i, 3}, A_{i, 4} \leq 3$
$A_{i, 1}, A_{i, 2}, A_{i, 3}, A_{i, 4}$ はそれぞれ $i$ 枚目のピースの上、右、下、左の辺の種類を表し、$1, 2, 3$ がそれぞれ普通の辺、凸な辺、凹な辺を表す
回したり裏返したりして一致するピースはすべて同じものとして認識される
与えられる入力について、答えが定まることが保証される
入力はすべて整数
入力
入力はすべて標準入力から与えられます。
入力の $1$ 行目にテストケースの個数が与えられます。
$T$
続いて、以下の形式のテストケースが $T$ 個与えられます。
$N$ $S$
$A_{1, 1}$ $A_{1, 2}$ $A_{1, 3}$ $A_{1, 4}$
$A_{2, 1}$ $A_{2, 2}$ $A_{2, 3}$ $A_{2, 4}$
$\vdots$
$A_{N, 1}$ $A_{N, 2}$ $A_{N, 3}$ $A_{N, 4}$
出力
ピースの形状を入力と同様の形式で $T$ 行出力してください。
ただし、ピースを回したり裏返したりして、
$A_1$ $A_2$ $A_3$ $A_4$
が辞書順で最小となるようにして出力してください。
入力例 1
2
2 1
1 1 1 2
1 1 1 2
3 0
1 1 2 3
1 2 2 1
3 3 1 1
出力例 1
1 3 1 3
1 1 2 3

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

bool lex_less(const vector<int>& a, const vector<int>& b) {
    for (int i = 0; i < 4; ++i) if (a[i] != b[i]) return a[i] < b[i];
    return false;
}

vector<int> rotate_left(const vector<int>& a, int k) {
    vector<int> r(4);
    for (int i = 0; i < 4; ++i) r[i] = a[(i + k) & 3];
    return r;
}

vector<int> mirror_lr(const vector<int>& a) {
    return {a[0], a[3], a[2], a[1]};
}

vector<int> canonical_dihedral(vector<int> a) {
    vector<int> best(4, INT_MAX);

    for (int k = 0; k < 4; ++k) {
        vector<int> r = rotate_left(a, k);
        if (lex_less(r, best)) best = r;
    }
    vector<int> m = mirror_lr(a);
    for (int k = 0; k < 4; ++k) {
        vector<int> r = rotate_left(m, k);
        if (lex_less(r, best)) best = r;
    }
    return best;
}

bool is_centrally_symmetric(const vector<int>& v) {
    return v[0] == v[2] && v[1] == v[3];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T; cin >> T;
    while (T--) {
        long long N;
        int S;
        cin >> N >> S;

        long long cnt[4] = {0,0,0,0}; 
        for (long long i = 0; i < N; ++i) {
            int a1,a2,a3,a4;
            cin >> a1 >> a2 >> a3 >> a4;
            ++cnt[a1]; ++cnt[a2]; ++cnt[a3]; ++cnt[a4];
        }

        long long M = N + 1; 
        vector<int> perims;
        for (long long d = 1; d * d <= M; ++d) {
            if (M % d == 0) {
                long long w = d, h = M / d;
                perims.push_back( int(2 * (w + h)) );
                if (w != h) perims.push_back( int(2 * (h + w)) ); 
            }
        }
        sort(perims.begin(), perims.end());
        perims.erase(unique(perims.begin(), perims.end()), perims.end());

        vector<int> answer(4, 0);
        bool found = false;

        for (int P : perims) {
            int x1 = P - int(cnt[1]); 
            if (x1 < 0 || x1 > 4) continue;

            int sum23 = 4 - x1;
            int delta = int(cnt[3] - cnt[2]); 
            if (((sum23 + delta) & 1) != 0) continue;

            int x2 = (sum23 + delta) / 2;
            int x3 = sum23 - x2;
            if (x2 < 0 || x2 > 4 || x3 < 0 || x3 > 4) continue;

            vector<int> edges;
            edges.insert(edges.end(), x1, 1);
            edges.insert(edges.end(), x2, 2);
            edges.insert(edges.end(), x3, 3);
            sort(edges.begin(), edges.end());

            vector<int> best(4, INT_MAX);
            bool any = false;

            do {
                bool cs = is_centrally_symmetric(edges);
                if ((S == 1 && !cs) || (S == 0 && cs)) continue;

                vector<int> cand = canonical_dihedral(edges);
                if (!any || lex_less(cand, best)) {
                    best = cand;
                    any = true;
                }
            } while (next_permutation(edges.begin(), edges.end()));

            if (!any) continue;

            answer = best;
            found = true;
            break;
        }

        cout << answer[0] << ' ' << answer[1] << ' ' << answer[2] << ' ' << answer[3] << '\n';
    }
    return 0;
}
accuracy: 0.09090909090909091
time_ms: 20
memory_kb: 3336
final_rank: 11

code_snippet: #include <bits/stdc++.h>

using namespace std;

int main() {
    int t;
    cin >> t;
    while (t --> 0) {
        int n, s;
        cin >> n >> s;
        int type_1 = 0, type_2 = 0, type_3 = 0, sides_number = (n+1) * 4;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < 4; ++j) {
                int x;
                cin >> x;
                type_1 += x == 1;
                type_2 += x == 2;
                type_3 += x == 3;
            }
        }
        //cout << type_1 << ' ' << type_2 << ' ' << type_3 << ' ' << sides_number << endl;
        tuple<int, int, int, int> answer = {INT_MAX, INT_MAX, INT_MAX, INT_MAX};
        for (int a = 1; a <= 3; ++a) {
            for (int b = 1; b <= 3; ++b) {
                for (int c = 1; c <= 3; ++c) {
                    for (int d = 1; d <= 3; ++d) {
                        int new_1 = type_1 + (a == 1) + (b == 1) + (c == 1) + (d == 1);
                        int new_2 = type_2 + (a == 2) + (b == 2) + (c == 2) + (d == 2);
                        int new_3 = type_3 + (a == 3) + (b == 3) + (c == 3) + (d == 3);
                        if (s == 0) {
                            if (new_2 == new_3 && (a != c || b != d)) {
                                answer = min(answer, {a, b, c, d});
                            }
                        } else {
                            if (new_2 == new_3 && (a == c && b == d)) {
                                answer = min(answer, {a, b, c, d});
                            }
                        }
                    }
                }
            }
        }
        auto& [a, b, c, d] = answer;
        cout << a << ' ' << b << ' ' << c << ' ' << d << endl;
    }
}
accuracy: 0.09090909090909091
time_ms: 60
memory_kb: 3052
final_rank: 15

code_snippet: #include <vector>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <iostream>
#include <unordered_map>
#include <climits>
#include <queue>
#include <stack>
#include <random>
#include <set>
#include <cassert>
#include <iterator>
using Piece = std::vector<int>;
Piece solve(const int n, const int s, const std::vector<Piece> pieces);
int main() {
	int test_case; std::cin >> test_case;
	for (auto i = 0; i < test_case; ++i) {
		int n, s; std::cin >> n >> s;
		std::vector<Piece> pieces(n, Piece(4));
		for (auto& piece : pieces) {
			for (auto& a : piece) {
				std::cin >> a;
			}
		}
		const auto result = solve(n, s, pieces);
		for (auto i = 0; i < 4; ++i) {
		    if (i > 0) {
		        std::cout << ' ';
		    }
		    std::cout << result[i];
		}
		std::cout << '\n';
	}
}

std::vector<int> solve(const int n, const int s, const std::vector<Piece> pieces) {
	int corner_count{ 0 };
	int edge_count{ 0 };
	int outer_bump_count{ 0 };
	int outer_dip_count{ 0 };
	int inner_bump_count{ 0 };
	int inner_dip_count{ 0 };
	const auto is_one_line = std::any_of(pieces.begin(), pieces.end(), [](const auto& piece) {return std::count(piece.begin(), piece.end(), 1) == 3; });
	for (const auto piece : pieces) {
		const auto edge = std::count(piece.begin(), piece.end(), 1);
		const auto bump = std::count(piece.begin(), piece.end(), 2);
		const auto dip = std::count(piece.begin(), piece.end(), 3);
		if (is_one_line && edge == 3) {
			corner_count += 2;
		}
		else if (!is_one_line && edge == 2) {
			corner_count += 1;
		}
		edge_count += edge;
		outer_bump_count += bump;
		outer_dip_count += dip;
		if (edge == 0) {
			inner_bump_count += bump;
			inner_dip_count += dip;
			outer_bump_count -= bump;
			outer_dip_count -= dip;
		}
		else if (edge == 1) {
			const auto edge_position = std::distance(piece.begin(), std::find(piece.begin(), piece.end(), 1));
			const auto opposite = *std::next(piece.begin(), (edge_position + 2) & 3);
			if (opposite == 2) {
				inner_bump_count += 1;
				outer_bump_count -= 1;
			}
			else if (opposite == 3) {
				inner_dip_count += 1;
				outer_dip_count -= 1;
			}
			else {
				throw 0;
			}
		}
	}
	if (corner_count == 2) {
		if (outer_bump_count + 1 == outer_dip_count) {
			return { 1, 1, 1, 2 };
		}
		else if (outer_bump_count == outer_dip_count + 1) {
			return { 1, 1, 1, 3 };
		}
		else {
			throw 0;
		}
	}
	if (corner_count == 3) {
		if (outer_bump_count + 2 == outer_dip_count) {
			return { 1, 1, 2, 2 };
		}
		else if (outer_bump_count == outer_dip_count + 2) {
			return { 1, 1, 3, 3 };
		}
		else if (outer_bump_count == outer_dip_count) {
			return { 1, 1, 2, 3 };
		}
		else {
			throw 0;
		}
	}
	if (corner_count == 4) {
		if (is_one_line) {
			if (outer_bump_count + 2 == outer_dip_count) {
				return { 1, 2, 1, 2 };
			}
			else if (outer_bump_count == outer_dip_count + 2) {
				return { 1, 3, 1, 3 };
			}
			else if (outer_bump_count == outer_dip_count) {
				return { 1, 2, 1, 3 };
			}
			else {
				throw 0;
			}
		}
		else {
			if (edge_count % 2 == 0) {
				if (inner_bump_count + 4 == inner_dip_count) {
					return { 2, 2, 2, 2 };
				}
				else if (inner_bump_count + 2 == inner_dip_count) {
					return { 2, 2, 2, 3 };
				}
				else if (inner_bump_count == inner_dip_count) {
					return s == 0 ? Piece{ 2, 2, 3, 3} : Piece{2, 3, 2, 3};
				}
				else if (inner_bump_count == inner_dip_count + 2) {
					return { 2, 3, 3, 3 };
				}
				else if (inner_bump_count == inner_dip_count + 4) {
					return { 3, 3, 3, 3 };
				}
				else {
					throw 0;
				}
			}
			else {
				Piece result(4, 1);
				if (outer_bump_count + 2 == outer_dip_count) {
					result[1] = result[3] = 2;
				}
				else if (outer_bump_count == outer_dip_count) {
					result[1] = 2;
					result[3] = 3;
				}
				else if (outer_bump_count == outer_dip_count + 2) {
					result[1] = result[3] = 3;
				}
				else {
					throw 0;
				}
				if (inner_bump_count + 1 == inner_dip_count) {
					result[2] = 2;
				}
				else if (inner_bump_count == inner_dip_count + 1) {
					result[2] = 3;
				}
				else {
					throw 0;
				}
				return result;
			}
		}
	}
	throw 0;
}
accuracy: 1
time_ms: 80
memory_kb: 23964
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;

int solve(){
    int n,s,a[4],cnt1[4]={},cho=0,chi=0;
    cin>>n>>s;
    while(n--){
        int c=0;
        for(auto& x:a){
            cin>>x;
            c+=x==1;
        }
        cnt1[c]++;
        rotate(a,min_element(a,a+4),a+4);
        if(c>=2){
            for(auto& x:a)if(x!=1)cho+=(x==2?1:-1);
        }else if(c==1){
                cho+=(a[1]==2?1:-1);
                chi+=(a[2]==2?1:-1);
                cho+=(a[3]==2?1:-1);
        }else for(auto& x:a)if(x!=1)chi+=(x==2?1:-1);
    }
    if(cnt1[3]){
        if(cnt1[3]&1)cout<<"1 1 1 "<<2+(cho>0);
        else cout<<"1 "<<2+(cho>0)<<" 1 "<<2+(cho>=0);
        cout<<'\n';
        return 0;
    }
    if(cnt1[2]==3){
        assert(chi==0);
        cout<<"1 1 "<<2+(cho>0)<<' '<<2+(cho>=0)<<'\n';
        return 0;
    }
    if(cnt1[1]&1){
        assert(chi!=0);
        cout<<"1 "<<2+(cho>0)<<' '<<2+(chi>0)<<' '<<2+(cho>=0)<<'\n';
        return 0;
    }
    if(chi==4)cout<<"3 3 3 3\n";
    if(chi==2)cout<<"2 3 3 3\n";
    if(chi==0){
        if(s)cout<<"2 3 2 3\n";
        else cout<<"2 2 3 3\n";
    }
    if(chi==-2)cout<<"2 2 2 3\n";
    if(chi==-4)cout<<"2 2 2 2\n";
    return 0;
}
int main(){
    cin.tie(0)->sync_with_stdio(0);
    int T;
    cin>>T;
    while(T--)solve();
}
accuracy: 1
time_ms: 30
memory_kb: 3456
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;

int solve(){
    int n,s,a[4],cnt1[4]={},cho=0,chi=0;
    cin>>n>>s;
    while(n--){
        int c=0;
        for(auto& x:a){
            cin>>x;
            c+=x==1;
        }
        cnt1[c]++;
        rotate(a,min_element(a,a+4),a+4);
        if(c>=2){
            for(auto& x:a)if(x!=1)cho+=(x==2?1:-1);
        }else if(c==1){
                cho+=(a[1]==2?1:-1);
                chi+=(a[2]==2?1:-1);
                cho+=(a[3]==2?1:-1);
        }else for(auto& x:a)if(x!=1)chi+=(x==2?1:-1);
    }
    if(cnt1[3]){
        if(cnt1[3]&1)cout<<"1 1 1 "<<2+(cho>0);
        else cout<<"1 "<<2+(cho>0)<<" 1 "<<2+(cho>=0);
        cout<<'\n';
        return 0;
    }
    if(cnt1[2]==3){
        assert(chi==0);
        cout<<"1 1 "<<2+(cho>0)<<' '<<2+(cho>=0)<<'\n';
        return 0;
    }
    if(cnt1[1]&1){
        assert(chi!=0);
        cout<<"1 "<<2+(cho>0)<<' '<<2+(cho>0)<<' '<<2+(cho>=0)<<'\n';
        return 0;
    }
    if(chi==4)cout<<"3 3 3 3\n";
    if(chi==2)cout<<"2 3 3 3\n";
    if(chi==0){
        if(s)cout<<"2 3 2 3\n";
        else cout<<"2 2 3 3\n";
    }
    if(chi==-2)cout<<"2 2 2 3\n";
    if(chi==-4)cout<<"2 2 2 2\n";
    return 0;
}
int main(){
    cin.tie(0)->sync_with_stdio(0);
    int T;
    cin>>T;
    while(T--)solve();
}
accuracy: 0.09090909090909091
time_ms: 20
memory_kb: 3312
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define each(e, v) for (auto &e : v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}

template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}

template <typename T>
int flg(T x, int i) {
    return (x >> i) & 1;
}

template <typename T>
void print(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (v.empty()) cout << '\n';
}

template <typename T>
void printn(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << '\n';
}

template <typename T>
int lb(const vector<T> &v, T x) {
    return lower_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
int ub(const vector<T> &v, T x) {
    return upper_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
void rearrange(vector<T> &v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <typename T>
vector<int> id_sort(const vector<T> &v, bool greater = false) {
    int n = v.size();
    vector<int> ret(n);
    iota(begin(ret), end(ret), 0);
    sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });
    return ret;
}

template <typename S, typename T>
pair<S, T> operator+(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first + q.first, p.second + q.second);
}

template <typename S, typename T>
pair<S, T> operator-(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first - q.first, p.second - q.second);
}

template <typename S, typename T>
istream &operator>>(istream &is, pair<S, T> &p) {
    S a;
    T b;
    is >> a >> b;
    p = make_pair(a, b);
    return is;
}

template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
    return os << p.first << ' ' << p.second;
}

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const int MOD = 1000000007;
// const int MOD = 998244353;

int main() {
    int T;
    cin >> T;

    while (T--) {
        int N, S;
        cin >> N >> S;

        vector<vector<int>> a(N, vector<int>(4));
        rep(i, N) {
            rep(j, 4) {
                cin >> a[i][j];
                a[i][j]--;
            }
        }

        vector<int> cnt(3, 0), deg(5, 0), li(3, 0);
        rep(i, N) {
            int tmp = 0;
            rep(j, 4) {
                if (a[i][j] == 0) tmp++;
                cnt[a[i][j]]++;
                rep2(k, 1, 3) {
                    if (k == 2) continue;
                    int l = (j + k) % 4;
                    if (a[i][l] == 0) li[a[i][j]]++;
                }
            }
            deg[tmp]++;
        }

        vector<int> ans(4, 0);

        if (deg[3] > 0) {
            if (cnt[1] > cnt[2]) {
                rep(j, cnt[1] - cnt[2]) ans[3 - 2 * j] = 2;
            } else if (cnt[1] < cnt[2]) {
                rep(j, cnt[2] - cnt[1]) ans[3 - 2 * j] = 1;
            } else {
                ans[1] = 1, ans[3] = 2;
            }
        } else if (deg[2] < 4) {
            if (cnt[1] > cnt[2]) {
                ans[2] = 2, ans[3] = 2;
            } else if (cnt[1] < cnt[2]) {
                ans[2] = 1, ans[3] = 1;
            } else {
                ans[2] = 1, ans[3] = 2;
            }
        } else if (deg[1] & 1) {
            if (cnt[1] - cnt[2] == 3) {
                ans[1] = 2, ans[2] = 2, ans[3] = 2;
            } else if (cnt[2] - cnt[1] == 3) {
                ans[1] = 1, ans[2] = 1, ans[3] = 1;
            } else if (cnt[1] - cnt[2] == 1) {
                if (li[1] == li[2]) {
                    ans[1] = 1, ans[2] = 2, ans[3] = 2;
                } else {
                    ans[1] = 2, ans[2] = 1, ans[3] = 2;
                }
            } else {
                if (li[1] == li[2]) {
                    ans[1] = 1, ans[2] = 1, ans[3] = 2;
                } else {
                    ans[1] = 1, ans[2] = 2, ans[3] = 1;
                }
            }
        } else {
            if (cnt[1] - cnt[2] == 4) {
                ans[0] = 2, ans[1] = 2, ans[2] = 2, ans[3] = 2;
            } else if (cnt[2] - cnt[1] == 4) {
                ans[0] = 1, ans[1] = 1, ans[2] = 1, ans[3] = 1;
            } else if (cnt[1] - cnt[2] == 2) {
                ans[0] = 1, ans[1] = 2, ans[2] = 2, ans[3] = 2;
            } else if (cnt[2] - cnt[1] == 2) {
                ans[0] = 1, ans[1] = 1, ans[2] = 1, ans[3] = 2;
            } else {
                if (S == 0) {
                    ans[0] = 1, ans[1] = 1, ans[2] = 2, ans[3] = 2;
                } else {
                    ans[0] = 1, ans[1] = 2, ans[2] = 1, ans[3] = 2;
                }
            }
        }

        print(ans, 1);
    }
}
accuracy: 1
time_ms: 30
memory_kb: 13576
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define each(e, v) for (auto &e : v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}

template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}

template <typename T>
int flg(T x, int i) {
    return (x >> i) & 1;
}

template <typename T>
void print(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (v.empty()) cout << '\n';
}

template <typename T>
void printn(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << '\n';
}

template <typename T>
int lb(const vector<T> &v, T x) {
    return lower_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
int ub(const vector<T> &v, T x) {
    return upper_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
void rearrange(vector<T> &v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <typename T>
vector<int> id_sort(const vector<T> &v, bool greater = false) {
    int n = v.size();
    vector<int> ret(n);
    iota(begin(ret), end(ret), 0);
    sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });
    return ret;
}

template <typename S, typename T>
pair<S, T> operator+(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first + q.first, p.second + q.second);
}

template <typename S, typename T>
pair<S, T> operator-(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first - q.first, p.second - q.second);
}

template <typename S, typename T>
istream &operator>>(istream &is, pair<S, T> &p) {
    S a;
    T b;
    is >> a >> b;
    p = make_pair(a, b);
    return is;
}

template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
    return os << p.first << ' ' << p.second;
}

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const int MOD = 1000000007;
// const int MOD = 998244353;

int main() {
    int T;
    cin >> T;

    while (T--) {
        int N, S;
        cin >> N >> S;

        vector<vector<int>> a(N, vector<int>(4));
        rep(i, N) {
            rep(j, 4) {
                cin >> a[i][j];
                a[i][j]--;
            }
        }

        vector<int> cnt(3, 0), deg(5, 0), li(3, 0);
        rep(i, N) {
            int tmp = 0;
            rep(j, 4) {
                if (a[i][j] == 0) tmp++;
                cnt[a[i][j]]++;
                rep2(k, 1, 3) {
                    if (k == 2) continue;
                    int l = (j + k) % 4;
                    if (a[i][l] == 0) li[a[i][j]]++;
                }
            }
            deg[tmp]++;
        }

        vector<int> ans(4, 0);

        if (deg[3] > 0) {
            if (cnt[1] > cnt[2]) {
                rep(j, cnt[1] - cnt[2]) ans[3 - 2 * j] = 2;
            } else if (cnt[1] < cnt[2]) {
                rep(j, cnt[2] - cnt[1]) ans[3 - 2 * j] = 1;
            } else {
                ans[1] = 1, ans[3] = 2;
            }
        } else if (deg[2] < 4) {
            if (cnt[1] > cnt[2]) {
                ans[2] = 2, ans[3] = 2;
            } else if (cnt[1] < cnt[2]) {
                ans[2] = 1, ans[3] = 1;
            } else {
                ans[2] = 1, ans[3] = 2;
            }
        } else if (deg[1] & 1) {
            if (cnt[1] - cnt[2] == 3) {
                ans[1] = 2, ans[2] = 2, ans[3] = 2;
            } else if (cnt[2] - cnt[1] == 3) {
                ans[1] = 1, ans[2] = 1, ans[3] = 1;
            } else if (cnt[1] - cnt[2] == 1) {
                if (li[1] == li[2]) {
                    ans[1] = 1, ans[2] = 2, ans[3] = 2;
                } else {
                    ans[1] = 2, ans[2] = 1, ans[3] = 2;
                }
            } else {
                if (li[1] == li[2]) {
                    ans[1] = 1, ans[2] = 1, ans[3] = 2;
                } else {
                    ans[1] = 1, ans[2] = 2, ans[3] = 1;
                }
            }
        } else {
            if (cnt[1] - cnt[2] == 4) {
                ans[0] = 2, ans[1] = 2, ans[2] = 2, ans[3] = 2;
            } else if (cnt[2] - cnt[1] == 4) {
                ans[0] = 2, ans[1] = 2, ans[2] = 2, ans[3] = 2;
            } else if (cnt[1] - cnt[2] == 2) {
                ans[0] = 1, ans[1] = 2, ans[2] = 2, ans[3] = 2;
            } else if (cnt[2] - cnt[1] == 2) {
                ans[0] = 1, ans[1] = 1, ans[2] = 1, ans[3] = 2;
            } else {
                if (S == 0) {
                    ans[0] = 1, ans[1] = 1, ans[2] = 2, ans[3] = 2;
                } else {
                    ans[0] = 1, ans[1] = 2, ans[2] = 1, ans[3] = 2;
                }
            }
        }

        print(ans, 1);
    }
}
accuracy: 0.18181818181818182
time_ms: 30
memory_kb: 11820
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
  int T;
  cin >> T;
  for (int i = 0; i < T; i++){
    int N, S;
    cin >> N >> S;
    vector<vector<int>> A(N, vector<int>(4));
    for (int j = 0; j < N; j++){
      for (int k = 0; k < 4; k++){
        cin >> A[j][k];
      }
    }
    int cnt11 = 0, cnt111 = 0;
    int cnt1 = 0, cnt2 = 0, cnt3 = 0;
    int ce2 = 0, ce3 = 0;
    for (int j = 0; j < N; j++){
      int p1 = 0;
      for (int k = 0; k < 4; k++){
        if (A[j][k] == 1){
          cnt1++;
          p1++;
        }
        if (A[j][k] == 2){
          cnt2++;
          if (A[j][(k + 1) % 4] == 1 || A[j][(k + 3) % 4] == 1){
            ce2++;
          }
        }
        if (A[j][k] == 3){
          cnt3++;
          if (A[j][(k + 1) % 4] == 1 || A[j][(k + 3) % 4] == 1){
            ce3++;
          }
        }
      }
      if (p1 == 2){
        cnt11++;
      }
      if (p1 == 3){
        cnt111++;
      }
    }
    if (cnt111 > 0){
      if (cnt111 == 2){
        if (cnt2 < cnt3){
          cout << "1 2 1 2" << "\n";
        } else if (cnt2 > cnt3){
          cout << "1 3 1 3" << "\n";
        } else {
          cout << "1 2 1 3" << "\n";
        }
      } else {
        if (cnt2 < cnt3){
          cout << "1 1 1 2" << "\n";
        } else {
          cout << "1 1 1 3" << "\n";
        }
      }
    } else if (cnt11 < 4){
      if (cnt2 < cnt3){
        cout << "1 1 2 2" << "\n";
      } else if (cnt2 > cnt3){
        cout << "1 1 3 3" << "\n";
      } else {
        cout << "1 1 2 3" << "\n";
      }
    } else if (cnt1 % 2 == 1){
      if (cnt2 < cnt3){
        if (cnt2 - cnt3 == -3){
          cout << "1 2 2 2" << "\n";
        } else {
          if (ce2 < ce3){
            cout << "1 2 3 2" << "\n";
          } else {
            cout << "1 2 2 3" << "\n";
          }
        }
      } else {
        if (cnt2 - cnt3 == 3){
          cout << "1 3 3 3" << "\n";
        } else {
          if (ce3 < ce2){
            cout << "1 3 2 3" << "\n";
          } else {
            cout << "1 2 3 3" << "\n";
          }
        }
      }
    } else {
      if (cnt2 - cnt3 == 4){
        cout << "3 3 3 3" << "\n";
      } else if (cnt2 - cnt3 == -4){
        cout << "2 2 2 2" << "\n";
      } else if (cnt2 - cnt3 == 2){
        cout << "2 3 3 3" << "\n";
      } else if (cnt2 - cnt3 == -2){
        cout << "2 2 2 3" << "\n";
      } else {
        if (S == 1){
          cout << "2 3 2 3" << "\n";
        } else {
          cout << "2 2 3 3" << "\n";
        }
      }
    }
  }
}
accuracy: 1
time_ms: 70
memory_kb: 13576
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
  int T;
  cin >> T;
  for (int i = 0; i < T; i++){
    int N, S;
    cin >> N >> S;
    vector<vector<int>> A(N, vector<int>(4));
    for (int j = 0; j < N; j++){
      for (int k = 0; k < 4; k++){
        cin >> A[j][k];
      }
    }
    int cnt11 = 0, cnt111 = 0;
    int cnt1 = 0, cnt2 = 0, cnt3 = 0;
    int ce2 = 0, ce3 = 0;
    for (int j = 0; j < N; j++){
      int p1 = 0;
      for (int k = 0; k < 4; k++){
        if (A[j][k] == 1){
          cnt1++;
          p1++;
        }
        if (A[j][k] == 2){
          cnt2++;
          if (A[j][(k + 1) % 4] == 1 || A[j][(k + 3) % 4] == 1){
            ce2++;
          }
        }
        if (A[j][k] == 3){
          cnt3++;
          if (A[j][(k + 1) % 4] == 1 || A[j][(k + 3) % 4] == 1){
            ce3++;
          }
        }
      }
      if (p1 == 2){
        cnt11++;
      }
      if (p1 == 3){
        cnt111++;
      }
    }
    if (cnt111 > 0){
      if (cnt111 == 2){
        if (cnt2 < cnt3){
          cout << "1 2 1 2" << "\n";
        } else if (cnt2 > cnt3){
          cout << "1 3 1 3" << "\n";
        } else {
          cout << "1 2 1 3" << "\n";
        }
      } else {
        if (cnt2 < cnt3){
          cout << "1 1 1 2" << "\n";
        } else {
          cout << "1 1 1 3" << "\n";
        }
      }
    } else if (cnt11 < 4){
      if (cnt2 < cnt3){
        cout << "1 1 2 2" << "\n";
      } else if (cnt2 > cnt3){
        cout << "1 1 3 3" << "\n";
      } else {
        cout << "1 1 2 3" << "\n";
      }
    } else if (cnt1 % 2 == 1){
      if (cnt2 < cnt3){
        if (ce2 < ce3){
          cout << "1 2 3 2" << "\n";
        } else {
          cout << "1 2 2 3" << "\n";
        }
      } else {
        if (ce3 < ce2){
          cout << "1 3 2 3" << "\n";
        } else {
          cout << "1 2 3 3" << "\n";
        }
      }
    } else {
      if (cnt2 - cnt3 == 4){
        cout << "3 3 3 3" << "\n";
      } else if (cnt2 - cnt3 == -4){
        cout << "2 2 2 2" << "\n";
      } else if (cnt2 - cnt3 == 2){
        cout << "2 3 3 3" << "\n";
      } else if (cnt2 - cnt3 == -2){
        cout << "2 2 2 3" << "\n";
      } else {
        if (S == 1){
          cout << "2 3 2 3" << "\n";
        } else {
          cout << "2 2 3 3" << "\n";
        }
      }
    }
  }
}
accuracy: 0.09090909090909091
time_ms: 70
memory_kb: 11712
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
  int T;
  cin >> T;
  for (int i = 0; i < T; i++){
    int N, S;
    cin >> N >> S;
    vector<vector<int>> A(N, vector<int>(4));
    for (int j = 0; j < N; j++){
      for (int k = 0; k < 4; k++){
        cin >> A[j][k];
      }
    }
    int cnt11 = 0, cnt111 = 0;
    int cnt1 = 0, cnt2 = 0, cnt3 = 0;
    int ce2 = 0, ce3 = 0;
    for (int j = 0; j < N; j++){
      int p1 = 0;
      for (int k = 0; k < 4; k++){
        if (A[j][k] == 1){
          cnt1++;
          p1++;
        }
        if (A[j][k] == 2){
          cnt2++;
          if (A[j][(k + 1) % 4] == 1 || A[j][(k + 3) % 4] == 1){
            ce2++;
          }
        }
        if (A[j][k] == 3){
          cnt3++;
          if (A[j][(k + 1) % 4] == 1 || A[j][(k + 3) % 4] == 1){
            ce3++;
          }
        }
      }
      if (p1 == 2){
        cnt11++;
      }
      if (p1 == 3){
        cnt111++;
      }
    }
    if (cnt111 > 0){
      if (cnt111 == 2){
        if (cnt2 < cnt3){
          cout << "1 2 1 2" << "\n";
        } else {
          cout << "1 3 1 3" << "\n";
        }
      } else {
        if (cnt2 < cnt3){
          cout << "1 1 1 2" << "\n";
        } else {
          cout << "1 1 1 3" << "\n";
        }
      }
    } else if (cnt11 < 4){
      if (cnt2 < cnt3){
        cout << "1 1 2 2" << "\n";
      } else if (cnt2 > cnt3){
        cout << "1 1 3 3" << "\n";
      } else {
        cout << "1 1 2 3" << "\n";
      }
    } else if (cnt1 % 2 == 1){
      if (cnt2 < cnt3){
        if (ce2 < ce3){
          cout << "1 2 3 2" << "\n";
        } else {
          cout << "1 2 2 3" << "\n";
        }
      } else {
        if (ce3 < ce2){
          cout << "1 3 2 3" << "\n";
        } else {
          cout << "1 2 3 3" << "\n";
        }
      }
    } else {
      if (cnt2 - cnt3 == 4){
        cout << "3 3 3 3" << "\n";
      } else if (cnt2 - cnt3 == -4){
        cout << "2 2 2 2" << "\n";
      } else if (cnt2 - cnt3 == 2){
        cout << "2 3 3 3" << "\n";
      } else if (cnt2 - cnt3 == -2){
        cout << "2 2 2 3" << "\n";
      } else {
        if (S == 1){
          cout << "2 3 2 3" << "\n";
        } else {
          cout << "2 2 3 3" << "\n";
        }
      }
    }
  }
}
accuracy: 0.09090909090909091
time_ms: 60
memory_kb: 12020
final_rank: 16

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline ll time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

void solve(){
    int n,s; cin >> n >> s;
    vector<array<int, 4>> v(n);
    bool f = false;
    int dif = 0; // -(sum(2)-sum(3))
    for(int i=0;i<n;i++){
        for(int j=0;j<4;j++){
            cin >> v[i][j];
            if(v[i][j] == 2)dif--;
            else if(v[i][j] == 3)dif++;
        }
        if(v[i][0] == 1 and v[i][2] == 1){
            f = true;
        }
        if(v[i][1] == 1 and v[i][3] == 1){
            f = true;
        }
    }
    // 長方形の短い方の辺が1
    // この場合は流石に簡単なはず？
    if(f){
        // ジグソーパズル凹凸の数は等しい
        // 盗まれたピースの凹凸の差が入力から定まる
        // これ一般の場合でも言えるな
        int kado = 0;
        for(int i=0;i<n;i++){
            int cnt = 0;
            for(int j=0;j<4;j++){
                if(v[i][j] == 1)cnt++;
            }
            if(cnt == 3)kado++;
        }
        if(kado < 2){
            if(dif == 1){
                printf("1 1 1 2\n");
            }
            else{
                printf("1 1 1 3\n");
            }
        }
        else{
            if(dif == 2){
                printf("1 2 1 2\n");
            }
            else if(dif == 0){
                printf("1 2 1 3\n");
            }
            else{
                printf("1 3 1 3\n");
            }
        }
        return;
    }
    // 4つ角の場合
    {
        int kado = 0;
        for(int i=0;i<n;i++){
            int cnt = 0;
            for(int j=0;j<4;j++){
                if(v[i][j] == 1)cnt++;
            }
            if(cnt == 2)kado++;
        }
        if(kado < 4){
            if(dif == 2){
                printf("1 1 2 2\n");
            }
            else if(dif == 0){
                printf("1 1 2 3\n");
            }
            else{
                printf("1 1 3 3\n");
            }
            return;
        }
    }
    // 端の場合
    // 考えられるdifは、{3,1,-1,-3}
    // そうでない場合
    // 考えられるdifは、{4,2,0,-2,-4}

    int dd = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<4;j++){
            if(v[i][(j+1)%4] == 1 or v[i][(j+3)%4] == 1){
                if(v[i][j] == 2) dd--;
                else if(v[i][j] == 3) dd++;
            }
        }
    }

    // この偶奇で判別可能
    if(abs(dif) == 3 or abs(dif) == 1){
        if(dif == 3){
            printf("1 2 2 2\n");
        }
        else if(dif == 1){
            if(dd == 2) printf("1 2 3 2\n");
            else printf("1 2 2 3\n");
        }
        else if(dif == -1){
            if(dd == -2) printf("1 3 2 3\n");
            else printf("1 2 3 3\n");
        }
        else{
            printf("1 3 3 3\n");
        }
        return;
    }
    else{
        // printf("unsolved\n");
        if(dif == 4){
            printf("2 2 2 2\n");
        }
        else if(dif == -4){
            printf("3 3 3 3\n");
        }
        else if(dif == 2){
            printf("2 2 2 3\n");
        }
        else if(dif == -2){
            printf("2 3 3 3\n");
        }
        else{
            if(s){
                printf("2 3 2 3\n");
            }
            else{
                printf("2 2 3 3\n");
            }
        }
    }
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    while(q--){
        solve();
    }
}
accuracy: 1
time_ms: 30
memory_kb: 5928
final_rank: 2

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline ll time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

void solve(){
    int n,s; cin >> n >> s;
    vector<array<int, 4>> v(n);
    bool f = false;
    int dif = 0; // -(sum(2)-sum(3))
    for(int i=0;i<n;i++){
        for(int j=0;j<4;j++){
            cin >> v[i][j];
            if(v[i][j] == 2)dif--;
            else if(v[i][j] == 3)dif++;
        }
        if(v[i][0] == 1 and v[i][2] == 1){
            f = true;
        }
        if(v[i][1] == 1 and v[i][3] == 1){
            f = true;
        }
    }
    // 長方形の短い方の辺が1
    // この場合は流石に簡単なはず？
    if(f){
        // ジグソーパズル凹凸の数は等しい
        // 盗まれたピースの凹凸の差が入力から定まる
        // これ一般の場合でも言えるな
        int kado = 0;
        for(int i=0;i<n;i++){
            int cnt = 0;
            for(int j=0;j<4;j++){
                if(v[i][j] == 1)cnt++;
            }
            if(cnt == 3)kado++;
        }
        if(kado < 2){
            if(dif == 1){
                printf("1 1 1 2\n");
            }
            else{
                printf("1 1 1 3\n");
            }
        }
        else{
            if(dif == 2){
                printf("1 2 1 2\n");
            }
            else if(dif == 0){
                printf("1 2 1 3\n");
            }
            else{
                printf("1 3 1 3\n");
            }
        }
        return;
    }
    // 4つ角の場合
    {
        int kado = 0;
        for(int i=0;i<n;i++){
            int cnt = 0;
            for(int j=0;j<4;j++){
                if(v[i][j] == 1)cnt++;
            }
            if(cnt == 2)kado++;
        }
        if(kado < 4){
            if(dif == 2){
                printf("1 1 2 2\n");
            }
            else if(dif == 0){
                printf("1 1 2 3\n");
            }
            else{
                printf("1 1 3 3\n");
            }
            return;
        }
    }
    // 端の場合
    // 考えられるdifは、{3,1,-1,-3}
    // そうでない場合
    // 考えられるdifは、{4,2,0,-2,-4}

    int dd = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<4;j++){
            if(v[i][(j+1)%4] == 1 or v[i][(j+3)%4] == 1){
                if(v[i][j] == 2) dd--;
                else dd++;
            }
        }
    }

    // この偶奇で判別可能
    if(abs(dif) == 3 or abs(dif) == 1){
        if(dif == 3){
            printf("1 2 2 2\n");
        }
        else if(dif == 1){
            if(dd == 2) printf("1 2 3 2\n");
            else printf("1 2 2 3\n");
        }
        else if(dif == -1){
            if(dd == -2) printf("1 3 2 3\n");
            else printf("1 2 3 3\n");
        }
        else{
            printf("1 3 3 3\n");
        }
        return;
    }
    else{
        // printf("unsolved\n");
        if(dif == 4){
            printf("2 2 2 2\n");
        }
        else if(dif == -4){
            printf("3 3 3 3\n");
        }
        else if(dif == 2){
            printf("2 2 2 3\n");
        }
        else if(dif == -2){
            printf("2 3 3 3\n");
        }
        else{
            if(s){
                printf("2 3 2 3\n");
            }
            else{
                printf("2 2 3 3\n");
            }
        }
    }
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    while(q--){
        solve();
    }
}
accuracy: 0.09090909090909091
time_ms: 30
memory_kb: 5548
final_rank: 12

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline ll time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

void solve(){
    int n,s; cin >> n >> s;
    vector<array<int, 4>> v(n);
    bool f = false;
    int dif = 0; // -(sum(2)-sum(3))
    for(int i=0;i<n;i++){
        for(int j=0;j<4;j++){
            cin >> v[i][j];
            if(v[i][j] == 2)dif--;
            else if(v[i][j] == 3)dif++;
        }
        if(v[i][0] == 1 and v[i][2] == 1){
            f = true;
        }
        if(v[i][1] == 1 and v[i][3] == 1){
            f = true;
        }
    }
    // 長方形の短い方の辺が1
    // この場合は流石に簡単なはず？
    if(f){
        // ジグソーパズル凹凸の数は等しい
        // 盗まれたピースの凹凸の差が入力から定まる
        // これ一般の場合でも言えるな
        int kado = 0;
        for(int i=0;i<n;i++){
            int cnt = 0;
            for(int j=0;j<4;j++){
                if(v[i][j] == 1)cnt++;
            }
            if(cnt == 3)kado++;
        }
        if(kado < 2){
            if(dif == 1){
                printf("1 1 1 2\n");
            }
            else{
                printf("1 1 1 3\n");
            }
        }
        else{
            if(dif == 2){
                printf("1 2 1 2\n");
            }
            else if(dif == 0){
                printf("1 2 1 3\n");
            }
            else{
                printf("1 3 1 3\n");
            }
        }
        return;
    }
    // 4つ角の場合
    {
        int kado = 0;
        for(int i=0;i<n;i++){
            int cnt = 0;
            for(int j=0;j<4;j++){
                if(v[i][j] == 1)cnt++;
            }
            if(cnt == 2)kado++;
        }
        if(kado < 4){
            if(dif == 2){
                printf("1 1 2 2\n");
            }
            else if(dif == 0){
                printf("1 1 2 3\n");
            }
            else{
                printf("1 1 3 3\n");
            }
            return;
        }
    }
    // 端の場合
    // 考えられるdifは、{3,1,-1,-3}
    // そうでない場合
    // 考えられるdifは、{4,2,0,-2,-4}

    // この偶奇で判別可能
    if(abs(dif) == 3 or abs(dif) == 1){
        if(dif == 3){
            printf("1 2 2 2\n");
        }
        else if(dif == 1){
            if(s) printf("1 2 3 2\n");
            else printf("1 2 2 3\n");
        }
        else if(dif == -1){
            if(s) printf("1 3 2 3\n");
            else printf("1 2 3 3\n");
        }
        else{
            printf("1 3 3 3\n");
        }
        return;
    }
    else{
        // printf("unsolved\n");
        if(dif == 4){
            printf("2 2 2 2\n");
        }
        else if(dif == -4){
            printf("3 3 3 3\n");
        }
        else if(dif == 2){
            printf("2 2 2 3\n");
        }
        else if(dif == -2){
            printf("2 3 3 3\n");
        }
        else{
            if(s){
                printf("2 3 2 3\n");
            }
            else{
                printf("2 2 3 3\n");
            }
        }
    }
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    while(q--){
        solve();
    }
}
accuracy: 0.09090909090909091
time_ms: 30
memory_kb: 5664
final_rank: 13

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline ll time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

void solve(){
    int n,s; cin >> n >> s;
    vector<array<int, 4>> v(n);
    bool f = false;
    int dif = 0; // -(sum(2)-sum(3))
    for(int i=0;i<n;i++){
        for(int j=0;j<4;j++){
            cin >> v[i][j];
            if(v[i][j] == 2)dif--;
            else if(v[i][j] == 3)dif++;
        }
        if(v[i][0] == 1 and v[i][2] == 1){
            f = true;
        }
        if(v[i][1] == 1 and v[i][3] == 1){
            f = true;
        }
    }
    // 長方形の短い方の辺が1
    // この場合は流石に簡単なはず？
    if(f){
        // ジグソーパズル凹凸の数は等しい
        // 盗まれたピースの凹凸の差が入力から定まる
        // これ一般の場合でも言えるな
        int kado = 0;
        for(int i=0;i<n;i++){
            int cnt = 0;
            for(int j=0;j<4;j++){
                if(v[i][j] == 1)cnt++;
            }
            if(cnt == 3)kado++;
        }
        if(kado < 2){
            if(dif == 1){
                printf("1 1 1 2\n");
            }
            else{
                printf("1 1 1 3\n");
            }
        }
        else{
            if(dif == 2){
                printf("1 2 1 2\n");
            }
            else if(dif == 0){
                printf("1 2 1 3\n");
            }
            else{
                printf("1 3 1 3\n");
            }
        }
        return;
    }
    // 4つ角の場合
    {
        int kado = 0;
        for(int i=0;i<n;i++){
            int cnt = 0;
            for(int j=0;j<4;j++){
                if(v[i][j] == 1)cnt++;
            }
            if(cnt == 2)kado++;
        }
        if(kado < 4){
            if(dif == 2){
                printf("1 1 2 2\n");
            }
            else if(dif == 0){
                printf("1 1 2 3\n");
            }
            else{
                printf("1 1 3 3\n");
            }
            return;
        }
    }
    // 端の場合
    // 考えられるdifは、{3,1,-1,-3}
    // そうでない場合
    // 考えられるdifは、{4,2,0,-2,-4}

    // この偶奇で判別可能
    if(abs(dif) == 3 or abs(dif) == 1){
        if(dif == 3){
            printf("1 2 2 2\n");
        }
        else if(dif == 1){
            printf("1 2 2 3\n");
        }
        else if(dif == -1){
            printf("1 2 3 3\n");
        }
        else{
            printf("1 3 3 3\n");
        }
        return;
    }
    else{
        // printf("unsolved\n");
        if(dif == 4){
            printf("2 2 2 2\n");
        }
        else if(dif == -4){
            printf("3 3 3 3\n");
        }
        else if(dif == 2){
            printf("2 2 2 3\n");
        }
        else if(dif == -2){
            printf("2 3 3 3\n");
        }
        else{
            if(s){
                printf("2 3 2 3\n");
            }
            else{
                printf("2 2 3 3\n");
            }
        }
    }
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    while(q--){
        solve();
    }
}
accuracy: 0.09090909090909091
time_ms: 30
memory_kb: 5748
final_rank: 14

code_snippet: #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T, typename V>
void ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); }
template <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); }
template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; }
template <typename T> bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; }
int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> vector<T> sort_unique(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <typename T> int arglb(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); }
template <typename T> int argub(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T, size_t sz> ostream &operator<<(ostream &os, const array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
#if __cplusplus >= 201703L
template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }
#endif
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef HITONANODE_LOCAL
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl
#define dbgif(cond, x) ((cond) ? cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl : cerr)
#else
#define dbg(x) 0
#define dbgif(cond, x) 0
#endif

vector<int> solve() {
    int N, S;
    cin >> N >> S;
    vector A(N, vector<int>(4));
    cin >> A;
    int n2 = 0, n3 = 0, n2e = 0, n3e = 0;
    vector<int> n1cnt(5);
    for (auto a : A) {
        int n1 = count(ALL(a), 1);
        n1cnt[n1]++;
        REP(t, 2) {
            REP(d, 4) {
                if (a[d] == 1) {
                    int e = (d + 1) % 4;
                    if (a[e] == 2) n2e++;
                    if (a[e] == 3) n3e++;
                }
            }
            reverse(ALL(a));
        }
        for (auto x : a) {
            if (x == 2) {
                n2++;
            }
            if (x == 3) {
                n3++;
            }
        }
    }
    if (n1cnt[3] % 2) {
        vector<int> ret{1, 1, 1};
        ret.push_back(n2 < n3 ? 2 : 3);
        return ret;
    }

    if (n1cnt[3]) {
        if (n2 == n3) return {1, 2, 1, 3};
        if (n2 < n3) return {1, 2, 1, 2};
        if (n2 > n3) return {1, 3, 1, 3};
        throw;
    }

    if (n1cnt[2] % 2) {
        vector<int> ret{1, 1};
        if (n2e == n3e) ret.push_back(2), ret.push_back(3);
        if (n2e < n3e) ret.push_back(2), ret.push_back(2);
        if (n2e > n3e) ret.push_back(3), ret.push_back(3);
        return ret;
    }
    if (n1cnt[1] % 2) {
        vector<int> ret{1, 0, 0, 0};
        if (n2e == n3e) ret[1] = 2, ret[3] = 3, n2 += 1, n3 += 1;
        if (n2e < n3e) ret[1] = 2, ret[3] = 2, n2 += 2;
        if (n2e > n3e) ret[1] = 3, ret[3] = 3, n3 += 2;
        ret[2] = (n2 < n3 ? 2 : 3);
        return ret;
    }

    if (abs(n2 - n3) == 4) return vector<int>(4, n2 < n3 ? 2 : 3);
    if (abs(n2 - n3) == 2) {
        vector<int> ret{2, 0, 0, 3};
        ret[1] = ret[2] = (n2 < n3 ? 2 : 3);
        return ret;
    }
    if (n2 == n3) {
        if (S) return {2, 3, 2, 3};
        else return {2, 2, 3, 3};
    }
    throw;
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        auto ret = solve();
        REP(i, ret.size()) cout << ret[i] << (i + 1 == int(ret.size()) ? '\n' : ' ');
    }
}
accuracy: 1
time_ms: 40
memory_kb: 13468
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={0,1,-1,0,1,1,-1,-1,0};
const ll dx[9]={1,0,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
void output(vl v){
    vl ans=v;
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    swap(v[0],v[2]);
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    rep(i,4){
        if(i)cout << " ";
        cout << ans[i];
    }
    cout << endl;
}
vl trans(vl v){
    vl ans=v;
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    swap(v[0],v[2]);
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    return ans;
}
void solve(){
    ll n;cin >> n;ll hoge;cin >> hoge;
    vvl v(n,vl(4));
    ll onemax=0;
    rep(i,n){
        rep(j,4)cin >> v[i][j];
        ll one=0;
        rep(j,4)if(v[i][j]==1)one++;
        chmax(onemax,one);
        v[i]=trans(v[i]);
    }
    if(onemax==3){//1*(N+1)の盤面
        ll thcnt=0;
        ll vx=0,cv=0;
        rep(i,n){
            ll one=0;
            rep(j,4){
                if(v[i][j]==1)one++;
                if(v[i][j]==2)vx++;
                if(v[i][j]==3)cv++;
            }
            if(one==3)thcnt++;
        }
        if(thcnt==1){
            if(vx<cv)output({1,1,1,2});
            else output({1,1,1,3});
            return;
        }
        else{
            if(vx<cv)output({1,2,1,2});
            if(vx>cv)output({1,3,1,3});
            if(vx==cv)output({1,2,1,3});
            return;
        }
    }
    //
    ll twocnt=0,onecnt=0;
    ll vx=0,cv=0;
    rep(i,n){
        ll one=0;
        rep(j,4){
            if(v[i][j]==1)one++;
            if(v[i][j]==2)vx++;
            if(v[i][j]==3)cv++;
        }
        if(one==2)twocnt++;
        if(one==1)onecnt++;
    }
    if(twocnt==3){
        if(vx<cv)output({1,1,2,2});
        if(vx>cv)output({1,1,3,3});
        if(vx==cv)output({1,1,2,3});
        return;
    }
    if(onecnt&1){//辺を入れる
        //cout << 999 << endl;
        ll nvx=0,ncv=0;
        rep(i,n){
            ll one=0;
            rep(j,4){
                if(v[i][j]==1)one++;
            }
            if(one==1){
                //rep(j,4)cout << v[i][j] <<" ";cout << endl;
                vl idx={1,3};
                assert(v[i][0]==1);
                for(auto p:idx){
                    if(v[i][p]==2)nvx++;
                    if(v[i][p]==3)ncv++;
                }
            }
            if(one==2){
                //rep(j,4)cout << v[i][j] <<" ";cout << endl;
                vl idx={2,3};
                assert(v[i][0]==1);
                for(auto p:idx){
                    if(v[i][p]==2)nvx++;
                    if(v[i][p]==3)ncv++;
                }
            }
        }
        //cout << nvx <<" " << ncv << endl;
        vl ans={1,0,0,0};
        if(nvx<ncv){
            vx+=2;ans[1]=2;ans[3]=2;
        }
        else if(nvx>ncv){
            cv+=2;ans[1]=3;ans[3]=3;
        }
        else{
            ans[1]=2;ans[3]=3;
        }
        if(vx<cv)ans[2]=2;
        if(vx>cv)ans[2]=3;
        assert(ans[2]!=0);
        output(ans);
        return;
    }
    if(cv==vx){
        if(hoge==0)output({2,2,3,3});
        else output({2,3,2,3});
    }
    else if(vx<cv){
        if(vx+4==cv)output({2,2,2,2});
        else output({2,2,2,3});
    }
    else{
        if(vx==cv+4)output({3,3,3,3});
        else output({2,3,3,3});
    }
}
int main(){
    ll t;cin >> t;
    while(t--)solve();
}
accuracy: 1
time_ms: 100
memory_kb: 16476
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={0,1,-1,0,1,1,-1,-1,0};
const ll dx[9]={1,0,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
void output(vl v){
    vl ans=v;
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    swap(v[0],v[2]);
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    rep(i,4){
        if(i)cout << " ";
        cout << ans[i];
    }
    cout << endl;
}
vl trans(vl v){
    vl ans=v;
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    swap(v[0],v[2]);
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    return ans;
}
void solve(){
    ll n;cin >> n;ll hoge;cin >> hoge;
    vvl v(n,vl(4));
    ll onemax=0;
    rep(i,n){
        rep(j,4)cin >> v[i][j];
        ll one=0;
        rep(j,4)if(v[i][j]==1)one++;
        chmax(onemax,one);
        v[i]=trans(v[i]);
    }
    if(onemax==3){//1*(N+1)の盤面
        ll thcnt=0;
        ll vx=0,cv=0;
        rep(i,n){
            ll one=0;
            rep(j,4){
                if(v[i][j]==1)one++;
                if(v[i][j]==2)vx++;
                if(v[i][j]==3)cv++;
            }
            if(one==3)thcnt++;
        }
        if(thcnt==1){
            if(vx<cv)output({1,1,1,2});
            else output({1,1,1,3});
            return;
        }
        else{
            if(vx<cv)output({1,2,1,2});
            if(vx>cv)output({1,3,1,3});
            if(vx==cv)output({1,2,1,3});
            return;
        }
    }
    //
    ll twocnt=0,onecnt=0;
    ll vx=0,cv=0;
    rep(i,n){
        ll one=0;
        rep(j,4){
            if(v[i][j]==1)one++;
            if(v[i][j]==2)vx++;
            if(v[i][j]==3)cv++;
        }
        if(one==2)twocnt++;
        if(one==1)onecnt++;
    }
    if(twocnt==3){
        if(vx<cv)output({1,1,2,2});
        if(vx>cv)output({1,1,3,3});
        if(vx==cv)output({1,1,2,3});
        return;
    }
    if(onecnt&1){//辺を入れる
        ll nvx=0,ncv=0;
        rep(i,n){
            ll one=0;
            rep(j,4){
                if(v[i][j]==1)one++;
            }
            if(one==1){
                vl idx={1,3};
                assert(v[i][0]==1);
                for(auto p:idx){
                    if(v[i][p]==2)nvx++;
                    if(v[i][p]==3)ncv++;
                }
            }
            if(one==2){
                vl idx={3,4};
                assert(v[i][0]==1);
                for(auto p:idx){
                    if(v[i][p]==2)nvx++;
                    if(v[i][p]==3)ncv++;
                }
            }
        }
        vl ans={1,0,0,0};
        if(nvx<ncv){
            vx+=2;ans[1]=2;ans[3]=2;
        }
        else if(nvx>ncv){
            cv+=2;ans[1]=3;ans[3]=3;
        }
        else{
            ans[1]=2;ans[3]=3;
        }
        if(vx<cv)ans[2]=2;
        if(vx>cv)ans[2]=3;
        assert(ans[2]!=0);
        output(ans);
        return;
    }
    if(cv==vx){
        if(hoge==0)output({2,2,3,3});
        else output({2,3,2,3});
    }
    else if(vx<cv){
        if(vx+4==cv)output({2,2,2,2});
        else output({2,2,2,3});
    }
    else{
        if(vx==cv+4)output({3,3,3,3});
        else output({2,3,3,3});
    }
}
int main(){
    ll t;cin >> t;
    while(t--)solve();
}
accuracy: 0.09090909090909091
time_ms: 120
memory_kb: 14368
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={0,1,-1,0,1,1,-1,-1,0};
const ll dx[9]={1,0,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
void output(vl v){
    vl ans=v;
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    swap(v[0],v[2]);
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    rep(i,4){
        if(i)cout << " ";
        cout << ans[i];
    }
    cout << endl;
}
vl trans(vl v){
    vl ans=v;
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    swap(v[0],v[2]);
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    return ans;
}
void solve(){
    ll n;cin >> n;ll hoge;cin >> hoge;
    vvl v(n,vl(4));
    ll onemax=0;
    rep(i,n){
        rep(j,4)cin >> v[i][j];
        ll one=0;
        rep(j,4)if(v[i][j]==1)one++;
        chmax(onemax,one);
        v[i]=trans(v[i]);
    }
    if(onemax==3){//1*(N+1)の盤面
        ll thcnt=0;
        ll vx=0,cv=0;
        rep(i,n){
            ll one=0;
            rep(j,4){
                if(v[i][j]==1)one++;
                if(v[i][j]==2)vx++;
                if(v[i][j]==3)cv++;
            }
            if(one==3)thcnt++;
        }
        if(thcnt==1){
            if(vx<cv)output({1,1,1,2});
            else output({1,1,1,3});
            return;
        }
        else{
            if(vx<cv)output({1,2,1,2});
            if(vx>cv)output({1,3,1,3});
            if(vx==cv)output({1,2,1,3});
            return;
        }
    }
    //
    ll twocnt=0,onecnt=0;
    ll vx=0,cv=0;
    rep(i,n){
        ll one=0;
        rep(j,4){
            if(v[i][j]==1)one++;
            if(v[i][j]==2)vx++;
            if(v[i][j]==3)cv++;
        }
        if(one==2)twocnt++;
        if(one==1)onecnt++;
    }
    if(twocnt==3){
        if(vx<cv)output({1,1,2,2});
        if(vx>cv)output({1,1,3,3});
        if(vx==cv)output({1,1,2,3});
        return;
    }
    if(onecnt&1){//辺を入れる
        ll nvx=0,ncv=0;
        rep(i,n){
            ll one=0;
            rep(j,4){
                if(v[i][j]==1)one++;
            }
            if(one==1){
                vl idx={1,3};
                for(auto p:idx){
                    if(v[i][p]==2)nvx++;
                    if(v[i][p]==3)ncv++;
                }
            }
            if(one==2){
                vl idx={3,4};
                for(auto p:idx){
                    if(v[i][p]==2)nvx++;
                    if(v[i][p]==3)ncv++;
                }
            }
        }
        vl ans={1,0,0,0};
        if(nvx<ncv){
            vx+=2;ans[1]=2;ans[3]=2;
        }
        else if(nvx>ncv){
            cv+=2;ans[1]=3;ans[3]=3;
        }
        else{
            ans[1]=2;ans[3]=3;
        }
        if(vx<cv)ans[2]=2;
        if(vx>cv)ans[2]=3;
        assert(ans[2]!=0);
        output(ans);
        return;
    }
    if(cv==vx){
        if(hoge==0)output({2,2,3,3});
        else output({2,3,2,3});
    }
    else if(vx<cv){
        if(vx+4==cv)output({2,2,2,2});
        else output({2,2,2,3});
    }
    else{
        if(vx==cv+4)output({3,3,3,3});
        else output({2,3,3,3});
    }
}
int main(){
    ll t;cin >> t;
    while(t--)solve();
}
accuracy: 0.09090909090909091
time_ms: 100
memory_kb: 14188
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={0,1,-1,0,1,1,-1,-1,0};
const ll dx[9]={1,0,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
void output(vl v){
    vl ans=v;
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    swap(v[0],v[2]);
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    rep(i,4){
        if(i)cout << " ";
        cout << ans[i];
    }
    cout << endl;
}
vl trans(vl v){
    vl ans=v;
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    swap(v[0],v[2]);
    rep(i,4){
        ll px=v[3];
        per(j,3)v[j+1]=v[j];
        v[0]=px;
        chmin(ans,v);
    }
    return ans;
}
void solve(){
    ll n;cin >> n;ll hoge;cin >> hoge;
    vvl v(n,vl(4));
    ll onemax=0;
    rep(i,n){
        rep(j,4)cin >> v[i][j];
        ll one=0;
        rep(j,4)if(v[i][j]==1)one++;
        chmax(onemax,one);
        v[i]=trans(v[i]);
    }
    if(onemax==3){//1*(N+1)の盤面
        ll thcnt=0;
        ll vx=0,cv=0;
        rep(i,n){
            ll one=0;
            rep(j,4){
                if(v[i][j]==1)one++;
                if(v[i][j]==2)vx++;
                if(v[i][j]==3)cv++;
            }
            if(one==3)thcnt++;
        }
        if(thcnt==1){
            if(vx<cv)output({1,1,1,2});
            else output({1,1,1,3});
            return;
        }
        else{
            if(vx<cv)output({1,2,1,2});
            if(vx>cv)output({1,3,1,3});
            if(vx==cv)output({1,2,1,3});
            return;
        }
    }
    //
    ll twocnt=0,onecnt=0;
    ll vx=0,cv=0;
    rep(i,n){
        ll one=0;
        rep(j,4){
            if(v[i][j]==1)one++;
            if(v[i][j]==2)vx++;
            if(v[i][j]==3)cv++;
        }
        if(one==2)twocnt++;
        if(one==1)onecnt++;
    }
    if(twocnt==3){
        if(vx<cv)output({1,1,2,2});
        if(vx>cv)output({1,1,3,3});
        if(vx==cv)output({1,1,2,3});
        return;
    }
    if(onecnt&1){//辺を入れる
        ll nvx=0,ncv=0;
        rep(i,n){
            ll one=0;
            rep(j,4){
                if(v[i][j]==1)one++;
            }
            if(one==1){
                vl idx={1,3};
                for(auto p:idx){
                    if(v[i][p]==2)nvx++;
                    if(v[i][p]==3)ncv++;
                }
            }
            if(one==2){
                vl idx={3,4};
                for(auto p:idx){
                    if(v[i][p]==2)nvx++;
                    if(v[i][p]==3)ncv++;
                }
            }
        }
        vl ans={1,0,0,0};
        if(nvx<ncv){
            vx+=2;ans[1]=2;ans[3]=2;
        }
        else if(nvx>ncv){
            cv+=2;ans[1]=3;ans[3]=3;
        }
        else{
            ans[1]=2;ans[3]=3;
        }
        if(vx<cv)ans[2]=2;
        if(vx>cv)ans[2]=3;
        output(ans);
        return;
    }
    if(cv==vx){
        if(hoge==0)output({2,2,3,3});
        else output({2,3,2,3});
    }
    else if(vx<cv){
        if(vx+4==cv)output({2,2,2,2});
        else output({2,2,2,3});
    }
    else{
        if(vx==cv+4)output({3,3,3,3});
        else output({2,3,3,3});
    }
}
int main(){
    ll t;cin >> t;
    while(t--)solve();
}
accuracy: 0.09090909090909091
time_ms: 100
memory_kb: 14328
final_rank: 19

code_snippet: /**
 *    author:  otera
**/
#include<bits/stdc++.h>
using namespace std;

#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using int128_t = __int128_t;
#define repa(i, n) for(int i = 0; i < n; ++ i)
#define repb(i, a, b) for(int i = a; i < b; ++ i)
#define repc(i, a, b, c) for(int i = a; i < b; i += c)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define rep(...) overload4(__VA_ARGS__, repc, repb, repa)(__VA_ARGS__)
#define rep1a(i, n) for(int i = 0; i <= n; ++ i)
#define rep1b(i, a, b) for(int i = a; i <= b; ++ i)
#define rep1c(i, a, b, c) for(int i = a; i <= b; i += c)
#define rep1(...) overload4(__VA_ARGS__, rep1c, rep1b, rep1a)(__VA_ARGS__)
#define rev_repa(i, n) for(int i=n-1;i>=0;i--)
#define rev_repb(i, a, b) assert(a > b);for(int i=a;i>b;i--)
#define rev_rep(...) overload3(__VA_ARGS__, rev_repb, rev_repa)(__VA_ARGS__)
#define rev_rep1a(i, n) for(int i=n;i>=1;i--)
#define rev_rep1b(i, a, b) assert(a >= b);for(int i=a;i>=b;i--)
#define rev_rep1(...) overload3(__VA_ARGS__, rev_rep1b, rev_rep1a)(__VA_ARGS__)
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(), c.rend()
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define Sort(a) sort(all(a))
#define Rev(a) reverse(all(a))
#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define si(c) (int)(c).size()
inline ll popcnt(ull a){ return __builtin_popcountll(a); }
#define kth_bit(x, k) ((x>>k)&1)
#define unless(A) if(!(A))
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll intpow(ll a, ll b, ll m) {ll ans = 1; while(b){ if(b & 1) (ans *= a) %= m; (a *= a) %= m; b /= 2; } return ans; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)
#define vec(type,name,...) vector<type>name(__VA_ARGS__)
#define VEC(type,name,size) vector<type>name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)
#define vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vc<T>>;
template <class T> using vvvc = vector<vvc<T>>;
template <class T> using vvvvc = vector<vvvc<T>>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template<class T> void scan(T& a){ cin >> a; }
template<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){ scan(head); in(tail...); }
void print(){ cout << ' '; }
template<class T> void print(const T& a){ cout << a; }
template<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout << ' '; print(*i); } }
int out(){ cout << '\n'; return 0; }
template<class T> int out(const T& t){ print(t); cout << '\n'; return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); cout << ' '; out(tail...); return 0; }
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#define dump(...) { print(#__VA_ARGS__); print(":"); out(__VA_ARGS__); }
#else
#define debug(...)
#define dump(...)
#endif

struct io_setup {
    io_setup(int precision = 20) {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout << fixed << setprecision(precision);
    }
} io_setup_ {};

void solve() {
    INT(n, s);
    VV(int, a, n, 4);
    vc<int> cnt(4, 0);
    int cnt11 = 0, cnt111 = 0;
    vc<int> cnt1(4, 0);
    rep(i, n) {
        int co1 = 0;
        int pos1 = -1;
        rep(j, 4) {
            cnt[a[i][j]] ++;
            if(a[i][j] == 1) {
                ++ co1;
                pos1 = j;
            }
        }
        if(co1 == 1) {
            int nj = (pos1 + 2) % 4;
            rep(j, 4) {
                if(j == nj) continue;
                cnt1[a[i][j]] ++;
            }
        }
        if(co1 == 2) {
            ++ cnt11;
            rep(j, 4) {
                if(a[i][j] == 1) continue;
                cnt1[a[i][j]] ++;
            }
        }
        if(co1 == 3) ++ cnt111;
    }
    int diff = cnt[3] - cnt[2];
    for(int x = 1; x <= n + 1; ++ x) {
        unless((n + 1) % x == 0) continue;
        int y = (n + 1) / x;
        int res1 = 2 * (x + y) - cnt[1];
        if(res1 == 3) {
            unless(x == 1 or y == 1) continue;
            unless(cnt111 == 1) continue;
        }
        if(res1 == 2) {
            if(x == 1 or y == 1) {
                unless(cnt111 == 2) continue;
            } else {
                unless(cnt11 == 3) continue;
            }
        }
        if(res1 == 1) {
            if(x == 1 or y == 1) continue;
            else {
                unless(cnt11 == 4) continue;
            }
        }
        if(res1 < 0 or res1 >= 4) continue;
        int sum = 4 - res1;
        unless((sum - diff) % 2 == 0) continue;
        int res2 = (sum - diff) / 2 + diff;
        int res3 = (sum - diff) / 2;
        if(res2 < 0 or res3 < 0) continue;
        vc<int> ids(4);
        iota(all(ids), 0);
        vc<int> ans = vc<int>{4, 4, 4, 4};
        vc<int> now;
        rep(_, res1) now.eb(1);
        rep(_, res2) now.eb(2);
        rep(_, res3) now.eb(3);
        assert(si(now) == 4);
        dump(now);
        do {
            vc<int> res;
            vc<int> pos1;
            rep(i, 4) {
                res.eb(now[ids[i]]);
                if(now[ids[i]] == 1) {
                    pos1.eb(i);
                }
            }
            int curs = (res[0] == res[2] and res[1] == res[3] ? 1 : 0);
            if(res1 == 2) {
                if(x == 1 or y == 1) {
                    if(abs(pos1[0] - pos1[1]) != 2) continue;
                } else {
                    if(abs(pos1[0] - pos1[1]) != 1) continue;
                }
            }
            if(res1 == 1 and (res2 == 1 or res2 == 2)) {
                if(res[0] != 1) continue;
                int diff1 = cnt1[3] - cnt1[2];
                if(diff1 == 0) {
                    if(res[1] == res[3]) continue;
                } else {
                    assert(abs(diff1) == 2);
                    if(res[1] != res[3]) continue;
                }
            }
            if(curs == s) {
                if(chmin(ans, res)) ans = res;
            }
        } while(next_permutation(all(ids)));
        if(ans[0] != 4) {
            out(ans);
            return;
        }
    }
    assert(false);
}

signed main() {
    int testcase = 1;
    in(testcase);
    while(testcase--) solve();
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 16472
final_rank: 5