//6333
//
//ID: taoxiaa1
//PROG: concom
//LANG: C++
//
//URAL ID:248353QC
//#include
//#include
//#include
//#include
//#include
//6336
#include
using namespace std;
typedef long long LL;
int A[10], M[400][400];
int L;
LL calc (int n, int m) {
if (n < 0 || m < 0) return 0;
return 1LL * M[L - 1][L - 1] * (n / L) * (m / L) +
1LL * M[n % L][L - 1] * (m / L) +
1LL * M[L - 1][m % L] * (n / L) +
1LL * M[n % L][m % L];
}
void solve () {
cin >> L;
for (int i = 0; i < L; ++i) cin >> A[i];
int k = 0;
for (int i = 0; i < 4 * L; ++i) {
for (int j = 0; j <= i; ++j) {
if (j < 2 * L && i - j < 2 * L) M[j][i - j] = A[k];
k = (k + 1) % L;
}
}
L *= 2;
for (int i = 0; i < L; ++i) for (int j = 0; j < L; ++j) {
if (i) M[i][j] += M[i - 1][j];
if (j) M[i][j] += M[i][j - 1];
if (i && j) M[i][j] -= M[i - 1][j - 1];
}
int Q;
cin >> Q;
while (Q--) {
int xL, yL, xR, yR;
cin >> xL >> yL >> xR >> yR;
cout << calc(xR, yR) - calc(xL - 1, yR) - calc(xR, yL - 1) + calc(xL - 1, yL - 1) << endl;
}
}
int main () {
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--) solve();
return 0;
}
//6341
#include
using namespace std;
int ord(char c) {
if (isdigit(c)) return c - '0';
return c - 'A' + 10;
}
char str(int c) {
if (c < 10) return c + '0';
return c + 'A' - 10;
}
int r[16][16], c[16][16];
int a[16][16];
int b[4][4];
char s[16];
int ret;
void add(int ip, int jp, int v) {
for (int i = ip * 4; i < (ip + 1) * 4; ++i) {
for (int j = jp * 4; j < (jp + 1) * 4; ++j) {
r[i][a[i][j]] += v;
c[j][a[i][j]] += v;
}
}
}
bool rot(int ip, int jp) {
for (int i = ip * 4; i < (ip + 1) * 4; ++i) {
for (int j = jp * 4; j < (jp + 1) * 4; ++j) {
--r[i][a[i][j]];
--c[j][a[i][j]];
b[j - jp * 4][(ip + 1) * 4 - i - 1] = a[i][j];
}
}
bool succ = true;
for (int i = ip * 4; i < (ip + 1) * 4; ++i) {
for (int j = jp * 4; j < (jp + 1) * 4; ++j) {
a[i][j] = b[i - ip * 4][j - jp * 4];
if ((++r[i][a[i][j]] > 1) || (++c[j][a[i][j]] > 1)) succ = false;
}
}
return succ;
}
void dfs(int ip, int jp, int now) {
if (ip == 4 && jp == 0) {
ret = min(ret, now);
return;
}
add(ip, jp, 1);
if (now >= ret) return;
for (int i = 1; i <= 4; ++i) {
if (rot(ip, jp)) dfs(jp == 3 ? ip + 1 : ip, jp == 3 ? 0 : jp + 1, now + (i & 3));
}
add(ip, jp, -1);
}
void solve () {
for (int i = 0; i < 16; ++i) {
scanf("%s", s);
for (int j = 0; j < 16; ++j) a[i][j] = ord(s[j]);
}
memset(r, 0, sizeof(r));
memset(c, 0, sizeof(c));
ret = 16 * 4;
dfs(0, 0, 0);
printf("%d\n", ret);
}
int main() {
int T;
scanf("%d", &T);
while (T--) solve();
return 0;
}