Problem:
Problem F:
Dice Puzzle
Let’s try a dice puzzle. The rules of this puzzle are as follows.
Dice with six faces as shown in Figure 1 are used in the puzzle.
Figure 1: Faces of a die
With twenty seven such dice, a 3 × 3 × 3 cube is built as shown in Figure 2.
Figure 2: 3 × 3 × 3 cube
When building up a cube made of dice, the sum of the numbers marked on the faces of adjacent dice that are placed against each other must be seven (See Figure 3). For example, if one face of the pair is marked “2”, then the other face must be “5”.
Figure 3: A pair of faces placed against each other
The top and the front views of the cube are partially given, i.e. the numbers on faces of
some of the dice on the top and on the front are given.
Figure 4: Top and front views of the cube
The goal of the puzzle is to find all the plausible dice arrangements that are consistent
with the given top and front view information.
Your job is to write a program that solves this puzzle.
Input
The input consists of multiple datasets in the following format.
N
Dataset
1
Dataset
2
...
Dataset
N
N
is the number of the datasets.
The format of each dataset is as follows.
T
11
T
12
T
13
T
21
T
22
T
23
T
31
T
32
T
33
F
11
F
12
F
13
F
21
F
22
F
23
F
31
F
32
F
33
T
ij
and
F
ij
(1 ≤
i
≤ 3, 1 ≤
j
≤ 3) are the faces of dice appearing on the top and front views,
as shown in Figure 2, or a zero. A zero means that the face at the corresponding position is
unknown.
Output
For each plausible arrangement of dice, compute the sum of the numbers marked on the nine
faces appearing on the right side of the cube, that is, with the notation given in Figure 2,

∑
3
i
=1
∑
3
j
=1
R
ij
.
For each dataset, you should output the right view sums for all the plausible arrangements, in
ascending order and without duplicates. Numbers should be separated by a single space.
When there are no plausible arrangements for a dataset, output a zero.
For example, suppose that the top and the front views are given as follows.
Figure 5: Example
There are four plausible right views as shown in Figure 6. The right view sums are 33, 36, 32,
and 33, respectively. After rearranging them into ascending order and eliminating duplicates,
the answer should be “32 33 36”.
Figure 6: Plausible right views
The output should be one line for each dataset. The output may have spaces at ends of lines.
Sample Input
4
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
2 2 2
4 3 3
5 2 2
4 3 3
6 1 1
6 1 1
6 1 0
1 0 0
0 2 0
0 0 0
5 1 2
5 1 2
0 0 0
2 0 0
0 3 0
0 0 0
0 0 0
0 0 0
3 0 1
Output for the Sample Input
27
24
32 33 36
0

Submitted solutions:

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

enum POS {
    TOP = 1,
    FRONT,
    RIGHT,
    LEFT,
    BACK,
    BOTTOM
};

// dice[top][front][pos];
int dice[7][7][7];

void init() {
    int right[7][7] = {};
    right[1][2] = 3; right[1][3] = 5; right[1][4] = 2; right[1][5] = 4;
    right[2][1] = 4; right[2][3] = 1; right[2][4] = 6; right[2][6] = 3;
    right[3][1] = 2; right[3][2] = 6; right[3][5] = 1; right[3][6] = 5;
    right[4][1] = 5; right[4][2] = 1; right[4][5] = 6; right[4][6] = 2;
    right[5][1] = 3; right[5][3] = 6; right[5][4] = 1; right[5][6] = 4;
    right[6][2] = 4; right[6][3] = 2; right[6][4] = 5; right[6][5] = 3;
    for(int i=1; i<=6; ++i) {
        for(int j=1; j<=6; ++j) {
            dice[i][j][TOP] = i;
            dice[i][j][FRONT] = j;
            dice[i][j][RIGHT] = right[i][j];
            dice[i][j][BOTTOM] = 7 - dice[i][j][TOP];
            dice[i][j][BACK] = 7 - dice[i][j][FRONT];
            dice[i][j][LEFT] = 7 - dice[i][j][RIGHT];
        }
    }
}

using pii = pair<int, int>;

int calc(vector<pii> const& cube) {
    int res = 0;
    for(int i=0; i<3; ++i) {
        for(int j=0; j<3; ++j) {
            auto p = cube[i*9 + j*3 + 2];
            res += dice[p.first][p.second][RIGHT];
        }
    }
    return res;
}

void solve(int d, vector<pii> const& init, vector<pii>& cube, vector<int>& res) {
    if(d == 27) {
        res.push_back(calc(cube));
        return;
    }
    for(int i=1; i<=6; ++i) {
        for(int j=1; j<=6; ++j) {
            if(i == j || i + j == 7) {
                continue;
            }
            if(init[d].first != 0 && init[d].first != i) {
                continue;
            }
            if(init[d].second != 0 && init[d].second != j) {
                continue;
            }
            if((d / 3) % 3 != 0) { // not back side of cube
                if(dice[i][j][BACK] + cube[d-3].second != 7) {
                    continue;
                }
            }
            if(d % 3 != 0) { // not left side of cube
                if(dice[i][j][LEFT] + dice[cube[d-1].first][cube[d-1].second][RIGHT] != 7) {
                    continue;
                }
            }
            if(d >= 9) { // not top of cube
                if(i + dice[cube[d-9].first][cube[d-9].second][BOTTOM] != 7) {
                    continue;
                }
            }
            cube[d].first = i;
            cube[d].second = j;
            solve(d+1, init, cube, res);
        }
    }
}

int main() {
    int N;
    cin >> N;
    init();
    while(N--) {
        vector<pii> cube(27);
        for(int i=0; i<9; ++i) {
            cin >> cube[i].first;
        }
        for(int i=0; i<3; ++i) {
            for(int j=0; j<3; ++j) {
                cin >> cube[i*9 + 6 + j].second;
            }
        }
        auto init = cube;
        vector<int> res;
        solve(0, init, cube, res);
        if(res.empty()) {
            cout << 0 << endl;
            continue;
        }
        sort(res.begin(), res.end());
        res.erase(unique(res.begin(), res.end()), res.end());
        for(int i=0; i<res.size(); ++i) {
            cout << res[i] << " \n"[i == res.size()-1] << flush;
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3428
final_rank: 7

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

using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)

vector<array<int,6>> D;
int T[3][3],F[3][3];

set<int> S;
void dfs(int n,array<int,27>&d){
    if(n==27){
        int sm=0;
        rep(k,9)sm+=D[d[3*k+2]][2];
        S.insert(sm);
        return;
    }

    rep(p,24){
        bool OK=1;
        if(n%3!=0&&D[d[n-1]][2]+D[p][3]!=7)OK=0;
        if(n>8&&D[d[n-9]][5]+D[p][0]!=7)OK=0;
        if(n%9>2&&D[d[n-3]][1]+D[p][4]!=7)OK=0;
        if(n%9>5){
            int i=n/9,j=n%9-6;
            if(F[i][j]!=0&&F[i][j]!=D[p][1])OK=0;
        }
        if(n<9){
            int i=n/3,j=n%3;
            if(T[i][j]!=0&&T[i][j]!=D[p][0])OK=0;
        }
        if(OK){
            d[n]=p;
            dfs(n+1,d);
        }
    }
}

void solve(){
    rep(i,3)rep(j,3)cin>>T[i][j];
    rep(i,3)rep(j,3)cin>>F[i][j];

    array<int,27> d;
    S.clear();
    dfs(0,d);
    if(S.size()==0)S.insert(0);
    for(int s:S)cout<<s<<" \n"[s==(*(prev(S.end())))];
}

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

    array<int,6> P;
    rep(i,6)P[i]=i+1;
    do{
        if(P[0]+P[5]!=7||P[1]+P[4]!=7||P[2]+P[3]!=7)continue;
        auto Q=P;
        if(Q[0]>3){
            swap(Q[0],Q[5]);
            swap(Q[2],Q[3]);
        }
        while(Q[1]>3||Q[2]>3){
            auto NQ=Q;
            Q[1]=NQ[2];
            Q[2]=NQ[4];
            Q[4]=NQ[3];
            Q[3]=NQ[1];
        }
        if((Q[0]-Q[1]+3)%3!=2||(Q[1]-Q[2]+3)%3!=2||(Q[2]-Q[0]+3)%3!=2)continue;
        D.push_back(P);
    }while(next_permutation(P.begin(),P.end()));
    
    int N;
    cin>>N;
    rep(i,N)solve();

}
accuracy: 1
time_ms: 10
memory_kb: 3376
final_rank: 4

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

using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)


vector<array<int,6>> D;
int T[3][3],F[3][3];

set<int> S;

void dfs(int n,array<int,27>&d){
    if(n==27){
        
        // rep(i,27)cout<<d[i]<<" \n"[i==26];
        int sm=0;
        rep(k,9)sm+=D[d[3*k+2]][2];
        S.insert(sm);
        return;
    }

    rep(p,24){
        bool OK=1;
        if(n%3!=0){
            if(D[d[n-1]][2]+D[p][3]!=7)OK=0;
        }
        if(n>8){
            if(D[d[n-9]][5]+D[p][0]!=7)OK=0;
        }
        if(n%9>2){
            if(D[d[n-3]][1]+D[p][4]!=7)OK=0;
        }
        if(n%9>5){
            int i=n/9;
            int j=n%9-6;
            if(F[i][j]!=0&&F[i][j]!=D[p][1])OK=0;
        }
        if(n<9){
            int i=n/3;
            int j=n%3;
            if(T[i][j]!=0&&T[i][j]!=D[p][0])OK=0;
        }
        if(OK){
            d[n]=p;
            dfs(n+1,d);
            d[n]=-1;
        }
        
    }
}

