#include "happy.h"
#include "iostream"
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;

struct hap
{
public:
	vector <pair <int, int>> roads;
	int glu = -1;
	uint64_t mod = 0;
	bool active = 0;
};
vector <hap> haps;

void bfs(int n, int g, uint64_t mod)
{
	if (haps[n].glu != -1) return;
	haps[n].glu = g;
	haps[n].mod = mod;


	for (auto i : haps[n].roads)
	{
		bfs(i.first, g + 1, mod ^ i.second);
	}
}





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)
{
	vector <hap> hapss(N);
	vector <long long int> anss;
	haps = move(hapss);
	for (size_t i = 0; i < u.size(); i++)
	{
		haps[u[i]].roads.emplace_back(v[i], h[i]);
		haps[v[i]].roads.emplace_back(u[i], h[i]);
	}
	bfs(0, 0, 0);

	uint64_t ans = 0;

	for (size_t i = 0; i < Q; i++)
	{
		if (t[i] == 1)
		{
			haps[i].active = 1;

			for (size_t ii = 0; ii < N; ii++)
			{
				if (ii == i) continue;
				if (!haps[ii].active) continue;

				vector <int> PathA, PathB;



				{
					int ttk = ii;
					while (ttk != 0)
					{
						for (auto iii : haps[ttk].roads)
						{
							if (haps[iii.first].glu < haps[ii].glu)
							{
								PathA.push_back(iii.first);
								ttk = iii.first;
								break;
							}
						}

					}
					PathA.push_back(0);
				}



				{
					int ttk = i;
					while (ttk != 0)
					{
						for (auto iii : haps[ttk].roads)
						{
							if (haps[iii.first].glu < haps[i].glu)
							{
								PathB.push_back(iii.first);
								ttk = iii.first;
								break;
							}
						}

					}
					PathB.push_back(0);
				}
				reverse(PathA.begin(), PathA.end());
				reverse(PathB.begin(), PathB.end());

				int ffh = 0;
				while (PathA.size() > ffh && PathB.size() > ffh && PathA[ffh] == PathB[ffh]) ffh++;
				ffh--;

				ans += haps[i].mod ^ haps[ii].mod ^ haps[PathA[ffh]].mod;



			}


		}
		else
		{
			haps[i].active = 0;

			for (size_t ii = 0; ii < N; ii++)
			{
				if (ii == i) continue;
				if (!haps[ii].active) continue;

				vector <int> PathA, PathB;



				{
					int ttk = ii;
					while (ttk != 0)
					{
						for (auto iii : haps[ttk].roads)
						{
							if (haps[iii.first].glu < haps[ii].glu)
							{
								PathA.push_back(iii.first);
								ttk = iii.first;
								break;
							}
						}

					}
					PathA.push_back(0);
				}



				{
					int ttk = i;
					while (ttk != 0)
					{
						for (auto iii : haps[ttk].roads)
						{
							if (haps[iii.first].glu < haps[i].glu)
							{
								PathB.push_back(iii.first);
								ttk = iii.first;
								break;
							}
						}

					}
					PathB.push_back(0);
				}
				reverse(PathA.begin(), PathA.end());
				reverse(PathB.begin(), PathB.end());

				
				int ffh = 0;
				while (PathA.size() > ffh && PathB.size() > ffh && PathA[ffh] == PathB[ffh]) ffh++;
				ffh--;

				ans -= haps[i].mod ^ haps[ii].mod ^ haps[PathA[ffh]].mod;



			}

		}
		anss.push_back(ans);

	}

	return anss;


}
