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

vector<pair<int,int>> graph[45];
bool visited[45];

pair<int,pair<int,int>> dfs_find(int last,int cur,int target)
{
    if(cur==target)
    {
        for(auto i:graph[cur])
        {
            if(i.first==last)
            {
                return {i.second,{cur,last}};
            }
        }
    }
    for(auto i:graph[cur])
    {
        if(i.first==last)
        {
            continue;
        }
        auto tmp=dfs_find(cur,i.first,target);
        if(tmp.first!=-1)
        {
            for(auto j:graph[cur])
            {
                if(j.first==last)
                {
                    if(j.second>tmp.first)
                    {
                        return {j.second,{cur,last}};
                    }
                    break;
                }
            }
            return tmp;
        }
    }
    return {-1,{-1,-1}};
}

/*int dfs_sum(int last,int cur)
{
    visited[cur]=1;
    int ret=0;
    for(auto i:graph[cur])
    {
        if(i.first==last)
        {
            continue;
        }
        ret+=i.second+dfs_sum(cur,i.first);
    }
    return ret;
}*/

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 t=1;t<=Q;t++)
    {
        for(int i=1;i<=N;i++)
        {
            graph[i].clear();
        }
        for(int i=X1[t];i<=X2[t];i++)
        {
            for(int j=Y1[t];j<=Y2[t];j++)
            {
                auto tmp_find=dfs_find(-1,U[i][j],V[i][j]);
                if(tmp_find.first==-1)
                {
                    graph[U[i][j]].push_back({V[i][j],W[i][j]});
                    graph[V[i][j]].push_back({U[i][j],W[i][j]});
                }
                else if(tmp_find.first>W[i][j])
                {
                    for(auto k:graph[tmp_find.second.first])
                    {
                        if(k.first==tmp_find.second.second)
                        {
                            swap(graph[tmp_find.second.first].back(),k);
                            graph[tmp_find.second.first].pop_back();
                            break;
                        }
                    }
                    for(auto k:graph[tmp_find.second.second])
                    {
                        if(k.first==tmp_find.second.first)
                        {
                            swap(graph[tmp_find.second.second].back(),k);
                            graph[tmp_find.second.second].pop_back();
                            break;
                        }
                    }
                    graph[U[i][j]].push_back({V[i][j],W[i][j]});
                    graph[V[i][j]].push_back({U[i][j],W[i][j]});
                }
            }
        }
        for(int i=1;i<=N;i++)
        {
            visited[i]=0;
        }
        /*int tmp_sum=dfs_sum(-1,1);
        O[t]=tmp_sum;*/
        for(int i=1;i<=N;i++)
        {
            if(!visited[i])
            {
                O[t]=-1;
                break;
            }
        }
    }
}