void solve(){
    rep(i,3)rep(j,3)cin>>T[i][j];
    rep(i,3)rep(j,3)cin>>F[i][j];

    array<int,27> d;
    S.clear();
    dfs(0,d);
    if(S.size()==0)S.insert(0);
    for(int s:S)cout<<s<<" \n"[s==(*(prev(S.end())))];

    
}

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

    array<int,6> P;
    rep(i,6)P[i]=i+1;
    do{
        if(P[0]+P[5]!=7||P[1]+P[4]!=7||P[2]+P[3]!=7)continue;
        auto Q=P;
        if(Q[0]>3){
            swap(Q[0],Q[5]);
            // swap(Q[1],Q[4]);
            swap(Q[2],Q[3]);
        }
        while(Q[1]>3||Q[2]>3){
            auto NQ=Q;
            Q[1]=NQ[2];
            Q[2]=NQ[4];
            Q[4]=NQ[3];
            Q[3]=NQ[1];
        }
        if((Q[0]-Q[1]+3)%3!=2||(Q[1]-Q[2]+3)%3!=2||(Q[2]-Q[0]+3)%3!=2)continue;
        D.push_back(P);
    }while(next_permutation(P.begin(),P.end()));

    // rep(i,24)rep(j,6)cout<<D[i][j]<<" \n"[j==5];
    int N;
    cin>>N;
    rep(i,N)solve();

}
accuracy: 1
time_ms: 10
memory_kb: 3384
final_rank: 5

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

template < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }
template < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }
template < class T > vector< T >  operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }
template < class T > vector< T >  operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

// https://ei1333.github.io/luzhiled/snippets/other/dice.html
struct Dice
{
  // int x, y;
  int l, r, f, b, d, u;

  void RollN()
  {
    //  --y;
    int buff = d;
    d = f;
    f = u;
    u = b;
    b = buff;
  }

  void RollS()
  {
    // ++y;
    int buff = d;
    d = b;
    b = u;
    u = f;
    f = buff;
  }

  void RollL() // ----->
  {
    int buff = f;
    f = l;
    l = b;
    b = r;
    r = buff;
  }

  void RollR() // <------
  {
    int buff = f;
    f = r;
    r = b;
    b = l;
    l = buff;
  }

  void RollE() // .o -> o.
  {
    // --x;
    int buff = d;
    d = l;
    l = u;
    u = r;
    r = buff;
  }


  void RollW() // o. -> .o
  {
    //  ++x;
    int buff = d;
    d = r;
    r = u;
    u = l;
    l = buff;
  }


  vector< Dice > makeDice()
  {
    vector< Dice > ret;
    for(int i = 0; i < 6; i++) {
      Dice d(*this);
      if(i == 1) d.RollN();
      if(i == 2) d.RollS();
      if(i == 3) d.RollS(), d.RollS();
      if(i == 4) d.RollL();
      if(i == 5) d.RollR();
      for(int j = 0; j < 4; j++) {
        ret.emplace_back(d);
        d.RollE();
      }
    }
    return (ret);
  }
};

