#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;
        }
    }
}


Measured memory_bytes: 3244