#include<bits/stdc++.h>
#define maxn 105
#define pii pair<int,int>
using namespace std;

int sz[45];
int p[45];
int adj[45][45];

vector<pair<int,pii>>g;

void build(int n)
{
    for(int i=1;i<=n;i++)
    {
        sz[i]=1;
        p[i]=i;
    }
}
int fnd(int x)
{
    if(x==p[x])return x;
    return p[x]=fnd(p[x]);
}
bool same(int x,int y)
{
    return fnd(x)==fnd(y);
}
void unite(int x,int y)
{
    x=fnd(x);
    y=fnd(y);
    if(sz[x]<sz[y])swap(x,y);
    sz[x]+=sz[y];
    p[y]=x;
}

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[])
{
    for(int br=1;br<=q;br++)
    {
        if(x1[br]>x2[br])swap(x1[br],x2[br]);
        if(y1[br]>y2[br])swap(y1[br],y2[br]);
        for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)adj[i][j]=-1;
        for(int i=x1[br];i<=x2[br];i++)
        {
            for(int j=y1[br];j<=y2[br];j++)
            {
                int uu=u[i][j];
                int vv=v[i][j];
                if(uu>vv)swap(uu,vv);
                int ww=w[i][j];
                if(adj[uu][vv]==-1 || adj[uu][vv]>ww)
                {
                    adj[uu][vv]=ww;
                }
            }
        }

        g.clear();
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(adj[i][j]!=-1)
                {
                    g.push_back({adj[i][j],{i,j}});
                }
            }
        }

        sort(g.begin(),g.end());
        /*cout<<br<<endl;
        for(auto x:g)cout<<x.first<<" "<<x.second.first<<","<<x.second.second<<endl;
        cout<<endl<<endl;*/

        build(n);
        o[br]=0;
        int cv=1;
        int len=g.size();
        for(int i=0;i<len;i++)
        {
            int uu=g[i].second.first;
            int vv=g[i].second.second;
            int ww=g[i].first;
            if(!same(uu,vv))
            {
                o[br]+=ww;
                cv++;
                unite(uu,vv);
            }
        }

        if(cv<n-1)o[br]=-1;
    }
}