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

int par[44], kol[44];

void Clear() {
    fill(par, par+44, -1);
    fill(kol, kol+44, 1);
}

int Find(int x) {
    if (par[x] == -1)
        return x;
    return par[x] = Find(par[x]);
}

void Union(int a, int b) {
    int x = Find(a), y = Find(b);
    if (kol[x] <= kol[y]) {
        kol[y] += kol[x];
        kol[x] = 0;
        par[x] = y;
    }
    else {
        kol[x] += kol[y];
        kol[y] = 0;
        par[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)
{
	if (N == 2) {



        return;
	}

	if (N == 3) {


        return;
	}

	for (int q = 1; q <= Q; q++) {

        int ans = 0;
        Clear();

        priority_queue<pair<int, pair<int, int>>> pq;

        for (int i = X1[q]; i <= X2[q]; i++) {
            for (int j = Y1[q]; j <= Y2[q]; j++) {
                pq.push({-W[i][j], {U[i][j], V[i][j]}});
            }
        }

        while (!pq.empty()) {
            int w = -pq.top().first;
            int u = pq.top().second.first;
            int v = pq.top().second.second;
            pq.pop();

            if (Find(u) == Find(v))
                continue;

            ans += w;
            Union(u, v);
        }

        O[q] = -1;

        for (int i = 1; i <= N; i++) {
            if (kol[i] == N) {O[q] = ans; break;}
        }

	}
}