#include <bits/stdc++.h>
using namespace std;
const int RDX = 3;
const int MXR = 6;
const int MXN = 12;
const int MXL = 180000;
const int MXC = 3.7e8;
int N, table[RDX][RDX];
long long arr[MXC], brr[MXC];
int scnt[RDX], spv, R(1), pwr[MXN];
int coa[RDX][MXR], cob[RDX][MXR], coc[MXR][RDX], tmp[MXR];
bitset<MXC> legal;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
for (int i(0); i != RDX; ++i)
for (int j(0); j != RDX; ++j)
cin >> table[i][j], ++scnt[table[i][j]];
spv = max_element(scnt, scnt + RDX) - scnt;
coc[0][spv] = 1;
for (int i(0); i != RDX; ++i) {
coa[i][0] = cob[i][0] = 1;
if (i == spv) continue;
for (int j(0); j != RDX; ++j) scnt[j] = 0;
for (int j(0); j != RDX; ++j)
for (int k(0); k != RDX; ++k) scnt[k] += table[j][k] == i;
bool typ(*max_element(scnt, scnt + RDX) <= 1);
for (int j(0); j != RDX; ++j) {
bool ex(false);
for (int k(0); k != RDX; ++k) {
if ((typ ? table[j][k] : table[k][j]) == i)
(typ ? cob : coa)[k][R] = 1, ex = true;
}
if (ex) {
(typ ? coa : cob)[j][R] = 1;
coc[R][i] = 1;
coc[R][spv] = -1;
++R;
}
}
}
//for (int i(0); i != RDX; ++i) {
// for (int j(0); j != R; ++j) cout << coa[i][j] << ' ';
// cout << endl;
//}
//cout << endl;
//for (int i(0); i != RDX; ++i) {
// for (int j(0); j != R; ++j) cout << cob[i][j] << ' ';
// cout << endl;
//}
//cout << endl;
//for (int i(0); i != R; ++i) {
// for (int j(0); j != RDX; ++j) cout << coc[i][j] << ' ';
// cout << endl;
//}
//cout << endl;
assert(R <= MXR);
cin >> N;
pwr[0] = 1;
for (int i(1); i <= N; ++i) pwr[i] = pwr[i - 1] * R;
for (int i(0); i != pwr[N]; ++i) {
legal[i] = true;
for (int j(0); j != N && legal[i]; ++j)
if (i / pwr[j] % R >= RDX) legal[i] = false;
if (!legal[i]) continue;
cin >> arr[i];
}
for (int i(0); i != pwr[N]; ++i) {
if (!legal[i]) continue;
cin >> brr[i];
}
for (int i(0); i != N; ++i) {
for (int j(0); j != pwr[N]; ++j) {
if (j / pwr[i] % R) continue;
for (int k(0); k != RDX; ++k)
for (int l(0); l != R; ++l)
tmp[l] += arr[j + k * pwr[i]] * coa[k][l];
for (int k(0); k != R; ++k)
arr[j + k * pwr[i]] = tmp[k], tmp[k] = 0;
for (int k(0); k != RDX; ++k)
for (int l(0); l != R; ++l)
tmp[l] += brr[j + k * pwr[i]] * cob[k][l];
for (int k(0); k != R; ++k)
brr[j + k * pwr[i]] = tmp[k], tmp[k] = 0;
}
}
//for (int i(0); i != pwr[N]; ++i) cout << arr[i] << ' ';
//cout << endl;
//for (int i(0); i != pwr[N]; ++i) cout << brr[i] << ' ';
//cout << endl;
for (int i(0); i != pwr[N]; ++i) arr[i] *= brr[i], brr[i] = 0;
for (int i(0); i != N; ++i) {
// for (int j(0); j != pwr[N]; ++j)
// cout << arr[j] << ' ';
// cout << endl;
for (int j(0); j != pwr[N]; ++j) {
if (j / pwr[i] % R) continue;
for (int k(0); k != R; ++k)
for (int l(0); l != RDX; ++l)
tmp[l] += arr[j + k * pwr[i]] * coc[k][l];
for (int k(0); k != R; ++k)
arr[j + k * pwr[i]] = tmp[k], tmp[k] = 0;
}
}
for (int i(0); i != pwr[N]; ++i) {
if (!legal[i]) continue;
cout << arr[i] << ' ';
}
cout << endl;
return 0;
}