#include<bits/stdc++.h>
using namespace std;

int res;

void Resi(int N, int R, int C, int Q, int** U, int** V, int** W,
	int* X1, int* Y1, int* X2, int* Y2, int* O)
{
    if(N!=2 && N!=3 && N!=4)
	for (int i=1; i<=Q; i++)
		O[i] = -1;
    else if(N==2)
    {
        for(int i=1;i<=Q;i++)
        {
            res=1000000000;
            for(int j=X1[i];j<=X2[i];j++)
            {
                for(int k=Y1[i];k<=Y2[i];k++)
                    res=min(res,W[j][k]);
            }
            O[i]=res;
        }
    }
    else if(N==3)
    {
        int res12=100000000;
        int res13=100000000;
        int res23=100000000;
        for(int i=1;i<=Q;i++)
        {
            res12=100000000;
            res13=100000000;
            res23=100000000;
            for(int j=X1[i];j<=X2[i];j++)
            {
                for(int k=Y1[i];k<=Y2[i];k++)
                    {if(U[j][k]*V[j][k]==2)
                        res12=min(res12,W[j][k]);
                    else if(U[j][k]*V[j][k]==3)
                        res13=min(res13,W[j][k]);
                    else if(U[j][k]*V[j][k]==6)
                        res23=min(res23,W[j][k]);}
            }
            O[i]=min(min(res12+res23,res23+res13),res12+res13);
            if(O[i]>=10000000)
                O[i]=-1;
        }
    }
    else if(N==4)
    {
        int res12=100000000;
        int res13=100000000;
        int res14=100000000;
        int res23=100000000;
        int res24=100000000;
        int res34=100000000;
        for(int i=1;i<=Q;i++)
        {
            res12=100000000;
            res13=100000000;
            res23=100000000;
            res23=100000000;
            res24=100000000;
            res34=100000000;
            res=0;
            for(int j=X1[i];j<=X2[i];j++)
            {
                for(int k=Y1[i];k<=Y2[i];k++)
                    if(U[j][k]*V[j][k]==2)
                        res12=min(res12,W[j][k]);
                    else if(U[j][k]*V[j][k]==3)
                        res13=min(res13,W[j][k]);
                    else if(U[j][k]*V[j][k]==6)
                        res23=min(res23,W[j][k]);
                    else if(U[j][k]*V[j][k]==4)
                        res14=min(res14,W[j][k]);
                    else if(U[j][k]*V[j][k]==12)
                        res34=min(res34,W[j][k]);
                    else if(U[j][k]*V[j][k]==8)
                        res24=min(res24,W[j][k]);
            }
            vector<vector<pair<int,int> > > povezanosti;
            povezanosti.resize(4);
            povezanosti[0].push_back(make_pair(1,res12));
            povezanosti[1].push_back(make_pair(0,res12));
            povezanosti[0].push_back(make_pair(2,res13));
            povezanosti[2].push_back(make_pair(0,res13));
            povezanosti[0].push_back(make_pair(3,res14));
            povezanosti[3].push_back(make_pair(0,res14));
            povezanosti[1].push_back(make_pair(2,res23));
            povezanosti[2].push_back(make_pair(1,res23));
            povezanosti[3].push_back(make_pair(2,res34));
            povezanosti[2].push_back(make_pair(3,res34));
            povezanosti[1].push_back(make_pair(3,res24));
            povezanosti[3].push_back(make_pair(1,res24));
            set<pair<int,int> > idemo;
            for(int i=0;i<povezanosti[0].size();i++)
                idemo.insert(povezanosti[0][i]);
            pair<int,int> pom;
            if(idemo.empty())
            {
                O[i]=-1;
                continue;
            }
            pom=*(idemo.begin());
            res+=pom.second;
            for(int i=0;i<povezanosti[pom.first].size();i++)
                idemo.insert(povezanosti[pom.first][i]);
            pair<int,int> pom1;
            while(!idemo.empty())
            {
                pom1=*(idemo.begin());
                if(pom1.first!=0 && pom1.first!=pom.first)
                {
                    break;
                }
                idemo.erase(idemo.begin());
            }
            if(idemo.empty())
            {
                O[i]=-1;
                continue;
            }
            res+=pom1.second;
            for(int i=0;i<povezanosti[pom.first].size();i++)
                idemo.insert(povezanosti[pom.first][i]);
            pair<int,int> pom2;
            while(!idemo.empty())
            {
                pom2=*(idemo.begin());
                if(pom2.first!=0 && pom2.first!=pom.first && pom2.first!=pom1.first)
                {
                    break;
                }
                idemo.erase(idemo.begin());
            }
            if(idemo.empty())
            {
                O[i]=-1;
                continue;
            }
            res+=pom2.second;
            O[i]=res;
        }
    }
}