#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 = 60500000;
int N, table[RDX][RDX], mp[MXL];
long long ans[MXL], ta[MXC], tb[MXC], crr[MXL], tmp[MXR];
int scnt[RDX], spv, R(1), pw3[MXN], pwr[MXN];
int arr[MXL], brr[MXL], coa[RDX][MXR], cob[RDX][MXR], coc[MXR][RDX];
void work(short *a, short *b, long long *c) {
for (int i(0); i != pw3[N - 1]; ++i)
ta[mp[i]] = a[i], tb[mp[i]] = b[i];
for (int i(0); i != N - 1; ++i) {
for (int nj(0); nj != pwr[N - 2]; ++nj) {
int j(nj / pwr[i] * pwr[i + 1] + nj % pwr[i]);
for (int k(0); k != RDX; ++k)
for (int l(0); l != R; ++l)
tmp[l] += ta[j + k * pwr[i]] * coa[k][l];
for (int k(0); k != R; ++k)
ta[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] += tb[j + k * pwr[i]] * cob[k][l];
for (int k(0); k != R; ++k)
tb[j + k * pwr[i]] = tmp[k], tmp[k] = 0;
}
}
for (int i(0); i != pwr[N - 1]; ++i) ta[i] *= tb[i], tb[i] = 0;
for (int i(0); i != N - 1; ++i) {
for (int nj(0); nj != pwr[N - 2]; ++nj) {
int j(nj / pwr[i] * pwr[i + 1] + nj % pwr[i]);
for (int k(0); k != R; ++k)
for (int l(0); l != RDX; ++l)
tmp[l] += ta[j + k * pwr[i]] * coc[k][l];
for (int k(0); k != R; ++k)
ta[j + k * pwr[i]] = tmp[k], tmp[k] = 0;
}
}
for (int i(0); i != pw3[N - 1]; ++i)
c[i] += ta[mp[i]];
}
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;
}
}
}
assert(R <= MXR);
cin >> N;
pw3[0] = pwr[0] = 1;
for (int i(1); i <= N; ++i) pwr[i] = pwr[i - 1] * R, pw3[i] = pw3[i - 1] * 3;
for (int i(0); i != pw3[N]; ++i) cin >> arr[i];
for (int i(0); i != pw3[N]; ++i) cin >> brr[i];
for (int i(0); i != pw3[N - 1]; ++i) mp[i] = mp[i / RDX] * R + i % RDX;
for (int ad(0); ad != RDX; ++ad) {
for (int bd(0); bd != RDX; ++bd) {
work(arr + ad * pw3[N - 1], brr + bd * pw3[N - 1],
crr + table[ad][bd] * pw3[N - 1]);
}
}
for (int i(0); i != pw3[N]; ++i) {
cout << crr[i] << ' ';
}
cout << endl;
return 0;
}