#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mod 1000000007
#define MAX 1000000000000000
#define all(v) v.begin(),v.end()
#define rep(i,a,b) for(i=(ll)a;i<(ll)b;i++)
#define revrep(i,a,b) for(i=(ll)a;i>=(ll)b;i--)
#define ii pair<ll,pair<ll,ll> >
#define MP make_pair
#define pb push_back
#define f first
#define se second
#define ll int
#define vi vector<ll>
ll modexp(ll a,ll b){ ll res = 1; while(b > 0){ if(b & 1) res = (res * a)%mod; a = (a * a)%mod; b/=2; } return res; }
#define rs resize
typedef tree< ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update > OST;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
const ll N = 103,M = 503;
ll t,i,j,k,n,m,q,a,b,c,d,lo,hg,mid,s,ans;
ll fr[M][N][N],dp[M][N][N];
int main()
{
std::ios_base::sync_with_stdio(false); cin.tie(NULL);
scanf("%d",&t);
while(t--){
rep(i,0,M) rep(j,0,n + 1) rep(k,0,m + 1) dp[i][j][k] = fr[i][j][k] = 0;
scanf("%d%d%d",&n,&m,&q);
rep(i,1,n + 1) rep(j,1,m + 1){
scanf("%d",&a);
fr[a][i][j] = 1;
}
rep(k,1,M) rep(i,1,n + 1) rep(j,1,m + 1) fr[k][i][j] += (fr[k][i - 1][j] + fr[k][i][j - 1] - fr[k][i - 1][j - 1]);
rep(i,1,n + 1) rep(j,1,m + 1) rep(k,1,M) dp[k][i][j] = dp[k - 1][i][j] + fr[k][i][j];
while(q--){
scanf("%d%d%d%d",&a,&b,&c,&d);
s = (c - a + 1) * (d - b + 1);
lo = 1; hg = M - 3;
ans = -1;
while(lo <= hg){
mid = (lo + hg) / 2;
k = dp[mid][c][d] - dp[mid][a - 1][d] - dp[mid][c][b - 1] + dp[mid][a - 1][b - 1];
if(k < (s + 1) / 2)
lo = mid + 1;
else{
ans = mid;
hg = mid - 1;
}
}
printf("%d\n",ans);
}
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2luY2x1ZGUgPGV4dC9wYl9kcy9hc3NvY19jb250YWluZXIuaHBwPgojaW5jbHVkZSA8ZXh0L3BiX2RzL3RyZWVfcG9saWN5LmhwcD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdXNpbmcgbmFtZXNwYWNlIF9fZ251X3BiZHM7CiNkZWZpbmUgbW9kIDEwMDAwMDAwMDcKI2RlZmluZSBNQVggMTAwMDAwMDAwMDAwMDAwMAojZGVmaW5lIGFsbCh2KSB2LmJlZ2luKCksdi5lbmQoKQojZGVmaW5lIHJlcChpLGEsYikgZm9yKGk9KGxsKWE7aTwobGwpYjtpKyspCiNkZWZpbmUgcmV2cmVwKGksYSxiKSBmb3IoaT0obGwpYTtpPj0obGwpYjtpLS0pCiNkZWZpbmUgaWkgcGFpcjxsbCxwYWlyPGxsLGxsPiA+CiNkZWZpbmUgTVAgbWFrZV9wYWlyCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgZiBmaXJzdAojZGVmaW5lIHNlIHNlY29uZAojZGVmaW5lIGxsIGludAojZGVmaW5lIHZpIHZlY3RvcjxsbD4KbGwgbW9kZXhwKGxsIGEsbGwgYil7IGxsIHJlcyA9IDE7IHdoaWxlKGIgPiAwKXsgIGlmKGIgJiAxKSByZXMgPSAocmVzICogYSklbW9kOyAgYSA9IChhICogYSklbW9kOyAgYi89MjsgIH0gIHJldHVybiByZXM7IH0KI2RlZmluZSBycyByZXNpemUKdHlwZWRlZiB0cmVlPCBsbCwgbnVsbF90eXBlLCBsZX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gPSAwOwogICAgICAgIHNjYW5mKCIlZCVkJWQiLCZuLCZtLCZxKTsKICAgICAgICByZXAoaSwxLG4gKyAxKSByZXAoaiwxLG0gKyAxKXsKICAgICAgICAgICAgc2NhbmYoIiVkIiwmYSk7CiAgICAgICAgICAgIGZyW2FdW2ldW2pdID0gMTsKICAgICAgICB9CiAgICAgICAgcmVwKGssMSxNKSByZXAoaSwxLG4gKyAxKSByZXAoaiwxLG0gKyAxKSBmcltrXVtpXVtqXSArPSAoZnJba11baSAtIDFdW2pdICsgZnJba11baV1baiAtIDFdIC0gZnJba11baSAtIDFdW2ogLSAxXSk7CiAgICAgICAgcmVwKGksMSxuICsgMSkgcmVwKGosMSxtICsgMSkgcmVwKGssMSxNKSBkcFtrXVtpXVtqXSA9IGRwW2sgLSAxXVtpXVtqXSArIGZyW2tdW2ldW2pdOwogICAgICAgIHdoaWxlKHEtLSl7CiAgICAgICAgICAgIHNjYW5mKCIlZCVkJWQlZCIsJmEsJmIsJmMsJmQpOwogICAgICAgICAgICBzID0gKGMgLSBhICsgMSkgKiAoZCAtIGIgKyAxKTsKICAgICAgICAgICAgbG8gPSAxOyBoZyA9IE0gLSAzOwogICAgICAgICAgICBhbnMgPSAtMTsKICAgICAgICAgICAgd2hpbGUobG8gPD0gaGcpewogICAgICAgICAgICAgICAgbWlkID0gKGxvICsgaGcpIC8gMjsKICAgICAgICAgICAgICAgIGsgPSBkcFttaWRdW2NdW2RdIC0gZHBbbWlkXVthIC0gMV1bZF0gLSBkcFttaWRdW2NdW2IgLSAxXSArIGRwW21pZF1bYSAtIDFdW2IgLSAxXTsKICAgICAgICAgICAgICAgIGlmKGsgPCAocyArIDEpIC8gMikKICAgICAgICAgICAgICAgICAgICBsbyA9IG1pZCArIDE7CiAgICAgICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgICAgIGFucyA9IG1pZDsKICAgICAgICAgICAgICAgICAgICBoZyA9IG1pZCAtIDE7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcHJpbnRmKCIlZFxuIixhbnMpOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9