#include <bits/stdc++.h>

using namespace std;

#define uniq(xs) ((xs).erase(unique((xs).begin(), (xs).end()), (xs).end()))

namespace {
    struct Dice {
        enum FACE { TOP, FRONT, RIGHT, BOTTOM, BACK, LEFT };
        int v[6];
        Dice() {}
        Dice(int top, int front, int right) {
            v[TOP]   = top;    v[BOTTOM] = 7 - top;
            v[FRONT] = front;  v[BACK]   = 7 - front;
            v[RIGHT] = right;  v[LEFT]   = 7 - right;
        }
        bool operator==(const Dice& d) const {
            for (int i = 0; i < 6; i++)
                if (v[i] != d.v[i])
                    return false;
            return true;
        }
        // すべて時計回り
        void rollX() { roll(TOP, FRONT, BOTTOM, BACK); }
        void rollY() { roll(TOP, LEFT, BOTTOM, RIGHT); }
        void rollZ() { roll(FRONT, RIGHT, BACK, LEFT); }
        void roll(int a, int b, int c, int d) {
            int t = v[a];
            v[a] = v[b]; v[b] = v[c]; v[c] = v[d]; v[d] = t;
        }
        vector<Dice> allState() const {
            vector<Dice> Ret;
            Dice d = *this;
            for (int i = 0; i < 6; i++) {
                if (i % 2 == 0) d.rollX();
                else            d.rollY();
                for (int j = 0; j < 4; j++) {
                    d.rollZ();
                    Ret.push_back(d);
                }
            }
            return Ret;
        }
        bool isSameDice(const Dice& d) const {
            vector<Dice> states = allState();
            for (int i = 0; i < states.size(); i++) {
                if (states[i] == d) return true;
            }
            return false;
        }
    };
    ostream& operator<<(ostream& os, const Dice& d) {
        os << "(" << d.v[0];
        for (int i = 1; i < 6; i++) {
            os << "," << d.v[i];
        }
        os << ")";
        return os;
    }

    vector< vector<int> > T;
    vector< vector<int> > F;
    vector<int> ans;
    void input() {
        ans.clear();
        T.clear(); F.clear();
        T.resize(3, vector<int>(3));
        F.resize(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> states;
    void init() {
        Dice D(1, 2, 3);
        states = D.allState();
    }

    ostream& operator<<(ostream& os, const vector<int>& xs) {
        if (xs.empty()) return os << 0;
        os << xs[0];
        for (int i = 1; i < xs.size(); i++) {
            os << " " << xs[i];
        }
        return os;
    }

    vector<Dice> X;
    void dfs(int index) {
        if (index == 27) {
            int c = 0;
            for (int i = 0; i < 27; i++) {
                if (i % 3 == 2) {
                    c += X[i].v[Dice::RIGHT];
                }
            };
            ans.push_back(c);
            return;
        }
        for (int i = 0; i < states.size(); i++) {
            const Dice& state = states[i];
            int y = (index % 9) / 3;
            int x = index % 3;
            if (T[y][x] != 0 && T[y][x] != state.v[Dice::TOP]) continue;
            y = index / 9;
            if (F[y][x] != 0 && F[y][x] != state.v[Dice::FRONT]) continue;
            if (index >= 9) {
                const Dice& up = X[index - 9];
                if (up.v[Dice::BOTTOM] + state.v[Dice::TOP] != 7) continue;
            }
            if (index % 9 >= 3) {
                const Dice& inner = X[index - 3];
                if (inner.v[Dice::FRONT] + state.v[Dice::BACK] != 7) continue;
            }
            if (index % 3 >= 1) {
                const Dice& n = X[index - 1];
                if (n.v[Dice::RIGHT] + state.v[Dice::LEFT] != 7) continue;
            }
            X[index] = state;
            dfs(index + 1);
        }
    }

    void solve() {
        X.clear(); X.resize(27);
        dfs(0);
        sort(ans.begin(), ans.end());
        uniq(ans);
        cout << ans << endl;
    }
}

int main() {
    init();
    int N; cin >> N;
    for (int i = 0; i < N; i++) {
        input();
        solve();
    }
    return 0;
}

Measured memory_bytes: 1352