Language: C++

#include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>
#include "dice.h"

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { 1, -1, 0, 0,0,0 }; const int dy[] = { 0,0, 1, -1, 0,0 };const int dz[] = { 0,0,0, 0, 1, -1 };

const int side[6][4] = {
	{ 2, 4, 5, 3 },{ 1, 3, 6, 4 },{ 1, 5, 6, 2 },{ 1, 2, 6, 5 },{ 1, 4, 6, 3 },{ 2, 3, 5, 4 }
};
const int dice_right[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 }
};
struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};
vi ans;
vector<vector<vvi>> dices(3, vector<vvi>(3, vvi(3, vi(2))));
void dfs() {
	REP(i, 3) {
		REP(j, 3) {
			REP(k, 3) {
				vector<vector<vvi>> copy_dices = dices;
				if (dices[i][j][k][0] == 0 && dices[i][j][k][1] == 0) {
					REP(l, 6) {
						REP(m, 6) {
							if (l + m == 7 || l == m)
								continue;
							bool able = true;
							REP(n, 6) {
								if (0 <= i + dx[n] && i + dx[n] < 3 && 0 <= j + dy[n] && j + dy[n] < 3 && 0 <= k + dz[n] && k + dz[n] < 3) {
									switch (n) {
										case 0:
										case 1:
											if (dices[i + dx[n]][j + dy[n]][k + dz[n]][0] != 0 && dices[i + dx[n]][j + dy[n]][k + dz[n]][0] != l + 1)
												able = false;
											break;
										case 2:
										case 3:
											if (dices[i + dx[n]][j + dy[n]][k + dz[n]][1] != 0 && dices[i + dx[n]][j + dy[n]][k + dz[n]][1] != m + 1)
												able = false;
											break;
										case 4:
										case 5:
											if (dices[i + dx[n]][j + dy[n]][k + dz[n]][0] != 0 &&
												dices[i + dx[n]][j + dy[n]][k + dz[n]][1] != 0 &&
												dice_right[dices[i + dx[n]][j + dy[n]][k + dz[n]][0] - 1][dices[i + dx[n]][j + dy[n]][k + dz[n]][1] - 1] != dice_right[l][m])
												able = false;
											break;
									}
									if (!able)
										break;
								}
							}
							if (able) {
								dices[0][j][k][0] = l+1;
								dices[1][j][k][0] = l + 1;
								dices[2][j][k][0] = l + 1;
								dices[i][0][k][1] = m + 1;
								dices[i][1][k][1] = m + 1;
								dices[i][2][k][1] = m + 1;
								dfs();
								dices = copy_dices;
							}
						}
					}
					return;
				}
				if (dices[i][j][k][0] != 0 && dices[i][j][k][1] == 0) {
					REP(m, 6) {
						if (dices[i][j][k][0]-1 + m == 7 || dices[i][j][k][0]-1 == m)
							continue;
						bool able = true;
						REP(n, 6) {
							if (0 <= i + dx[n] && i + dx[n] < 3 && 0 <= j + dy[n] && j + dy[n] < 3 && 0 <= k + dz[n] && k + dz[n] < 3) {
								switch (n) {
									case 0:
									case 1:
										if (dices[i + dx[n]][j + dy[n]][k + dz[n]][0] != 0 && dices[i + dx[n]][j + dy[n]][k + dz[n]][0] != dices[i][j][k][0])
											able = false;
										break;
									case 2:
									case 3:
										if (dices[i + dx[n]][j + dy[n]][k + dz[n]][1] != 0 && dices[i + dx[n]][j + dy[n]][k + dz[n]][1] != m + 1)
											able = false;
										break;
									case 4:
									case 5:
										if (dices[i + dx[n]][j + dy[n]][k + dz[n]][0] != 0 &&
											dices[i + dx[n]][j + dy[n]][k + dz[n]][1] != 0 &&
											dice_right[dices[i + dx[n]][j + dy[n]][k + dz[n]][0] - 1][dices[i + dx[n]][j + dy[n]][k + dz[n]][1] - 1] != dice_right[dices[i][j][k][0]-1][m])
											able = false;
										break;
								}
								if (!able)
									break;
							}
						}
						if (able) {
							dices[i][0][k][1] = m + 1;
							dices[i][1][k][1] = m + 1;
							dices[i][2][k][1] = m + 1;
							dfs();
							dices = copy_dices;
						}
					}
					return;
				}
				if (dices[i][j][k][0] == 0 && dices[i][j][k][1] != 0) {
					REP(l, 6) {
						if (l + dices[i][j][k][1]-1 == 7 || l == dices[i][j][k][1]-1)
							continue;
						bool able = true;
						REP(n, 6) {
							if (0 <= i + dx[n] && i + dx[n] < 3 && 0 <= j + dy[n] && j + dy[n] < 3 && 0 <= k + dz[n] && k + dz[n] < 3) {
								switch (n) {
									case 0:
									case 1:
										if (dices[i + dx[n]][j + dy[n]][k + dz[n]][0] != 0 && dices[i + dx[n]][j + dy[n]][k + dz[n]][0] != l + 1)
											able = false;
										break;
									case 2:
									case 3:
										if (dices[i + dx[n]][j + dy[n]][k + dz[n]][1] != 0 && dices[i + dx[n]][j + dy[n]][k + dz[n]][1] != dices[i][j][k][1])
											able = false;
										break;
									case 4:
									case 5:
										if (dices[i + dx[n]][j + dy[n]][k + dz[n]][0] != 0 &&
											dices[i + dx[n]][j + dy[n]][k + dz[n]][1] != 0 &&
											dice_right[dices[i + dx[n]][j + dy[n]][k + dz[n]][0] - 1][dices[i + dx[n]][j + dy[n]][k + dz[n]][1] - 1] != dice_right[l][dices[i][j][k][1]-1])
											able = false;
										break;
								}
								if (!able)
									break;
							}
						}
						if (able) {
							dices[0][j][k][0] = l + 1;
							dices[1][j][k][0] = l + 1;
							dices[2][j][k][0] = l + 1;
							dfs();
							dices = copy_dices;
						}
					}
					return;
				}
			}
		}
	}
	int sum = 0;
	REP(i,3) {
		REP(j, 3) {
			if (dice_right[dices[i][j][2][0] - 1][dices[i][j][2][1] - 1] == 0)
				return;
			sum+=dice_right[dices[i][j][2][0] - 1][dices[i][j][2][1] - 1];
		}
	}
	ans.push_back(sum);
}
int main() {
	int kurikaesu;
	cin >> kurikaesu;
	while (kurikaesu--) {
		ans.clear();
		REP(i, 3) {
			REP(j, 3) {
				REP(k, 3) {
					REP(l, 2) {
						dices[i][j][k][l] = -1;
					}
				}
			}
		}
		REP(i, 3) {
			REP(j, 3) {
				cin >> dices[0][i][j][0];
				dices[1][i][j][0] = dices[0][i][j][0];
				dices[2][i][j][0] = dices[0][i][j][0];
			}
		}
		REP(i, 3) {
			REP(j, 3) {
				cin >> dices[i][2][j][1];
				dices[i][1][j][1] = dices[i][2][j][1];
				dices[i][0][j][1] = dices[i][2][j][1];
			}
		}
		dfs();
		SORT(ans);
		ans.erase(unique(ans.begin(), ans.end()), ans.end());
		REP(i, ans.size()) {
			cout << ans[i];
			if (i != ans.size() - 1)
				cout << ' ';
		}
		if (ans.empty())
			cout << 0;
		cout << endl;
	}
}

Compiler output:
a.cc:19:10: fatal error: dice.h: No such file or directory
   19 | #include "dice.h"
      |          ^~~~~~~~
compilation terminated.