int main() {
    using men = vector<vector<int>>;
    vector<tuple<men, men, int, men>> A; {
        vector getF(7, vector(7, -1));
        vector getT(7, vector(7, -1));
        vector getR(7, vector(7, -1));
        vector<vector<pair<int,int>>> invF(7), invT(7), invR(7);
        Dice D{4, 3, 2, 5, 6, 1};
        for(Dice d : D.makeDice()) {
            getF[d.u][d.r] = d.f; invF[d.f].push_back({d.u, d.r});
            getT[d.f][d.r] = d.u; invT[d.u].push_back({d.f, d.r});
            getR[d.f][d.u] = d.r; invR[d.r].push_back({d.f, d.u});
        }

        vector F(4, vector(4, -1));
        vector T(4, vector(4, -1));
        vector R(4, vector(4, -1));
        for(R[1][1] = 1; R[1][1] <= 6; R[1][1]++) {
            for(auto a : invR[R[1][1]]) { tie(F[1][3], T[3][3]) = a;
                for(auto b : invR[R[1][1]]) { tie(F[1][2], T[3][2]) = b;
                    for(auto c : invR[R[1][1]]) { tie(F[1][1], T[3][1]) = c;
                        for(auto d : invT[T[3][3]]) { tie(F[2][3], R[2][1]) = d;
                            for(auto e : invT[T[3][3]]) { tie(F[3][3], R[3][1]) = e;
                                for(auto f : invF[F[1][3]]) { tie(T[2][3], R[1][2]) = f;
                                    for(auto g : invF[F[1][3]]) { tie(T[1][3], R[1][3]) = g;
                                        T[1][1] = getT[F[1][1]][R[1][3]];
                                        T[2][1] = getT[F[1][1]][R[1][2]];
                                        T[1][2] = getT[F[1][2]][R[1][3]];
                                        T[2][2] = getT[F[1][2]][R[1][2]];

                                        F[2][1] = getF[T[3][1]][R[2][1]];
                                        F[3][1] = getF[T[3][1]][R[3][1]];
                                        F[2][2] = getF[T[3][2]][R[2][1]];
                                        F[3][2] = getF[T[3][2]][R[3][1]];

                                        R[2][2] = getR[F[2][3]][T[2][3]];
                                        R[3][2] = getR[F[3][3]][T[2][3]];
                                        R[2][3] = getR[F[2][3]][T[1][3]];
                                        R[3][3] = getR[F[3][3]][T[1][3]];

                                        int sumR = 0;
                                        for(int i = 1; i <= 3; i++) {
                                            for(int j = 1; j <= 3; j++) {
                                                sumR += R[i][j];
                                            }
                                        }

                                        bool ok = [&] {
                                            for(int i = 1; i <= 3; i++) {
                                                for(int j = 1; j <= 3; j++) {
                                                    if(F[i][j] == -1 or T[i][j] == -1 or R[i][j] == -1) return false;
                                                }
                                            }
                                            for(int i = 1; i <= 3; i++) {
                                                if(getT[F[i][1]][R[i][3]] != T[1][1]) return false;
                                                if(getT[F[i][1]][R[i][2]] != T[2][1]) return false;
                                                if(getT[F[i][1]][R[i][1]] != T[3][1]) return false;

                                                if(getT[F[i][2]][R[i][3]] != T[1][2]) return false;
                                                if(getT[F[i][2]][R[i][2]] != T[2][2]) return false;
                                                if(getT[F[i][2]][R[i][1]] != T[3][2]) return false;

                                                if(getT[F[i][3]][R[i][3]] != T[1][3]) return false;
                                                if(getT[F[i][3]][R[i][2]] != T[2][3]) return false;
                                                if(getT[F[i][3]][R[i][1]] != T[3][3]) return false;
                                            }
                                            return true;
                                        }();

                                        if(ok) A.push_back({T, F, sumR, R});
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    int N = in();
    for(int _ : rep(N)) {
        men t(4, vector(4, 0));
        men f(4, vector(4, 0));
        for(int i = 1; i <= 3; i++) for(int j = 1; j <= 3; j++) t[i][j] = in();
        for(int i = 1; i <= 3; i++) for(int j = 1; j <= 3; j++) f[i][j] = in();
        vector<int> ans;
        for(auto &[T, F, sumR, R] : A) {
            bool ok = [&] {
                for(int i = 1; i <= 3; i++) for(int j = 1; j <= 3; j++) {
                    if(t[i][j] != 0 and T[i][j] != t[i][j]) return false;
                    if(f[i][j] != 0 and F[i][j] != f[i][j]) return false;
                }
                return true;
            }();
            if(ok) ans.push_back(sumR);
        }
        unique(ans);
        if(ans.empty()) ans.push_back(0);
        print(ans);
    }
}
accuracy: 1
time_ms: 10
memory_kb: 15004
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, a, b) for(ll i = a; i < b; ++i)
#define rrep(i, a, b) for(ll i = a; i >= b; --i)
constexpr ll inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#define roll_swap(x, a, b, c, d) swap(x.a, x.b), swap(x.b, x.c), swap(x.c, x.d);
struct Dice {
    int top, front, right, left, back, bottom;
    Dice(int to = 1, int fr = 2, int ri = 3, int le = 4, int ba = 5, int bo = 6)
        : top(to), front(fr), right(ri), left(le), back(ba), bottom(bo) {}
    void roll_right() {
        roll_swap((*this), top, left, bottom, right);
    }
    void roll_left() {
        roll_swap((*this), top, right, bottom, left);
    }
    void roll_front() {
        roll_swap((*this), top, back, bottom, front);
    }
    void roll_back() {
        roll_swap((*this), top, front, bottom, back);
    }
    void roll_cw() {
        roll_swap((*this), back, left, front, right);
    }
    void roll_ccw() {
        roll_swap((*this), back, right, front, left);
    }
    void roll(int dir) {
        if(dir == 0) (*this).roll_front();
        if(dir == 1) (*this).roll_right();
        if(dir == 2) (*this).roll_back();
        if(dir == 3) (*this).roll_left();
    }
    friend bool operator<(const Dice& d1, const Dice& d2) {
        int vd1[] = {d1.top, d1.front, d1.right, d1.left, d1.back, d1.bottom};
        int vd2[] = {d2.top, d2.front, d2.right, d2.left, d2.back, d2.bottom};
        return vector<int>(vd1, vd1 + 6) < vector<int>(vd2, vd2 + 6);
    }
};

vector<Dice> all_rotate() {
    vector<Dice> res;
    res.reserve(24);
    Dice d;
    for(int i = 0; i < 6; ++i) {
        for(int j = 0; j < 4; ++j) {
            res.emplace_back(d);
            d.roll_cw();
        }
        if(i & 1) d.roll_front();
        else d.roll_right();
    }
    return res;
}
int main(void) {
    const vector<Dice> dice = all_rotate();
    int TC;
    cin >> TC;
    while(TC--) {
        constexpr int n = 3;
        vector<vector<int>> t(n, vector<int>(n)), f(n, vector<int>(n));
        rep(i, 0, n) {
            rep(j, 0, n) {
                cin >> t[i][j];
            }
        }
        rep(i, 0, n) {
            swap(t[0][i], t[n - 1][i]);
        }
        rep(i, 0, n) {
            rep(j, 0, n) {
                cin >> f[i][j];
            }
        }
        vector<vector<int>> r(n, vector<int>(n));
        vector<vector<int>> cntt(n, vector<int>(n)), cntf(n, vector<int>(n)), cntr(n, vector<int>(n));
        rep(i, 0, n) {
            rep(j, 0, n) {
                if(t[i][j] != 0) cntt[i][j] = 1;
                if(f[i][j] != 0) cntf[i][j] = 1;
            }
        }
        vector<vector<vector<int>>> d(n, vector<vector<int>>(n, vector<int>(n, -1)));
        vector<int> ans;
        auto dfs = [&](auto& dfs, int idx) -> void {
            int x = idx % 3, y = idx / 3 % 3, z = idx / 9;
            if(t[x][y] != 0 and dice[d[x][y][z]].top != t[x][y]) return;
            if(f[z][y] != 0 and dice[d[x][y][z]].front != f[z][y]) return;
            if(r[z][x] != 0 and dice[d[x][y][z]].right != r[z][x]) return;
            if(x > 0 and d[x - 1][y][z] != -1 and dice[d[x - 1][y][z]].back + dice[d[x][y][z]].front != 7) return;
            if(x < n - 1 and d[x + 1][y][z] != -1 and dice[d[x][y][z]].back + dice[d[x + 1][y][z]].front != 7) return;
            if(y > 0 and d[x][y - 1][z] != -1 and dice[d[x][y - 1][z]].right + dice[d[x][y][z]].left != 7) return;
            if(y < n - 1 and d[x][y + 1][z] != -1 and dice[d[x][y][z]].right + dice[d[x][y + 1][z]].left != 7) return;
            if(z > 0 and d[x][y][z - 1] != -1 and dice[d[x][y][z - 1]].bottom + dice[d[x][y][z]].top != 7) return;
            if(z < n - 1 and d[x][y][z + 1] != -1 and dice[d[x][y][z]].bottom + dice[d[x][y][z + 1]].top != 7) return;
            if(idx == n * n * n - 1) {
                int sum = 0;
                rep(i, 0, n) {
                    rep(j, 0, n) {
                        sum += r[i][j];
                    }
                }
                ans.push_back(sum);
                return;
            }
            t[x][y] = dice[d[x][y][z]].top;
            cntt[x][y]++;
            f[z][y] = dice[d[x][y][z]].front;
            cntf[z][y]++;
            r[z][x] = dice[d[x][y][z]].right;
            cntr[z][x]++;
            int nidx = idx + 1;
            int nx = nidx % 3, ny = nidx / 3 % 3, nz = nidx / 9;
            rep(i, 0, 24) {
                d[nx][ny][nz] = i;
                dfs(dfs, nidx);
                d[nx][ny][nz] = -1;
            }
            if(--cntt[x][y] == 0) t[x][y] = 0;
            if(--cntf[z][y] == 0) f[z][y] = 0;
            if(--cntr[z][x] == 0) r[z][x] = 0;
        };
        rep(i, 0, 24) {
            d[0][0][0] = i;
            dfs(dfs, 0);
            d[0][0][0] = -1;
        }
        if(ans.empty()) {
            ans.push_back(0);
        }
        sort(ans.begin(), ans.end());
        ans.erase(unique(ans.begin(), ans.end()), ans.end());
        rep(i, 0, (int)ans.size()) {
            cout << ans[i] << " \n"[i + 1 == (int)ans.size()];
        }
    }
}
accuracy: 1
time_ms: 40
memory_kb: 3512
final_rank: 14

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
    ll modpow(ll x, ll b,ll mod=MOD){
        ll res = 1;
        while(b){
            if(b&1)res = res * x % mod;
            x = x * x % mod;
            b>>=1;
        }
        return res;
    }

    ll modinv(ll x){
        return modpow(x, MOD-2);
    }

    bool was_output = false;

    void print();
    template <class t> void print(const vector<t> &);
    template <class t, class u> void print(const pair<t, u> &);
    template <class t> void print(const t&);
    template <class Head, class... Tail> void print(const Head&, const Tail&...);

    template <class t> void println(const vector<vector<t>>&);
    template <class t> void println(const vector<t>&);
    template <class t> void println(const t&);
    template <class Head, class... Tail> void println(const Head&, const Tail&...);
    void println();
    void newline();

    void print(){
        return;
    }

    template <class t>
    void print(const vector<t>&x){
        for(const t&i:x)print(i);
    }
    template <class t, class u>
    void print(const pair<t,u>&p){
        print(p.first);
        print(p.second);
    }
    template <class t>
    void print(const t&x){
        if(was_output)cout<<" ";
        cout<<x;
        was_output = true;
    }
    template <class Head, class... Tail>
    void print(const Head&head,const Tail&...tail){
        print(head);
        print(tail...);
    }

    template<class t>
    void println(const vector<vector<t>>&x){
        for(vector<t> i:x)println(i);
    }
    template<class t>
    void println(const vector<t>&x){
        for(const t&i:x)print(i);
        println();
    }
    template <class t>
    void println(const t&x){
        print(x);
        println();
    }
    void println(){
        if(was_output){
            cout << endl;
            was_output = false;
        }
    }
    template <class Head, class... Tail>
    void println(const Head&head,const Tail&...tail){
        print(head);
        println(tail...);
    }

    void newline(){
        was_output = true;
        println();
    }

    template<class t>
    istream& operator>>(istream&is, vector<t>&x){
        for(auto &i:x)is >> i;
        return is;
    }

    template<class t, class u>
    istream& operator>>(istream&is, pair<t, u>&x){
        is >> x.first >> x.second;
        return is;
    }

    template<class t>
    ostream& operator<<(ostream&os, vector<t> &v){
        os << "{";
        for(t &i:v){
            os << i << ", ";
        }
        os << "}";
        return os;
    }

    template<class t = long long>
    t in(){
        t res; cin >> res; return res;
    }

    template<class t>
    vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
        sort(line.begin(),line.end(),comp);
        return line;
    }

    template<class t>
    vector<t> reversed(vector<t> line){
        reverse(line.begin(),line.end());
        return line;
    }
    string reversed(string str){
        reverse(str.begin(),str.end());
        return str;
    }

    long long gcd(long long a,long long b){
        while(b){
            a %= b;
            swap(a,b);
        }
        return a;
    }

    long long lcm(long long a,long long b){
        return a / gcd(a,b) * b;
    }

    class output_initializer{
    public:
        output_initializer(){
            ios::sync_with_stdio(false);
            cin.tie(nullptr);
            cout << setprecision(20);
        }
    };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();
}

using namespace templates;

class Dice{
private:
    static set<Dice> dices;
public:
    array<int,6> faces;
    Dice(){
        rep(i,6){
            faces[i] = i + 1;
        }
    }
    Dice(int v1,int v2,int v3,int v4,int v5,int v6){
        faces = array<int,6>({v1,v2,v3,v4,v5,v6});
    }
    int operator[](int i) const{
        return faces[i];
    }
    Dice rotate(char dir){
        if(dir=='R'){
            return Dice(faces[3],faces[1],faces[0],faces[5],faces[4],faces[2]);
        }
        if(dir=='L'){
            return this->rotate('R').rotate('R').rotate('R');
        }
        if(dir=='F'){
            return Dice(faces[4],faces[0],faces[2],faces[3],faces[5],faces[1]);
        }
        if(dir=='B'){
            return this->rotate('F').rotate('F').rotate('F');
        }
        exit(1);
    }

    static void initialize(){
        Dice d;
        while(dices.size()<24){
            d = d.rotate("RLFB"[rand()%4]);
            dices.emplace(d);
        }
    }
    static vector<Dice> Finds(int top,int front,int left=0){
        if(dices.empty())initialize();
        vector<Dice> res;
        foreach(d,dices){
            bool flag = true;
            if(top>0 and d[0]!=top)flag = false;
            if(front>0 and d[1]!=front)flag =false;
            if(left>0 and d[3]!=left)flag =false;
            if(flag)res.emplace_back(d);
        }
        return res;
    }
};

set<Dice> Dice::dices;

bool operator<(const Dice a,const Dice b){
    return a.faces < b.faces;
}

set<int> func(){
    set<int> res;
    vvector<int> tops(3,vector<int>(3));
    vvector<int> fronts(3,vector<int>(3));
    foreach(i,tops)foreach(j,i)j=in();
    foreach(i,fronts)foreach(j,i)j=in();
    vvvector<Dice> dices(3,vvector<Dice>(3,vector<Dice>(3)));
    method(rec,void,int y){
        if(y==3){
            int sum = 0;
            rep(i,3){
                rep(j,3){
                    sum += dices[i][2][j][2];
                }
            }
            res.emplace(sum);
            return;
        }
        method(rec2,void,int j,int k){
            if(k<0){
                rec(y+1);
                return;
            }
            int front = k==2 ? fronts[y][j] : 7-dices[y][j][k+1][4];
            int top = y==0 ? tops[k][j] : 7-dices[y-1][j][k][5];
            int left = j==0 ? 0 : 7-dices[y][j-1][k][2];
            foreach(d,Dice::Finds(top,front,left)){
                dices[y][j][k] = d;
                rec2((j+1)%3,k-(j+1)/3);
            }
        };
        rec2(0,2);
    };
    rec(0);
    return res;
}

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

    int n = in();

    rep(i,n){
        auto ans = func();
        if(ans.empty())ans.emplace(0);
        foreach(i,ans)print(i);println();
    }

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3308
final_rank: 3

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

typedef string::const_iterator State;
#define eps 1e-8L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))
#define ALL(x) (x).begin(), (x).end()

#define int long long

static const int dice[][6] = {
    {1, 2, 3, 5, 4, 6},
    {1, 3, 5, 4, 2, 6},
    {1, 5, 4, 2, 3, 6},
    {1, 4, 2, 3, 5, 6},

    {2, 1, 4, 6, 3, 5},
    {2, 4, 6, 3, 1, 5},
    {2, 6, 3, 1, 4, 5},
    {2, 3, 1, 4, 6, 5},

    {3, 1, 2, 6, 5, 4},
    {3, 2, 6, 5, 1, 4},
    {3, 6, 5, 1, 2, 4},
    {3, 5, 1, 2, 6, 4},

    {4, 2, 1, 5, 6, 3},
    {4, 1, 5, 6, 2, 3},
    {4, 5, 6, 2, 1, 3},
    {4, 6, 2, 1, 5, 3},

    {5, 4, 1, 3, 6, 2},
    {5, 1, 3, 6, 4, 2},
    {5, 3, 6, 4, 1, 2},
    {5, 6, 4, 1, 3, 2},

    {6, 2, 4, 5, 3, 1},
    {6, 4, 5, 3, 2, 1},
    {6, 5, 3, 2, 4, 1},
    {6, 3, 2, 4, 5, 1},
};

set<int> ans;

void checkers(int x, int y, int z, vector<vector<vector<int>>> cur, vector<vector<int>> tops, vector<vector<int>> fronts)
{
  if (x == 3)
  {
    checkers(0, y + 1, z, cur, tops, fronts);
    return;
  }
  if (y == 3)
  {
    checkers(0, 0, z + 1, cur, tops, fronts);
    return;
  }
  if (z == 3)
  {
    int cur_ans = 0;
    for (int y = 0; y < 3; y++)
    {
      for (int z = 0; z < 3; ++z)
      {
        cur_ans += dice[cur[2][y][z]][2];
      }
    }
    ans.insert(cur_ans);
    return;
  }

  REP(i, 24)
  {
    if (z == 0)
    {
      if (tops[2 - y][x] != 0)
      {
        if (tops[2 - y][x] != dice[i][0])
          continue;
      }
    }

    if (y == 0)
    {
      if (fronts[2 - z][x] != 0)
      {
        if (fronts[2 - z][x] != dice[i][1])
          continue;
      }
    }

    if (x != 0)
    {
      if (dice[cur[x - 1][y][z]][2] + dice[i][4] != 7)
      {
        continue;
      }
    }

    if (y != 0)
    {
      if (dice[cur[x][y - 1][z]][3] + dice[i][1] != 7)
      {
        continue;
      }
    }

    if (z != 0)
    {
      if (dice[cur[x][y][z - 1]][5] + dice[i][0] != 7)
      {
        continue;
      }
    }

    cur[x][y][z] = i;
    checkers(x + 1, y, z, cur, tops, fronts);
  }
}

void solve()
{
  vector<vector<int>> tops, fronts;
  REP(t, 2)
  {
    REP(i, 3)
    {
      tops.push_back({});
      REP(q, 3)
      {
        int a;
        cin >> a;
        tops.back().push_back(a);
      }
    }
    swap(tops, fronts);
  }

  vector<vector<vector<int>>> cur(3, vector<vector<int>>(3, vector<int>(3)));
  checkers(0, 0, 0, cur, tops, fronts);
  int is_first = 1;
  if (ans.size() == 0)
    ans.insert(0);
  for (auto x : ans)
  {
    if (!is_first)
    {
      cout << " ";
    }
    else
      is_first = 0;
    cout << x;
  }
  cout << endl;
  ans.clear();
}
#undef int

// generated by oj-template v4.7.2
// (https://github.com/online-judge-tools/template-generator)
int main()
{
  // Fasterize input/output script
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(100);
  // scanf/printf user should delete this fasterize input/output script

  int t = 1;
  cin >> t; // comment out if solving multi testcase
  for (int testCase = 1; testCase <= t; ++testCase)
  {
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 3212
final_rank: 15

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

#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
// usage:
//   ordered_set::find_by_order (access)
//   ordered_set::order_of_key (lower_bound)

template<class K, class V>
using hashmap = gp_hash_table<K, V>;

#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(x) begin(x), end(x)
#define DUMP(x) cerr << #x << " = " << (x) << endl
//#define ASSERT(x) while (!(x)) {}
#define ASSERT(x) assert(x)

struct fast_ios {
    fast_ios() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    };
} fast_ios_;

using ll = long long;

template <class T> bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& v);
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p);
template <class T, class U>
ostream& operator<<(ostream& os, const map<T, U>& mp);

template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
    os << '[';
    REP(i, v.size()) {
        if (i) os << ',';
        os << v[i];
    }
    return os << ']';
}

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

template <class T> ostream& operator<<(ostream& os, const set<T>& st) {
    os << '{';
    int a = 0;
    for (const auto& e : st) {
        if (a) os << ' ';
        a = 1;
        os << e;
    }
    return os << '}';
}

template <class T, class U>
ostream& operator<<(ostream& os, const map<T, U>& mp) {
    os << '{';
    int a = 0;
    for (const auto& p : mp) {
        if (a) os << ' ';
        a = 1;
        os << p;
    }
    return os << '}';
}

const int INF = numeric_limits<int>::max();
const ll LINF = numeric_limits<ll>::max();

void solve() {
    vector<vector<int>> T(3, vector<int>(3)), F(3, vector<int>(3));
    REP(i, 3) REP(j, 3) cin >> T[i][j];
    REP(i, 3) REP(j, 3) cin >> F[i][j];

    using Dice = vector<int>;
    vector<Dice> dice{
            {1,2,3,5,4,6},
            {1,3,5,4,2,6},
            {1,5,4,2,3,6},
            {1,4,2,3,5,6},

            {2,3,1,4,6,5},
            {2,1,4,6,3,5},
            {2,4,6,3,1,5},
            {2,6,3,1,4,5},

            {3,2,6,5,1,4},
            {3,6,5,1,2,4},
            {3,5,1,2,6,4},
            {3,1,2,6,5,4},

            {4,2,1,5,6,3},
            {4,1,5,6,2,3},
            {4,5,6,2,1,3},
            {4,6,2,1,5,3},

            {5,1,3,6,4,2},
            {5,3,6,4,1,2},
            {5,6,4,1,3,2},
            {5,4,1,3,6,2},

            {6,2,4,5,3,1},
            {6,4,5,3,2,1},
            {6,5,3,2,4,1},
            {6,3,2,4,5,1},
    };

    vector<vector<vector<int>>> state(3,vector<vector<int>>(3, vector<int>(3, -1)));

    map<int,bool> ans;

    auto dfs = [&](auto dfs, int i, int j, int k) -> void {
        if (k == 3) return dfs(dfs, i, j+1, 0);
        if (j == 3) return dfs(dfs, i+1, 0, 0);
        if (i == 3) {
            // terminal

            int right = 0;
            REP(x, 3) REP(y, 3) {
                right += dice[state[x][y][2]][2];
            }
            ans[right] = true;
            return;
        }
        // cout << "i,j,k: " << i << ',' << j << ',' << k << endl;

        // int cnt = 0;
        map<int,bool> cnt;
        REP(l, 24) {
            // top
            if (i > 0 and dice[state[i-1][j][k]][5] + dice[l][0] != 7) continue;
            // back
            if (j > 0 and dice[state[i][j-1][k]][1] + dice[l][3] != 7) continue;
            // left
            if (k > 0 and dice[state[i][j][k-1]][2] + dice[l][4] != 7) continue;

            // condition T
            if (T[j][k] != 0 and dice[l][0] != T[j][k]) continue;
            // condition F
            if (F[i][k] != 0 and dice[l][1] != F[i][k]) continue;

            //++cnt;
            cnt[l] = true;

            state[i][j][k] = l;
            dfs(dfs, i, j, k+1);
            state[i][j][k] = -1;
        }

        if (i*j > 0 or j*k > 0 or k*i > 0) assert(cnt.size() <= 1);
    };

    dfs(dfs, 0, 0, 0);

    if (ans.empty()) {
        cout << 0 << endl;
        return;
    }

    bool first = true;
    for (auto [a, _] : ans) {
        if (!first) cout << ' ';
        first = false;
        cout << a;
    }
    cout << endl;
}

int main() {
    int n; cin >> n;
    while (n--) {
        solve();
    }
};
accuracy: 1
time_ms: 20
memory_kb: 3400
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)

int T[3][3];
int F[3][3];
int R[3][3];

int get_right(int t, int f) {
    if (t == 1) {
        if (f == 2) return 3;
        if (f == 3) return 5;
        if (f == 5) return 4;
        if (f == 4) return 2;
    } else if (t == 2) {
        if (f == 1) return 4;
        if (f == 4) return 6;
        if (f == 6) return 3;
        if (f == 3) return 1;
    } else if (t == 3) {
        if (f == 1) return 2;
        if (f == 2) return 6;
        if (f == 6) return 5;
        if (f == 5) return 1;
    } else if (t == 4) {
        if (f == 1) return 5;
        if (f == 5) return 6;
        if (f == 6) return 2;
        if (f == 2) return 1;
    } else if (t == 5) {
        if (f == 1) return 3;
        if (f == 3) return 6;
        if (f == 6) return 4;
        if (f == 4) return 1;
    } else if (t == 6) {
        if (f == 2) return 4;
        if (f == 4) return 5;
        if (f == 5) return 3;
        if (f == 3) return 2;
    }
    return 0;
}

vector<int> dfs(int a, int b, int c) {
    if (c == 3) return dfs(a, b+1, 0);
    if (b == 3) return dfs(a+1, 0, 0);
    if (a==3) {
        int ans = 0;
        rep(i,0,3) {
            rep(k,0,3) {
                ans += get_right(T[2][k], F[i][2]);
            }
        }
        return {ans};
    }
    if (T[b][c] && F[a][b]) {
        if (R[a][c] > 0 && get_right(T[b][c], F[a][b]) != R[a][c]) return {};
    }
    vector<int> ans;
    int t = T[b][c];
    int f = F[a][b];
    int r = R[a][c];
    rep(i,1,7) rep(j,1,7) {
        if (i==j || i+j == 7) continue;
        if (T[b][c] && T[b][c] != i) continue;
        if (F[a][b] && F[a][b] != j) continue;
        if (R[a][c] && get_right(i, j) != R[a][c]) continue;
        T[b][c] = i;
        F[a][b] = j;
        R[a][c] = get_right(i, j);
        auto x = dfs(a, b, c+1);
        for (int y : x) ans.push_back(y);
        T[b][c] = t;
        F[a][b] = f;
        R[a][c] = r;
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N;
    cin >> N;
    while (N--) {
        rep(i,0,3)rep(j,0,3) cin >> T[j][2-i];
        rep(i,0,3)rep(j,0,3) cin >> F[i][j];
        auto ans = dfs(0, 0, 0);
        if (ans.size() == 0) {
            cout << "0\n";
            continue;
        }
        sort(ans.begin(), ans.end());
        ans.erase(unique(ans.begin(), ans.end()), ans.end());
        rep(i,0,ans.size()) cout << ans[i] << (i < ans.size() - 1 ? " " : "\n");
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3592
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)

int T[3][3][3];
int F[3][3][3];
int R[3][3][3];

int get_right(int t, int f) {
    if (t == 1) {
        if (f == 2) return 3;
        if (f == 3) return 5;
        if (f == 5) return 4;
        if (f == 4) return 2;
    } else if (t == 2) {
        if (f == 1) return 4;
        if (f == 4) return 6;
        if (f == 6) return 3;
        if (f == 3) return 1;
    } else if (t == 3) {
        if (f == 1) return 2;
        if (f == 2) return 6;
        if (f == 6) return 5;
        if (f == 5) return 1;
    } else if (t == 4) {
        if (f == 1) return 5;
        if (f == 5) return 6;
        if (f == 6) return 2;
        if (f == 2) return 1;
    } else if (t == 5) {
        if (f == 1) return 3;
        if (f == 3) return 6;
        if (f == 6) return 4;
        if (f == 4) return 1;
    } else if (t == 6) {
        if (f == 2) return 4;
        if (f == 4) return 5;
        if (f == 5) return 3;
        if (f == 3) return 2;
    }
    return 0;
}

vector<int> dfs(int a, int b, int c) {
    if (c == 3) return dfs(a, b+1, 0);
    if (b == 3) return dfs(a+1, 0, 0);
    if (a==3) {
        int ans = 0;
        rep(i,0,3) {
            rep(j,0,3) {
                ans += R[i][2][j];
            }
        }
        return {ans};
    }
    int t = T[a][b][c], f = F[a][b][c], r = R[a][b][c];
    if (a > 0) T[a][b][c] = T[a-1][b][c];
    if (c > 0) F[a][b][c] = F[a][b][c-1];
    if (b > 0) R[a][b][c] = R[a][b-1][c];
    if (T[a][b][c] * F[a][b][c] > 0) {
        int x = get_right(T[a][b][c], F[a][b][c]);
        if (x == 0 || R[a][b][c] > 0 && x != R[a][b][c]) return {};
        R[a][b][c] = x;
    } else if (F[a][b][c] * R[a][b][c] > 0) {
        int x = get_right(F[a][b][c], R[a][b][c]);
        if (x == 0 || T[a][b][c] > 0 && x != T[a][b][c]) return {};
        T[a][b][c] = x;
    } else if (R[a][b][c] * T[a][b][c] > 0) {
        int x = get_right(R[a][b][c], T[a][b][c]);
        if (x == 0 || F[a][b][c] > 0 && x != F[a][b][c]) return {};
        F[a][b][c] = x;
    }
    int t2 = T[a][b][c], f2 = F[a][b][c], r2 = R[a][b][c];
    vector<int> ans;
    rep(i,1,7) rep(j,1,7) {
        if (i==j || i+j == 7) continue;
        if (T[a][b][c] > 0 && T[a][b][c] != i) continue;
        if (F[a][b][c] > 0 && F[a][b][c] != j) continue;
        if (R[a][b][c] >0 && get_right(i, j) != R[a][b][c]) continue;
        T[a][b][c] = i;
        F[a][b][c] = j;
        R[a][b][c] = get_right(i, j);
        // cout << "(" << a << "," << b << "," << c << ")," << i << " " << j << get_right(i,j) << endl;
        auto x = dfs(a, b, c+1);
        for (int y : x) ans.push_back(y);
        T[a][b][c] = t2;
        F[a][b][c] = f2;
        R[a][b][c] = r2;
    }
    T[a][b][c] = t;
    F[a][b][c] = f;
    R[a][b][c] = r;
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N;
    cin >> N;
    while (N--) {
        rep(i,0,3)rep(j,0,3)rep(k,0,3) T[i][j][k] = F[i][j][k] = 0;
        rep(i,0,3)rep(j,0,3) cin >> T[0][j][2-i];
        rep(i,0,3)rep(j,0,3) cin >> F[i][j][0];
        auto ans = dfs(0, 0, 0);
        if (ans.size() == 0) {
            cout << "0\n";
            continue;
        }
        sort(ans.begin(), ans.end());
        ans.erase(unique(ans.begin(), ans.end()), ans.end());
        rep(i,0,ans.size()) cout << ans[i] << (i < ans.size() - 1 ? " " : "\n");
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3528
final_rank: 11

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

static constexpr int dice[6][6] = {{0, 3, 5, 2, 4, 0}, {4, 0, 1, 6, 0, 3}, {2, 6, 0, 0, 1, 5},
                                   {5, 1, 0, 0, 6, 2}, {3, 0, 6, 1, 0, 4}, {0, 4, 2, 5, 3, 0}};

/**
 * @brief サイコロ
 * @docs docs/util/Dice.md
 */
struct Dice {
    int surface[6];
    int top() { return surface[0]; }
    int south() { return surface[1]; }
    int east() { return surface[2]; }
    int west() { return surface[3]; }
    int north() { return surface[4]; }
    int bottom() { return surface[5]; }
    int operator[](int i) const { return surface[i]; }
    Dice() {}
    Dice(int TOP, int FRONT) {
        surface[0] = TOP;
        surface[1] = FRONT;
        surface[2] = dice[TOP - 1][FRONT - 1];
        surface[3] = 7 - surface[2];
        surface[4] = 7 - surface[1];
        surface[5] = 7 - surface[0];
    }
};

void solve() {
    vector<vector<int>> T(3, vector<int>(3)), F(3, vector<int>(3));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cin >> T[i][j];
        }
    }
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cin >> F[i][j];
        }
    }

    vector<Dice> cand;
    for (int i = 1; i <= 6; i++) {
        for (int j = 1; j <= 6; j++) {
            if (i == j || i + j == 7) continue;
            cand.emplace_back(Dice(i, j));
        }
    }
    vector<vector<vector<Dice>>> D(3, vector<vector<Dice>>(3, vector<Dice>(3)));
    vector<int> ans;

    auto dfs = [&](auto self, int cur) -> void {
        if (cur == 27) {
            int sum = 0;
            for (int j = 0; j < 3; j++) {
                for (int k = 0; k < 3; k++) {
                    sum += D[2][j][k].east();
                }
            }
            ans.emplace_back(sum);
            return;
        }
        int x = cur / 9, y = (cur % 9) / 3, z = cur % 3;
        for (auto& d : cand) {
            if (z == 0 && T[2 - y][x] && d.top() != T[2 - y][x]) continue;
            if (y == 0 && F[z][x] && d.south() != F[z][x]) continue;
            if (x > 0 && D[x - 1][y][z].east() + d.west() != 7) continue;
            if (y > 0 && D[x][y - 1][z].north() + d.south() != 7) continue;
            if (z > 0 && D[x][y][z - 1].bottom() + d.top() != 7) continue;
            D[x][y][z] = d;
            self(self, cur + 1);
        }
    };
    dfs(dfs, 0);

    sort(ans.begin(), ans.end());
    ans.erase(unique(ans.begin(), ans.end()), ans.end());
    if (ans.empty()) ans.emplace_back(0);
    for (int i = 0; i < (int)ans.size(); i++) cout << ans[i] << (i + 1 == (int)ans.size() ? '\n' : ' ');
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    for (; N--;) solve();
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3516
final_rank: 10

code_snippet: #define LOCAL
#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(), (x).end()

template <typename T> istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < (int)v.size(); i++) {
        os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
    }
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {
    for (int i = 0; i < (int)v.size(); i++) {
        os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
    }
    return os;
}

template <int i, typename T> void print_tuple(ostream&, const T&) {}
template <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {
    if (i) os << ',';
    os << get<i>(t);
    print_tuple<i + 1, T, Args...>(os, t);
}
template <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {
    os << '{';
    print_tuple<0, tuple<Args...>, Args...>(os, t);
    return os << '}';
}

void debug_out() { cerr << '\n'; }
template <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {
    cerr << head;
    if (sizeof...(Tail) > 0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...)                                                                   \
    cerr << " ";                                                                     \
    cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n'; \
    cerr << " ";                                                                     \
    debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }

template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
}

template <class T1, class T2> inline bool chmin(T1& a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T1, class T2> inline bool chmax(T1& a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
#pragma endregion

static constexpr int dice[6][6] = {{0, 3, 5, 2, 4, 0}, {4, 0, 1, 6, 0, 3}, {2, 6, 0, 0, 1, 5},
                                   {5, 1, 0, 0, 6, 2}, {3, 0, 6, 1, 0, 4}, {0, 4, 2, 5, 3, 0}};

/**
 * @brief サイコロ
 * @docs docs/util/Dice.md
 */
struct Dice {
    int surface[6];
    int top() { return surface[0]; }
    int south() { return surface[1]; }
    int east() { return surface[2]; }
    int west() { return surface[3]; }
    int north() { return surface[4]; }
    int bottom() { return surface[5]; }
    int operator[](int i) const { return surface[i]; }
    Dice() {}
    Dice(int TOP, int FRONT) {
        surface[0] = TOP;
        surface[1] = FRONT;
        surface[2] = dice[TOP - 1][FRONT - 1];
        surface[3] = 7 - surface[2];
        surface[4] = 7 - surface[1];
        surface[5] = 7 - surface[0];
    }
};

void solve() {
    vector<vector<int>> T(3, vector<int>(3)), F(3, vector<int>(3));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cin >> T[i][j];
        }
    }
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cin >> F[i][j];
        }
    }

    vector<Dice> cand;
    for (int i = 1; i <= 6; i++) {
        for (int j = 1; j <= 6; j++) {
            if (i == j || i + j == 7) continue;
            cand.emplace_back(Dice(i, j));
        }
    }
    vector<vector<vector<Dice>>> D(3, vector<vector<Dice>>(3, vector<Dice>(3)));
    vector<int> ans;

    auto dfs = [&](auto self, int cur) -> void {
        if (cur == 27) {
            int sum = 0;
            for (int j = 0; j < 3; j++) {
                for (int k = 0; k < 3; k++) {
                    sum += D[2][j][k].east();
                }
            }
            ans.emplace_back(sum);
            return;
        }
        int x = cur / 9, y = (cur % 9) / 3, z = cur % 3;
        for (auto& d : cand) {
            if (z == 0 && T[2 - y][x] && d.top() != T[2 - y][x]) continue;
            if (y == 0 && F[z][x] && d.south() != F[z][x]) continue;
            if (x > 0 && D[x - 1][y][z].east() + d.west() != 7) continue;
            if (y > 0 && D[x][y - 1][z].north() + d.south() != 7) continue;
            if (z > 0 && D[x][y][z - 1].bottom() + d.top() != 7) continue;
            D[x][y][z] = d;
            self(self, cur + 1);
        }
    };
    dfs(dfs, 0);

    sort(ans.begin(), ans.end());
    ans.erase(unique(ans.begin(), ans.end()), ans.end());
    if (ans.empty()) ans.emplace_back(0);
    for (int i = 0; i < (int)ans.size(); i++) cout << ans[i] << (i + 1 == (int)ans.size() ? '\n' : ' ');
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    for (; N--;) solve();
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3512
final_rank: 9

code_snippet: #include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000001

struct dice{
	/*
	Top,Left,
	Front,Back,
	Right,Bottom
	*/
	vector<int> number = {1,2,3,4,5,6};
	vector<vector<int>> states;
	
	void findStates(){
		states.clear();
		rep(i,4){
			rep(j,4){
				states.push_back(number);
				rotateClockwise(1);
			}
			rotateFront(1);
		}
		rotateLeft(1);
		rep(i,4){
			states.push_back(number);
			rotateClockwise(1);
		}
		rotateLeft(2);
		rep(i,4){
			states.push_back(number);
			rotateClockwise(1);
		}
		rotateLeft(1);
	}
	
	void set(vector<int> numbers){
		number = numbers;
	}
	
	void rotateFront(int n = 1){
		n = (n%4+4)%4;
		rep(i,n){
			int t = top();
			top() = back();
			back() = bottom();
			bottom() = front();
			front() = t;
		}
	}
	
	void rotateLeft(int n = 1){
		n = (n%4+4)%4;
		rep(i,n){
			int t = top();
			top() = right();
			right() = bottom();
			bottom() = left();
			left() = t;
		}
	}
	
	void rotateClockwise(int n = 1){
		n = (n%4+4)%4;
		rep(i,n){
			int t = front();
			front() = right();
			right() = back();
			back() = left();
			left() = t;
		}
	}
	
	int& top(){
		return number[0];
	}
	
	int& bottom(){
		return number[5];
	}
	
	int& front(){
		return number[2];
	}
	
	int& back(){
		return number[3];
	}
	
	int& left(){
		return number[1];
	}
	
	int& right(){
		return number[4];
	}
	
	const int& top()const{
		return number[0];
	}
	
	const int& bottom()const{
		return number[5];
	}
	
	const int& front()const{
		return number[2];
	}
	
	const int& back()const{
		return number[3];
	}
	
	const int& left()const{
		return number[1];
	}
	
	const int& right()const{
		return number[4];
	}
	
};

vector F(3,vector<int>(3));
vector T(3,vector<int>(3));
set<int> S;
dice D;
vector<array<int,3>> A = {
	{0,0,0},{0,0,1},{0,0,2},{0,1,0},{0,2,0},{1,0,0},{2,0,0},{0,1,1},{0,1,2},
	{0,2,1},{0,2,2},{1,0,1},{1,0,2},{1,1,0},{1,1,1},{1,1,2},{1,2,0},{1,2,1},
	{1,2,2},{2,0,1},{2,0,2},{2,1,0},{2,1,1},{2,1,2},{2,2,0},{2,2,1},{2,2,2}
};

vector state(3,vector(3,vector<int>(3,-1)));

void dfs(int cur){
	if(cur==27){
		int sum = 0;
		rep(i,3){
			rep(j,3){
				dice temp;
				temp.set(D.states[state[i][2][j]]);
				sum += temp.right();
			}
		}
		S.insert(sum);
		return;
	}
	
	int x = A[cur][0],y = A[cur][1],z = A[cur][2];
	
	rep(i,24){
		dice temp;
		temp.set(D.states[i]);
		if(z==0){
			if(F[x][y]!=0 && F[x][y]!=temp.front())continue;
		}
		if(x==0){
			if(T[z][y]!=0 && T[z][y]!=temp.top())continue;
		}
		bool f = true;
		rep(j,cur){
			int xx = A[j][0],yy = A[j][1],zz = A[j][2];
			if(abs(xx-x)+abs(yy-y)+abs(zz-z)!=1)continue;
			dice nt;
			nt.set(D.states[state[xx][yy][zz]]);
			if(y<yy){
				if(temp.right()+nt.left()!=7)f = false;
			}
			if(y>yy){
				if(temp.left()+nt.right()!=7)f = false;
			}
			if(x<xx){
				if(temp.bottom()+nt.top()!=7)f = false;
			}
			if(x>xx){
				if(temp.top()+nt.bottom()!=7)f = false;
			}
			if(z<zz){
				if(temp.back()+nt.front()!=7)f = false;
			}
			if(z>zz){
				if(temp.front()+nt.back()!=7)f = false;
			}
		}
		if(!f)continue;
		state[x][y][z] = i;
		dfs(cur+1);
	}
	
}

int main(){
	
	int N;
	cin>>N;
	
	D.findStates();
	
	rep(_,N){
		rep(i,3){
			rep(j,3){
				cin>>T[i][j];
			}
		}
		rep(i,3){
			rep(j,3){
				cin>>F[i][j];
			}
		}
		reverse(T.begin(),T.end());
		S.clear();
		
		dfs(0);
		
		vector<int> ans;
		for(auto a:S)ans.push_back(a);
		
		if(ans.size()==0)cout<<0<<endl;
		else{
			rep(i,ans.size()){
				if(i!=0)cout<<' ';
				cout<<ans[i];
			}
			cout<<endl;
		}
		
	}
	
	
	
	return 0;
}
accuracy: 1
time_ms: 2120
memory_kb: 3080
final_rank: 19

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
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;
}

int d[24][3]={
    {6,2,3},
    {1,5,3},
    {1,2,4},
    {6,5,4},
    {1,3,2},
    {6,4,2},
    {6,3,5},
    {1,4,5},
    {2,1,3},
    {5,6,3},
    {5,1,4},
    {2,6,4},
    {5,3,1},
    {2,4,1},
    {2,3,6},
    {5,4,6},
    {4,1,2},
    {3,6,2},
    {3,1,5},
    {4,6,5},
    {3,2,1},
    {4,5,1},
    {4,2,6},
    {3,5,6},
};

int T[3][3],F[3][3];

int A[3][3][3];

vector<int> v;

void dfs(int num){
    if(num==27){
        int sum=0;
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                sum+=(7-d[A[i][j][2]][2]);
            }
        }
        v.push_back(sum);
        return;
    }
    int i=num/9,j=num/3%3,k=num%3;
    for(int id=0;id<24;id++){
        A[i][j][k]=id;
        // ここで判定を書く
        if(i==0){
            if(T[j][k]!=0 and T[j][k]!=d[id][0])continue;
        }
        if(j==2){
            if(F[i][k]!=0 and F[i][k]!=d[id][1])continue;
        }
        if(i>0){
            int u=A[i-1][j][k];
            if(7-d[u][0]+d[id][0]!=7)continue;
        }
        if(j>0){
            int u=A[i][j-1][k];
            if(d[u][1]+(7-d[id][1])!=7)continue;
        }
        if(k>0){
            int u=A[i][j][k-1];
            if((7-d[u][2])+d[id][2]!=7)continue;
        }
        // OKなら
        dfs(num+1);
    }
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    while(q--){
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                cin >> T[i][j];
            }
        }
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                cin >> F[i][j];
            }
        }
        v.clear();
        dfs(0);
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()),v.end());
        if(v.size()==0){
            printf("0\n");
        }
        else{
            printf("%d",v[0]);
            for(int i=1;i<v.size();i++){
                printf(" %d",v[i]);
            }
            printf("\n");
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3336
final_rank: 2

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

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)

