#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long


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 s=1; s<=Q; s++){
        O[s]=0;
        vector<pair<ll,ll>> v[100];
        priority_queue<pair<ll,ll>> pq;
        ll y1=Y1[s];
        ll y2=Y2[s];
        ll x1=X1[s];
        ll x2=X2[s];

        for(int i=x1;i<=x2;i++){
            for(int j=y1;j<=y2;j++){
                ll u=U[i][j];
                ll vv=V[i][j];
                ll w=W[i][j];
                v[u].pb({vv,w});
                v[vv].pb({u,w});
            }
        }
        ll bio[500];
        for(int i=1;i<=N;i++){
            bio[i]=0;
        }

        bio[1]=1;

        for(auto i:v[1]){
            pq.push({-i.second,i.first});
        }
        ll res=0;
        while(pq.empty()==0){
            auto top = pq.top();
            top.first=-top.first;
            pq.pop();
            if(bio[top.second])continue;
            bio[top.second]=1;
            res+=top.second;
            for(auto i:v[top.second]){
                if(bio[i.first]==0)pq.push({-i.second,i.first});
            }
        }
        for(int i=1;i<=N;i++){
            if(bio[i]==0){
                O[s]=-1;
                break;
            }
        }
        if(O[s]==0) O[s]=res;
	}
}