#include <bits/stdc++.h>

using namespace std;

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)
{
    int n = N;
    if(R <= 100 && C <= 100 && Q <= 100)
    {
        for(int qq = 1; qq <= Q; qq++)
        {
            int mat[n+1][n+1];
            int price[n+1];
            bool bio[n+1];
            for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)mat[i][j]=12345678,price[i]=12345678;
            for(int x = X1[qq]; x <= X2[qq]; x++)
            {
                for(int y = Y1[qq]; y <= Y2[qq]; y++)
                {
                    int u = U[x][y];
                    int v = V[x][y];
                    int w = W[x][y];
                    mat[u][v]=min(mat[u][v],w);
                    mat[v][u]=min(mat[v][u],w);
                }
            }
            int ans = 0;
            price[1]=0;
            for(int i = 0; i < n; i++)
            {
                int tr = -1;
                for(int j = 1; j <= n; j++)
                {
                    if(bio[j])continue;
                    if(tr == -1 || price[j]<price[tr])tr=j;
                }
                if(tr==-1)break;
                bio[tr]=true;
                ans += price[tr];
                for(int j = 1; j <= n; j++)
                {
                    if(bio[j])continue;
                    price[j]=min(price[j],mat[tr][j]);
                }
            }
            O[qq]=ans;
        }
        return ;
    }
}