#define popcount __builtin_popcount
#define next __next
#define prev __prev

#ifndef LOCAL
#define dmp(...) ;
#else
#define dmp(...)                                                               \
  cerr << "[ " << #__VA_ARGS__ << " ] : " << dump_str(__VA_ARGS__) << endl
#endif

// ---------------- Utility ------------------

template <class T>
bool chmin(T &a, const T &b) {
  if (a <= b) return false;
  a = b;
  return true;
}
template <class T>
bool chmax(T &a, const T &b) {
  if (a >= b) return false;
  a = b;
  return true;
}

template <class T>
using MaxHeap = priority_queue<T>;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;

template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

// ----------------- Input -------------------

template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}

// ----------------- Output ------------------

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

template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &e : v) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const deque<T> &d) {
  for (const T &e : d) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const set<T> &s) {
  os << "{ ";
  for (const T &e : s) os << e << " ";
  return os << "}";
}

template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  os << "{ ";
  for (const auto &[key, val] : m) os << "( " << key << " -> " << val << " ) ";
  return os << "}";
}

template <class TupleTy, size_t... I>
void dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {
  (..., (os << (I == 0 ? "" : ",") << std::get<I>(t)));
}

template <class... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  os << "(";
  dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());
  return os << ")";
}

void dump_str_rec(ostringstream &) {}

