#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
using namespace std;

// If this structure is used for two-dimensional surface,
// it assumes that the face id[0] faces the positive direction of the Y axis,
// the face id[1] faces the positive direction of the X axis
// and id[2] is top of the dice.
struct Dice {
  int id[6], cnt;
  const static int f[3][3];

  Dice() { init(); }

  void init() {
    for(int i = 0; i < 6; ++i) {
      id[i] = i;
    }
    cnt = 0;
  }

  // type (0:spin, 1:rollX, 2:rollY)
  // dir 1( ccw     neg      pos   )
  //    -1( cw      pos      neg   )
  void rotate(int type, int dir = 1) {
    const int *v = f[type];
    int a,b,c,t; a=v[0]; b=v[1]; c=v[2]; t=id[type];
    if(dir < 0) swap(a,c);
    id[type]=id[a]; id[a]=id[b]; id[b]=id[c]; id[c]=t;
  }

  bool next() {
    ++cnt;
    if(cnt == 24) {
      rotate(0,-1);
      rotate(1,-1);
      cnt = 0;
      return false;
    }
    if(cnt == 4*3) {
      rotate(2,-1);
      rotate(0,-1);
    } else if(cnt%4 == 0) {
      rotate(2);
    } else {
      rotate(0);
    }
    return true;
  }

  void roll(int dir) {
    //const static int dx[] = {1,0,-1,0};
    //const static int dy[] = {0,1,0,-1};
    switch(dir) {
      case 0: rotate(1,-1); break;
      case 1: rotate(2, 1); break;
      case 2: rotate(1, 1); break;
      case 3: rotate(2,-1); break;
    }
  }

  bool operator < (const Dice &d) const {
    for(int i = 0; i < 3; ++i)
      if(id[i] != d.id[i]) return id[i] < d.id[i];
    return false;
  }

  void show() {
    cout << id[4] << " / \\ " << id[5] << endl;
    cout << " /   \\" << endl;
    cout << "|\\ " << id[2] << " /|" << endl;
    cout << "|" << id[0] << "\\ /" << id[1] << "|" << endl;
    cout << " \\ | /" << endl;
    cout << "  \\|/" << endl;
    cout << "   " << id[3] << endl;
  }
};
const int Dice::f[3][3] = {{4,5,1},{3,4,2},{5,3,0}};
const int dx[] = { 1, 0, 0,-1, 0, 0};
const int dy[] = { 0, 1, 0, 0,-1, 0};
const int dz[] = { 0, 0, 1, 0, 0,-1};
const int fromID[] = {5,3,4,0,2,1};

struct P {
  int x, y, z;
  P() {}
  P(int x, int y, int z)
    : x(x), y(y), z(z) {}
};

inline bool isIn(const P &p) {
  return
    0 <= p.x && p.x < 3 &&
    0 <= p.y && p.y < 3 &&
    0 <= p.z && p.z < 3;
}

vector<P> bfs() {
  bool vis[3][3][3];
  queue<P> que;
  fill(vis[0][0], vis[3][0], false);
  vis[0][0][0] = true;
  que.push(P(0,0,0));
  vector<P> res;
  res.push_back(P(0,0,0));
  while(!que.empty()) {
    P p = que.front();
    que.pop();
    for(int k = 0; k < 3; ++k) {
      P np(p.x + dx[k], p.y + dy[k], p.z + dz[k]);
      if(!isIn(np)) continue;
      if(vis[np.z][np.y][np.x]) continue;
      vis[np.z][np.y][np.x] = true;
      que.push(np);
      res.push_back(np);
    }
  }
  return res;
}

Dice cube[3][3][3];
bool exist[3][3][3];
int T[3][3], F[3][3];
vector<P> ps;

void init() {
  fill(exist[0][0], exist[3][0], false);
}

bool canPut(const P &p, const Dice d) {
  for(int k = 0; k < 6; ++k) {
    P np(p.x + dx[k], p.y + dy[k], p.z + dz[k]);
    if(!isIn(np)) continue;
    if(!exist[np.z][np.y][np.x]) continue;
    const Dice &a = cube[np.z][np.y][np.x];
    if(d.id[fromID[k]] != a.id[fromID[k]]) return false;
  }
  if(F[p.y][p.z] != 0 && d.id[0] != F[p.y][p.z]-1) return false;
  if(T[p.x][p.z] != 0 && d.id[2] != T[p.x][p.z]-1) return false;
  return true;
}

void rec(int k, set<int> *res) {
  if(k == ps.size()) {
    int sum = 0;
    for(int i = 0; i < 3; ++i) {
      for(int j = 0; j < 3; ++j) {
        sum += cube[0][i][j].id[1] + 1;
      }
    }
    res->insert(sum);
    return;
  }

  exist[ps[k].z][ps[k].y][ps[k].x] = true;

  Dice d;
  do {
    if(canPut(ps[k], d)) {
      cube[ps[k].z][ps[k].y][ps[k].x] = d;
      rec(k+1, res);
    }
  } while(d.next());

  exist[ps[k].z][ps[k].y][ps[k].x] = false;
}

int main() {
  ps = bfs();
  int Tc;
  cin >> Tc;
  for(int tc = 0; tc < Tc; ++tc) {
    for(int i = 2; i >= 0; --i) {
      for(int j = 2; j >= 0; --j) {
        cin >> T[i][j];
      }
    }
    for(int i = 0; i < 3; ++i) {
      for(int j = 2; j >= 0; --j) {
        cin >> F[i][j];
      }
    }
    set<int> ans;
    init();
    rec(0, &ans);
    if(ans.size() == 0) {
      cout << 0 << endl;
    } else {
      for(set<int>::iterator it = ans.begin();
          it != ans.end(); ++it) {
        if(it != ans.begin()) cout << " ";
        cout << *it;
      }
      cout << endl;
    }
  }
  return 0;
}

Measured memory_bytes: 1208