#include <iostream>
///#include <fstream>
#include <vector>
#include "happy.h"
using namespace std;
///ifstream cin("happy.in");
///ofstream cout("happy.out");
int a[101][101];
std::vector <long long int> coada_val, coada_h;
/*
struct ura{
    int val, h;
} coada[10001];
*/
std::vector<long long int> s;
std::vector<long long int> happy(int n, int q, std::vector<int> u, std::vector<int> v, std::vector<int> h, std::vector<int> t, std::vector<int> l, std::vector<int> r){
    int i, j;
    long long int c;
    for( i = 1; i <= n - 1; i++ )
        a[u[i]][v[i]]=a[v[i]][u[i]]=-h[i];
    int w, cer, st, dr, st1, dr1;
    for( w = 1; w <= q; w++ ){
        cer=t[w];
        st=l[w];
        dr=r[w];
        for( i = st; i <= dr; i++ )
            for( j = st; j <= dr; j++ ){
                if( cer == 1 )
                    a[i][j]=max(a[i][j],-a[i][j]);
                else
                    a[i][j]=min(a[i][j],-a[i][j]);
            }
        for( c = 0; c < n; c++ ){
            ///lee din camera c
            coada_val[1]=c;
            coada_h[1]=0;   ///elem neutru
            st1=dr1=1;
            while( st1 <= dr1 ){
                for( i = 0; i < n; i++ ){
                    if( a[i][coada_val[st1]] > 0 ){
                        dr1++;
                        coada_val[dr1]=i;
                        coada_h[dr1]=(coada_h[st1]^a[i][coada_val[st1]]);
                    }
                    if( a[coada_val[st1]][i] > 0 ){
                        dr1++;
                        coada_val[dr1]=i;
                        coada_h[dr1]=(coada_h[st1]^a[coada_val[st1]][i]);
                    }
                }
                st1++;
            }
            for( i = 1; i <= dr1; i++ )
                if( coada_h[i] > s[w] )
                    s[w]=coada_h[i];
        }
    }
    return s;
}
/*
int main()
{
    return 0;
}
*/