template <class Head, class... Tail>
void dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
  oss << ", " << head;
  dump_str_rec(oss, forward<Tail>(tail)...);
}

template <class T, class... U>
string dump_str(T &&arg, U &&... args) {
  ostringstream oss;
  oss << arg;
  dump_str_rec(oss, forward<U>(args)...);
  return oss.str();
}

// --------------- Fast I/O ------------------

void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

// ------------ End of template --------------

#define endl "\n"

int dice[24][6] = {
    {1, 2, 3, 5, 4, 6}, {1, 3, 5, 4, 2, 6},
    {1, 5, 4, 2, 3, 6}, {1, 4, 2, 3, 5, 6},

    {2, 1, 4, 6, 3, 5}, {2, 4, 6, 3, 1, 5},
    {2, 6, 3, 1, 4, 5}, {2, 3, 1, 4, 6, 5},

    {3, 1, 2, 6, 5, 4}, {3, 2, 6, 5, 1, 4},
    {3, 6, 5, 1, 2, 4}, {3, 5, 1, 2, 6, 4},

    {4, 2, 1, 5, 6, 3}, {4, 1, 5, 6, 2, 3},
    {4, 5, 6, 2, 1, 3}, {4, 6, 2, 1, 5, 3},

    {5, 4, 1, 3, 6, 2}, {5, 1, 3, 6, 4, 2},
    {5, 3, 6, 4, 1, 2}, {5, 6, 4, 1, 3, 2},

    {6, 2, 4, 5, 3, 1}, {6, 4, 5, 3, 2, 1},
    {6, 5, 3, 2, 4, 1}, {6, 3, 2, 4, 5, 1},
};

