#include "happy.h"
using namespace std;

long long int dfs(vector<pair<int, int>> graph[], bool visited[], int N, int x, int y, long long int XOR)
{
    if(x==y){
        return XOR;
    }
    visited[x]=true;
    for(int i=0;i<graph[x].size();i++){
        if(!visited[graph[x][i].first]){
            long long int XOR1=XOR;
            XOR=XOR ^ graph[x][i].second;
            return dfs(graph, visited, N, graph[x][i].first, y, XOR);
            XOR=XOR1;
        }
    }
}

vector <long long int> happy (int N, int Q, vector <int> u, vector <int> v, vector <int> h, vector <int> t, vector <int> l, vector <int> r) {
    vector<long long int> sum;
    bool open[N]={false};
    vector<pair<int, int>> graph[N];
    vector<int> opens;
    for(int i=0;i<N;i++){
        graph[u[i]].push_back({v[i], h[i]});
        graph[v[i]].push_back({u[i], h[i]});
    }

    for(int i=0;i<Q;i++){
        for(int j=l[i];j<=r[i];j++){
            if(t[i]==1){
                open[j]=true;
            }
            else{
                open[j]=false;
            }
        }
        for(int j=0;j<N;j++){
            if(open[j]){
                opens.push_back(j);
            }
        }
        for(int j=0;j<opens.size()-1;j++){
            bool visited[N]={false};
            long long int sbor=dfs(graph, visited, N, opens[j], opens[j+1], opens[j]);
            sum.push_back(sbor);
        }
    }
    return sum;
}
