//root -> 頂点vの情報を事前に計算. dfsしながら差分計算.
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

struct Edge {
	int to, color, cost;
	Edge() { }
	Edge(int to, int color, int cost) { this->to = to; this->color = color; this->cost = cost; }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int n, q;
Graph tree;
int x[100000], y[100000], u[100000], v[100000];

int parent[100000];
int depth[100000];
int length[100000];				//length[頂点v] = 頂点0->vの長さ
int parents[20][100000];

void dfs(int p, int v, int dep = 0, int len = 0) {
	int i;
	
	parent[v] = p;
	depth[v] = dep;
	length[v] = len;
	
	for (Edge e : tree[v]) {
		if (e.to == p) continue;
		dfs(v, e.to, dep + 1, len + e.cost);
	}
}

void prepare() {
	int i, j;
	rep(i, n) parents[0][i] = parent[i];
	rep(i, 19) {
		rep(j, n) {
			parents[i + 1][j] = parents[i][parents[i][j]];
		}
	}
}

int lca(int u, int v) {
	if (depth[u] < depth[v]) { swap(u, v); }
	
	int d = depth[u] - depth[v];
	for (int i = 19; i >= 0; i--) {
		if ((d >> i) & 1) {
			u = parents[i][u];
		}
	}
	
	for (int i = 19; i >= 0; i--) {
		if (parents[i][u] != parents[i][v]) {
			u = parents[i][u];
			v = parents[i][v];
		}
	}
	
	if (u == v) return u;
	return parent[u];
}

vector<int> queryC[100000];
map<int, int> cntMp[100000];	//cntMp[頂点v] = (色c, 頂点0->vで出現する本数)
map<int, int> costMp[100000];	//costMp[頂点v] = (色c, 頂点0->vでの合計長さ)
int cntC[100000];
int costC[100000];

void dfs2(int p, int v) {
	for (int c : queryC[v]) {
		cntMp[v][c] = cntC[c];
		costMp[v][c] = costC[c];
	}
	
	for (Edge e : tree[v]) {
		if (e.to == p) continue;
		
		cntC[e.color]++;
		costC[e.color] += e.cost;
		dfs2(v, e.to);
		cntC[e.color]--;
		costC[e.color] -= e.cost;
	}
}

int main() {
	int i;
	
	cin >> n >> q;
	tree.resize(n);
	
	rep(i, n - 1) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		a--; b--;
		tree[a].push_back(Edge(b, c, d));
		tree[b].push_back(Edge(a, c, d));
	}
	
	dfs(0, 0);
	prepare();
	
	rep(i, q) {
		cin >> x[i] >> y[i] >> u[i] >> v[i];
		u[i]--; v[i]--;
		int l = lca(u[i], v[i]);
		queryC[u[i]].push_back(x[i]);
		queryC[v[i]].push_back(x[i]);
		queryC[l].push_back(x[i]);
	}
	
	dfs2(0, 0);
	
	rep(i, q) {
		int l = lca(u[i], v[i]);
		int res = length[u[i]] + length[v[i]] - 2 * length[l];
		int honsu = cntMp[u[i]][x[i]] + cntMp[v[i]][x[i]] - 2 * cntMp[l][x[i]];
		int sumCost = costMp[u[i]][x[i]] + costMp[v[i]][x[i]] - 2 * costMp[l][x[i]];
		cout << res + honsu * y[i] - sumCost << endl;
	}
	return 0;
}
### Prompt

Develop a solution in CPP to the problem described below:

There is a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and Vertex b_i, and the color and length of that edge are c_i and d_i, respectively. Here the color of each edge is represented by an integer between 1 and N-1 (inclusive). The same integer corresponds to the same color, and different integers correspond to different colors.

Answer the following Q queries:

* Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.)

Constraints

