#include <iostream>
#include <bits/stdc++.h>
#define For(a,b,c) for(int a=b;a<=c;a++)
#define Ford(a,b,c) for(int a=b;a>=c;a--)
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define piii pair<int,pair<int,int>>
#define se second
#define fi first
using namespace std;
const int MAX=1e3,MOD=1e9+7;
int x[5],b[MAX],t,parent[MAX],rang[MAX];

vector<pii> edges[MAX];

vector<piii> v;

int Find(int x)
{
    if (x==parent[x]) return x;
    return (parent[x]=Find(parent[x]));
}

void Merge(int a,int b)
{
    int x=Find(a),y=Find(b);
    if (x==y) return;
    if (rang[x]>rang[y])
    {
        parent[y]=x;
    }
    else if (rang[x]==rang[y])
    {
        rang[x]++;
        parent[y]=x;
    }
    else {parent[x]=y;}
}



int res(int U[][],int V[][],int W[][],int x1,int x2,int x3,int x4)
{
    int rez=0;
    For(i,1,n) parent[i]=i,rang[i]=1;
    v.clear()
    For(i,x1,x2)
    For(j,y1,y2)
    {
        edges[u[i][j]].pb({v[i][j],w[i][j]});
        edges[u[i][j]].pb({v[i][j],w[i][j]});
        v.pb({w[i][j],{u[i][j],v[i][j]});
    }
    sort(v.begin(),v.end());
    for(auto x:v)
    {
        if (Find(x.se.fi)==Find(x.se.se)) continue;
        else Merge(x.se.fi,x.se.se);
        rez+=x.fi;
    }
    return 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[])
{
    For(i,1,q)
    {
        o[i]=res(U,V,W,X1[i],Y1[i],X2[i],Y2[i]);
    }
}