void solve() {
  auto T = vect(3, vect(3, 0));
  auto F = vect(3, vect(3, 0));
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) { cin >> T[i][j]; }
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) { cin >> F[i][j]; }
  }

  auto d = vect(3, vect(3, vect(3, -1)));
  vector<int> ans;

  function<void(int)> dfs = [&](int n) -> void {
    if (n == 27) {
      int sum = 0;
      for (int i = 0; i < 3; i++) {
        for (int k = 0; k < 3; k++) { sum += dice[d[i][2][k]][2]; }
      }
      ans.push_back(sum);
      return;
    }
    int i = n / 9, j = (n % 9) / 3, k = n % 3;
    for (int t = 0; t < 24; t++) {
      d[i][j][k] = t;
      if (T[i][j] != 0 && T[i][j] != dice[d[i][j][k]][0]) continue;
      if (F[k][j] != 0 && F[k][j] != dice[d[i][j][k]][1]) continue;
      if (i > 0 && dice[d[i - 1][j][k]][1] + dice[d[i][j][k]][3] != 7) continue;
      if (j > 0 && dice[d[i][j - 1][k]][2] + dice[d[i][j][k]][4] != 7) continue;
      if (k > 0 && dice[d[i][j][k - 1]][5] + dice[d[i][j][k]][0] != 7) continue;
      dfs(n + 1);
    }
  };

  dfs(0);

  sort(all(ans));
  ans.erase(unique(all(ans)), ans.end());
  if (ans.size() == 0)
    cout << 0 << endl;
  else {
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i];
      if (i + 1 < ans.size())
        cout << ' ';
      else
        cout << endl;
    }
  }
  return;
}

