#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 100 + 3, M = 500 + 5;
int n, m, q, a, b, c, d, v[N][N], freq[N][N][M], f[M], sz;
int main() {
int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d%d", &n, &m, &q);
for(int i=1; i<=n; ++i)
for(int j=1; j<=m; ++j) {
scanf("%d", &v[i][j]);
for(int k=1; k<=500; ++k)
freq[i][j][k] = freq[i-1][j][k] + freq[i][j-1][k]
- freq[i-1][j-1][k] + (v[i][j] == k);
}
while(q--) {
scanf("%d%d%d%d", &a, &b, &c, &d);
sz = (c-a+1)*(d-b+1) + 1;
sz /= 2;
for(int i=1; i<=500; ++i) {
f[i] = freq[c][d][i] - freq[a-1][d][i]
- freq[c][b-1][i] + freq[a-1][b-1][i];
if(f[i] == 0) continue;
sz -= f[i];
if(sz <= 0) {
sz = i;
break;
}
}
printf("%d\n", sz);
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnR5cGVkZWYgbG9uZyBsb25nIGxsOwp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjb25zdCBpbnQgTiA9IDEwMCArIDMsIE0gPSA1MDAgKyA1OwppbnQgbiwgbSwgcSwgYSwgYiwgYywgZCwgdltOXVtOXSwgZnJlcVtOXVtOXVtNXSwgZltNXSwgc3o7CmludCBtYWluKCkgewoJaW50IHQ7CglzY2FuZigiJWQiLCAmdCk7Cgl3aGlsZSh0LS0pIHsKCQlzY2FuZigiJWQlZCVkIiwgJm4sICZtLCAmcSk7CgkJZm9yKGludCBpPTE7IGk8PW47ICsraSkKCQkJZm9yKGludCBqPTE7IGo8PW07ICsraikgewoJCQkJc2NhbmYoIiVkIiwgJnZbaV1bal0pOwoJCQkJZm9yKGludCBrPTE7IGs8PTUwMDsgKytrKQoJCQkJCWZyZXFbaV1bal1ba10gPSBmcmVxW2ktMV1bal1ba10gKyBmcmVxW2ldW2otMV1ba10KCQkJCQkJCQkgIC0gZnJlcVtpLTFdW2otMV1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