* 2 \leq N \leq 10^5
* 1 \leq Q \leq 10^5
* 1 \leq a_i, b_i \leq N
* 1 \leq c_i \leq N-1
* 1 \leq d_i \leq 10^4
* 1 \leq x_j \leq N-1
* 1 \leq y_j \leq 10^4
* 1 \leq u_j < v_j \leq N
* The given graph is a tree.
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q


Output

Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to Query j.

Example

Input

5 3
1 2 1 10
1 3 2 20
2 4 4 30
5 2 1 40
1 100 1 4
1 100 1 5
3 1000 3 4


Output

130
200
60

### Response

```cpp
//root -> 頂点vの情報を事前に計算. dfsしながら差分計算.
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

struct Edge {
	int to, color, cost;
	Edge() { }
	Edge(int to, int color, int cost) { this->to = to; this->color = color; this->cost = cost; }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int n, q;
Graph tree;
int x[100000], y[100000], u[100000], v[100000];

int parent[100000];
int depth[100000];
int length[100000];				//length[頂点v] = 頂点0->vの長さ
int parents[20][100000];

void dfs(int p, int v, int dep = 0, int len = 0) {
	int i;
	
	parent[v] = p;
	depth[v] = dep;
	length[v] = len;
	
	for (Edge e : tree[v]) {
		if (e.to == p) continue;
		dfs(v, e.to, dep + 1, len + e.cost);
	}
}

void prepare() {
	int i, j;
	rep(i, n) parents[0][i] = parent[i];
	rep(i, 19) {
		rep(j, n) {
			parents[i + 1][j] = parents[i][parents[i][j]];
		}
	}
}

int lca(int u, int v) {
	if (depth[u] < depth[v]) { swap(u, v); }
	
	int d = depth[u] - depth[v];
	for (int i = 19; i >= 0; i--) {
		if ((d >> i) & 1) {
			u = parents[i][u];
		}
	}
	
	for (int i = 19; i >= 0; i--) {
		if (parents[i][u] != parents[i][v]) {
			u = parents[i][u];
			v = parents[i][v];
		}
	}
	
	if (u == v) return u;
	return parent[u];
}

vector<int> queryC[100000];
map<int, int> cntMp[100000];	//cntMp[頂点v] = (色c, 頂点0->vで出現する本数)
map<int, int> costMp[100000];	//costMp[頂点v] = (色c, 頂点0->vでの合計長さ)
int cntC[100000];
int costC[100000];

void dfs2(int p, int v) {
	for (int c : queryC[v]) {
		cntMp[v][c] = cntC[c];
		costMp[v][c] = costC[c];
	}
	
	for (Edge e : tree[v]) {
		if (e.to == p) continue;
		
		cntC[e.color]++;
		costC[e.color] += e.cost;
		dfs2(v, e.to);
		cntC[e.color]--;
		costC[e.color] -= e.cost;
	}
}

int main() {
	int i;
	
	cin >> n >> q;
	tree.resize(n);
	
	rep(i, n - 1) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		a--; b--;
		tree[a].push_back(Edge(b, c, d));
		tree[b].push_back(Edge(a, c, d));
	}
	
	dfs(0, 0);
	prepare();
	
	rep(i, q) {
		cin >> x[i] >> y[i] >> u[i] >> v[i];
		u[i]--; v[i]--;
		int l = lca(u[i], v[i]);
		queryC[u[i]].push_back(x[i]);
		queryC[v[i]].push_back(x[i]);
		queryC[l].push_back(x[i]);
	}
	
	dfs2(0, 0);
	
	rep(i, q) {
		int l = lca(u[i], v[i]);
		int res = length[u[i]] + length[v[i]] - 2 * length[l];
		int honsu = cntMp[u[i]][x[i]] + cntMp[v[i]][x[i]] - 2 * cntMp[l][x[i]];
		int sumCost = costMp[u[i]][x[i]] + costMp[v[i]][x[i]] - 2 * costMp[l][x[i]];
		cout << res + honsu * y[i] - sumCost << endl;
	}
	return 0;
}
```