int main() {
  fastio();
  // solve();
  int t;
  cin >> t;
  while (t--) solve();

  // int t; cin >> t;
  // for(int i=1;i<=t;i++){
  //   cout << "Case #" << i << ": ";
  //   solve();
  // }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3356
final_rank: 6

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

using namespace std;

#define REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  REP(i, SZ(v)) {
    if (i) os << ", ";
    os << v[i];
  }
  return os << "]";
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << " " << p.second << ")";
}

template<class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template<class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;

string dice[24] = {
    "124536", "132456", "153246", "145326",
    "213645", "236415", "264135", "241365",
    "321564", "315624", "356214", "362154",
    "426513", "465123", "451263", "412653",
    "514632", "546312", "563142", "531462",
    "623541", "635421", "654231", "642351"
};
map<string, int> id;

int r[27] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2};
int c[27] = {0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2};
int d[27] = {0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2};

int dr[6] = {1, -1, 0, 0, 0, 0};
int dc[6] = {0, 0, 1, -1, 0, 0};
int dd[6] = {0, 0, 0, 0, 1, -1};

P adj[6] = {{5, 0},
            {0, 5},
            {3, 1},
            {1, 3},
            {4, 2},
            {2, 4}};

void solve() {
  vvi t(3, vi(3)), f(3, vi(3));
  REP(i, 3) {
    REP(j, 3) {
      cin >> t[i][j];
    }
  }

  REP(i, 3) {
    REP(j, 3) {
      cin >> f[i][j];
    }
  }


  function<void(int, vector<vvi> &, set<int> &)> dfs = [&](int k, vector<vvi> &v, set<int> &st) {
    if (k == 27) {
      int su = 0;
      REP(i, 3) {
        REP(j, 3) {
          su += dice[v[i][j][2]][4] - '0';
        }
      }
      st.insert(su);
      return;
    }

    REP(i, 24) {
      if (r[k] == 0) {
        if (t[2 - c[k]][d[k]] != 0 && t[2 - c[k]][d[k]] != dice[i][0] - '0') continue;
      }
      if (c[k] == 0) {
        if (f[r[k]][d[k]] != 0 && f[r[k]][d[k]] != dice[i][1] - '0') continue;
      }
      v[r[k]][c[k]][d[k]] = i;
      bool ok = true;
      REP(j, 6) {
        int nr = r[k] + dr[j], nc = c[k] + dc[j], nd = d[k] + dd[j];
        if (!(0 <= nr && nr < 3 && 0 <= nc && nc < 3 && 0 <= nd && nd < 3)) continue;
        if (v[nr][nc][nd] == -1) continue;
        if ((dice[i][adj[j].first] - '0') + (dice[v[nr][nc][nd]][adj[j].second] - '0') != 7) {
          ok = false;
          break;
        }
      }
      if (ok) dfs(k + 1, v, st);
      v[r[k]][c[k]][d[k]] = -1;
    }
  };

  vector<vvi> v(3, vvi(3, vi(3, -1)));
  set<int> st;
  dfs(0, v, st);

  if (SZ(st) == 0) {
    cout << 0 << endl;
    return;
  }
  int cnt = 0;
  for (auto &e: st) {
    cnt++;
    cout << e;
    if (cnt == SZ(st)) {
      cout << endl;
    } else {
      cout << " ";
    }
  }

}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  REP(i, 24) {
    id[dice[i]] = i;
  }

  int n;
  cin >> n;
  REP(i, n) {
    solve();
  }

  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3352
final_rank: 12

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

set<int>st;
vector<vector<int>>v({ { 1,2,3 },{ 1,3,5 },{ 1,5,4 },{ 1,4,2 },{ 2,3,1 },{ 2,1,4 },{ 2,4,6 },{ 2,6,3 },{ 3,1,2 },{ 3,2,6 },{ 3,6,5 },{ 3,5,1 },{ 4,2,1 },{ 4,1,5, },{ 4,5,6 },{ 4,6,2 },{ 5,1,3 },{ 5,3,6 },{ 5,6,4 },{ 5,4,1 },{ 6,3,2 },{ 6,2,4 },{ 6,4,5 },{ 6,5,3 } });
vector<vector<int>>a(3, vector<int>(3));
vector<vector<int>>b(3, vector<int>(3));

void func(vector<int>&vp, int p = 0) {
	//cout << p << endl;
	if (p == 27) {
		int sum = 0;
		for (int i = 0; i < 27; i += 3) {
			sum += v[vp[i]][2];
		}
		st.insert(sum);
		return;
	}
	int x = p % 3, y = p % 9 / 3, z = p / 9;
	for (int i = 0; i < 24; i++) {
		vp[p] = i;
		bool flag = true;
		if (x) {
			flag &= v[vp[p - 1]][2] == v[i][2];
		}
		if (y) {
			flag &= v[vp[p - 3]][1] == v[i][1];
		}
		if (z) {
			flag &= v[vp[p - 9]][0] == v[i][0];
		}
		if (v[i][0] != a[y][x] && a[y][x])flag = false;
		if (v[i][1] != b[z][x] && b[z][x])flag = false;
		if (flag) {
			func(vp, p + 1);
		}
		vp[p] = -1;
	}
	return;
}

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

	cin >> T;
	while (T--) {
		for (auto &i : a)for (auto &j : i)cin >> j;
		for (auto &i : b)for (auto &j : i)cin >> j;
		vector<int>vp(27, -1);
		func(vp);
		if (st.empty())cout << 0 << endl;
		for (auto i : st) {
			cout << i;
			if (i == *(--st.end()))cout << endl;
			else cout << " ";
		}
		st.clear();
	}
}
accuracy: 1
time_ms: 20
memory_kb: 3136
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
vector<vector<int>> r = {{-1, 2, 4, 1, 3, -1}, {3, -1, 0, 5, -1, 2}, {1, 5, -1, -1, 0, 4}, {4, 0, -1, -1, 5, 1}, {2, -1, 5, 0, -1, 3}, {-1, 3, 1, 4, 2, -1}};
set<vector<int>> dfs(vector<int> A, int p){
  if (p == 3){
    set<vector<int>> st;
    st.insert(A);
    return st;
  } else if (A[p] != -1){
    return dfs(A, p + 1);
  } else {
    set<vector<int>> st;
    for (int i = 0; i < 6; i++){
      vector<int> B = A;
      B[p] = i;
      set<vector<int>> st2 = dfs(B, p + 1);
      for (auto C : st2){
        st.insert(C);
      }
    }
    return st;
  }
}
int main(){
  int N;
  cin >> N;
  for (int i = 0; i < N; i++){
    vector<vector<int>> T(3, vector<int>(3));
    for (int j = 0; j < 3; j++){
      for (int k = 0; k < 3; k++){
        cin >> T[k][j];
        T[k][j]--;
      }
    }
    vector<vector<int>> F(3, vector<int>(3));
    for (int j = 0; j < 3; j++){
      for (int k = 0; k < 3; k++){
        cin >> F[k][j];
        F[k][j]--;
      }
    }
    map<vector<vector<int>>, int> mp;
    for (int j = 0; j < 3; j++){
      set<vector<int>> tmp1 = dfs(T[j], 0);
      set<vector<int>> tmp2 = dfs(F[j], 0);
      set<vector<vector<int>>> st;
      for (auto A : tmp1){
        for (auto B : tmp2){
          vector<vector<int>> R(3, vector<int>(3));
          bool ok = true;
          for (int k = 0; k < 3; k++){
            for (int l = 0; l < 3; l++){
              R[k][l] = r[A[k]][B[l]];
              if (R[k][l] == -1){
                ok = false;
              }
            }
          }
          if (ok){
            st.insert(R);
          }
        }
      }
      for (auto R : st){
        mp[R]++;
      }
    }
    vector<int> ans;
    for (auto P : mp){
      if (P.second == 3){
        int sum = 0;
        for (int j = 0; j < 3; j++){
          for (int k = 0; k < 3; k++){
            sum += P.first[j][k] + 1;
          }
        }
        ans.push_back(sum);
      }
    }
    sort(ans.begin(), ans.end());
    ans.erase(unique(ans.begin(), ans.end()), ans.end());
    int cnt = ans.size();
    if (cnt == 0){
      cout << 0 << endl;
    } else {
      for (int j = 0; j < cnt; j++){
        cout << ans[j];
        if (j < cnt - 1){
          cout << ' ';
        }
      }
      cout << endl;
    }
  }
}
accuracy: 1
time_ms: 50
memory_kb: 3784
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=305,INF=1<<30;

int ri(int u,int f){
    if(u==1){
        if(f==2) return 3;
        if(f==3) return 5;
        if(f==5) return 4;
        if(f==4) return 2;
    }
    if(u==2){
        if(f==1) return 4;
        if(f==4) return 6;
        if(f==6) return 3;
        if(f==3) return 1;
    }
    if(u==3){
        if(f==1) return 2;
        if(f==2) return 6;
        if(f==6) return 5;
        if(f==5) return 1;
    }
    if(u==4){
        if(f==1) return 5;
        if(f==5) return 6;
        if(f==6) return 2;
        if(f==2) return 1;
    }
    if(u==5){
        if(f==1) return 3;
        if(f==3) return 6;
        if(f==6) return 4;
        if(f==4) return 1;
    }
    if(u==6){
        if(f==2) return 4;
        if(f==4) return 5;
        if(f==5) return 3;
        if(f==3) return 2;
    }
    
    return -1000;
}//uとfが与えられた時にrを返す(u==1,f==2,r==3型のサイコロ)

struct dice{
    int u,f,r,b,l,d;
    
    void init(int uu,int ff){
        u=uu;
        f=ff;
        r=ri(u,f);
        b=7-f;
        l=7-r;
        d=7-u;
    }
    
    void turnF(){
        int save=f;
        f=u;
        u=b;
        b=d;
        d=save;
    }
    
    void turnR(){
        int save=r;
        r=u;
        u=l;
        l=d;
        d=save;
    }
    
    void turnB(){
        int save=b;
        b=u;
        u=f;
        f=d;
        d=save;
    }
    
    void turnL(){
        int save=l;
        l=u;
        u=r;
        r=d;
        d=save;
    }
    
    void turn1(){
        int save=r;
        r=b;
        b=l;
        l=f;
        f=save;
    }//時計回り
    
    void turn2(){
        int save=r;
        r=f;
        f=l;
        l=b;
        b=save;
    }//反時計回り
};

int A[3][3],B[3][3];
set<int> ans;

vector<int> X;

void DFS(){
    if(si(X)==8){
        if(A[0][0]&&A[0][0]!=X[0]) return;
        if(A[1][0]&&A[1][0]!=X[1]) return;
        if(A[2][0]&&A[2][0]!=X[2]) return;
        if(A[2][1]&&A[2][1]!=X[3]) return;
        if(A[2][2]&&A[2][2]!=X[4]) return;
        if(B[0][0]&&B[0][0]!=X[5]) return;
        if(B[1][0]&&B[1][0]!=X[6]) return;
        if(B[2][0]&&B[2][0]!=X[7]) return;
        
        if(X[2]+X[5]==7) return;
        
        vector<vector<vector<dice>>> Y(3,vector<vector<dice>>(3,vector<dice>(3)));
        
        Y[0][2][0].init(X[2],X[5]);
        Y[0][2][1].init(X[3],7-Y[0][2][0].r);
        Y[0][2][1].turn1();
        Y[0][2][2].init(X[4],7-Y[0][2][1].r);
        Y[0][2][2].turn1();
        
        Y[0][1][0].init(X[1],7-Y[0][2][0].b);
        Y[0][1][1].init(7-Y[0][1][0].r,7-Y[0][2][1].b);
        Y[0][1][1].turnL();
        Y[0][1][2].init(7-Y[0][1][1].r,7-Y[0][2][2].b);
        Y[0][1][2].turnL();
        
        Y[0][0][0].init(X[0],7-Y[0][1][0].b);
        Y[0][0][1].init(7-Y[0][0][0].r,7-Y[0][1][1].b);
        Y[0][0][1].turnL();
        Y[0][0][2].init(7-Y[0][0][1].r,7-Y[0][1][2].b);
        Y[0][0][2].turnL();
        
        //
        
        Y[1][2][0].init(7-Y[0][2][0].d,X[6]);
        Y[1][2][1].init(7-Y[0][2][1].d,7-Y[1][2][0].r);
        Y[1][2][1].turn1();
        Y[1][2][2].init(7-Y[0][2][2].d,7-Y[1][2][1].r);
        Y[1][2][2].turn1();
        
        Y[1][1][0].init(7-Y[0][1][0].d,7-Y[1][2][0].b);
        Y[1][1][1].init(7-Y[0][1][1].d,7-Y[1][2][1].b);
        Y[1][1][2].init(7-Y[0][1][2].d,7-Y[1][2][2].b);
        
        Y[1][0][0].init(7-Y[0][0][0].d,7-Y[1][1][0].b);
        Y[1][0][1].init(7-Y[0][0][1].d,7-Y[1][1][1].b);
        Y[1][0][2].init(7-Y[0][0][2].d,7-Y[1][1][2].b);
        
        //
        
        Y[2][2][0].init(7-Y[1][2][0].d,X[7]);
        Y[2][2][1].init(7-Y[1][2][1].d,7-Y[2][2][0].r);
        Y[2][2][1].turn1();
        Y[2][2][2].init(7-Y[1][2][2].d,7-Y[2][2][1].r);
        Y[2][2][2].turn1();
        
        Y[2][1][0].init(7-Y[1][1][0].d,7-Y[2][2][0].b);
        Y[2][1][1].init(7-Y[1][1][1].d,7-Y[2][2][1].b);
        Y[2][1][2].init(7-Y[1][1][2].d,7-Y[2][2][2].b);
        
        Y[2][0][0].init(7-Y[1][0][0].d,7-Y[2][1][0].b);
        Y[2][0][1].init(7-Y[1][0][1].d,7-Y[2][1][1].b);
        Y[2][0][2].init(7-Y[1][0][2].d,7-Y[2][1][2].b);
        
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                if(A[i][j]&&A[i][j]!=Y[0][i][j].u) return;
                if(B[i][j]&&B[i][j]!=Y[i][2][j].f) return;
            }
        }
        
        for(int z=0;z<3;z++){
            for(int x=0;x<3;x++){
                for(int y=0;y<3;y++){
                    if(abs(Y[z][x][y].u)>=7) return;
                    if(abs(Y[z][x][y].f)>=7) return;
                    if(abs(Y[z][x][y].r)>=7) return;
                    if(abs(Y[z][x][y].b)>=7) return;
                    if(abs(Y[z][x][y].l)>=7) return;
                    if(abs(Y[z][x][y].d)>=7) return;
                }
            }
        }
        
        int sum=0;
        
        for(int z=0;z<3;z++){
            for(int x=0;x<3;x++){
                sum+=Y[z][x][2].r;
            }
        }
        
        ans.insert(sum);
        
        return;
    }
    
    for(int i=1;i<=6;i++){
        X.push_back(i);
        DFS();
        X.pop_back();
    }
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int Q;cin>>Q;
    while(Q--){
        ans.clear();
        for(int i=0;i<3;i++) for(int j=0;j<3;j++) cin>>A[i][j];
        for(int i=0;i<3;i++) for(int j=0;j<3;j++) cin>>B[i][j];
        
        DFS();
        
        if(si(ans)==0) cout<<0<<endl;
        else{
            for(auto it=ans.begin();it!=ans.end();it++){
                if(it!=ans.begin()) cout<<" ";
                cout<<*it;
            }
            cout<<endl;
        }
    }
}
accuracy: 1
time_ms: 1130
memory_kb: 3188
final_rank: 18

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef double ld;
typedef complex<ld> Point;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;

//top,frontからrightを決定
int ident(int a, int b) {
	int c;
	if (a == 1) {
		if (b == 2)c = 3;
		else if (b == 3)c = 5;
		else if (b == 5)c = 4;
		else c = 2;
	}
	else if (a == 2) {
		if (b == 1)c = 4;
		else if (b == 4)c = 6;
		else if (b == 6)c = 3;
		else c = 1;
	}
	else if (a == 3) {
		if (b == 1)c = 2;
		else if (b == 2)c = 6;
		else if (b == 5)c = 1;
		else c = 5;
	}
	else if (a == 4) {
		if (b == 1)c = 5;
		else if (b == 5)c = 6;
		else if (b == 6)c = 2;
		else c = 1;
	}
	else if (a == 5) {
		if (b == 1)c = 3;
		else if (b == 3)c = 6;
		else if (b == 6)c = 4;
		else c = 1;
	}
	else {
		if (b == 2)c = 4;
		else if (b == 3)c = 2;
		else if (b == 5)c = 3;
		else c = 5;
	}
	return c;
}
int calc(int x) {
	int ret = 0;
	rep (i,9) {
		ret += x % 6 + 1;
		x /= 6;
	}
	return ret;
}
//46656=6^6
int t[3][3], f[3][3];
int t3[9];
void solve() {
	t3[0] = 1;
	rep(i, 8)t3[i + 1] = t3[i] * 6;
	int n; cin >> n; 
	rep(aa, n) {
		map<int, bool> mp[3];
		vector<int> c;
		rep(i, 3) {
			rep(j, 3) {
				cin >> t[i][j];
			}
		}
		rep(i, 3) {
			rep(j, 3) {
				cin >> f[i][j];
			}
		}
		rep(i, 3) {
			rep(j, 46656) {
				vector<int> v;
				int cop = j;
				
				rep (k,6) {
					v.push_back(cop % 6 + 1); cop /= 6;
				}
				bool valid = true;
				rep(k, 3) {
					if (t[k][i] > 0 && t[k][i] != v[k])valid = false;
				}
				rep(k, 3) {
					if (f[k][i] > 0 && f[k][i] != v[3 + k])valid = false;
				}
				if (!valid)continue;
				int sum = 0;
				rep(k, 3) {
					rep(l, 3) {
						if (v[k]+v[3+l]==7||v[k]==v[3+l]) {
							valid = false; break;
						}
						int u = ident(v[k], v[3 + l]);
						u--;
						sum += t3[3*k + l] * u;
					}
				}
				if (!valid)continue;
				mp[i][sum] = true;
				if (i == 0)c.push_back(sum);
			}
		}
		sort(c.begin(), c.end());
		c.erase(unique(c.begin(), c.end()), c.end());
		vector<int> ans;
		rep(i, c.size()) {
			if (mp[1][c[i]] && mp[2][c[i]]) {
				ans.push_back(calc(c[i]));
			}
		}
		sort(ans.begin(), ans.end());
		ans.erase(unique(ans.begin(), ans.end()), ans.end());
		rep(i, ans.size()) {
			if (i > 0)cout << " ";
			cout << ans[i];
		}
		if (ans.size() == 0)cout << 0;
		cout << endl;
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	//stop
	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 3312
final_rank: 17