Problem:
Score : 600 points

Problem Statement

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.

Sample Input 1

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

Sample Output 1

130
200
60

The graph in this input is as follows:

Here the edges of Color 1 are shown as solid red lines, the edge of Color 2 is shown as a bold green line, and the edge of Color 4 is shown as a blue dashed line.

Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130.

Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200.

Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

const int cm = 1 << 17;
char cn[cm], * ci = cn + cm, ct;
inline char getcha() {
	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }
	return *ci++;
}
inline int getint() {
	int A = 0;
	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';
	return A;
}

const int dm = 1 << 20;
char dn[dm], * di = dn;
inline void putint(int X) {
	int keta = 0;
	char C[10];
	while (X) {
		*(C + keta) = '0' + X % 10;
		X /= 10;
		keta++;
	}
	for (int i = keta - 1; i >= 0; i--)* di++ = (*(C + i));
	*di++ = '\n';
}

pair<pair<int,int>, pair<int, int>> E[200001];
int H[100001];
int P[100001][17];
int D[100001], RD[100001];

int oya(int A, int B) {
	if (B == 0) return A;
	int k = 31 - __builtin_clz(B);
	return oya(P[A][k], B - (1 << k));
}
void dfs(int A, int prev, int rd, int d) {
	RD[A] = rd;
	D[A] = d;
	P[A][0] = prev;
	int mae = prev;
	rep(i, 16) {
		P[A][i + 1] = P[mae][i];
		mae = P[A][i + 1];
	}
	for (int i = H[A]; i; i = E[i].first.second) {
		if (E[i].first.first != prev) dfs(E[i].first.first, A, rd + E[i].second.first, d + 1);
	}
}

pair<int, int> QE[300001];
int QH[100001];
int CD[100001], CN[100001];
void dfs2(int A, int C) {
	int tmp;
	for (int i = QH[A]; i; i = tmp) {
		tmp = QE[i].second;
		QE[i].second = CN[QE[i].first];
		QE[i].first = CD[QE[i].first];
	}
	for (int i = H[A]; i; i = E[i].first.second) {
		if (E[i].first.first != C) {
			CD[E[i].second.second] += E[i].second.first;
			CN[E[i].second.second]++;
			dfs2(E[i].first.first, A);
			CD[E[i].second.second] -= E[i].second.first;
			CN[E[i].second.second]--;
		}
	}
}


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	
	int N = getint(), Q = getint();
	for (int i = 1; i < N + N - 1; i++) {
		int a = getint(), b = getint(), c = getint(), d = getint();
		E[i] = mp(mp(b, H[a]), mp(d, c));
		H[a] = i++;
		E[i] = mp(mp(a, H[b]), mp(d, c));
		H[b] = i;
	}

	dfs(1, 0, 0, 0);

	int motoD[100000], y[100000];
	rep(i, Q) {
		int x = getint();
		y[i] = getint();
		int u = getint(), v = getint();

		if (D[u] > D[v]) swap(u, v);
		int sa = D[v] - D[u];

		int u2 = u;
		int v2 = oya(v, sa);
		int lca = u;
		if (u2 != v2) {
			for (int k = 16; k >= 0; k--) {
				if (P[u2][k] != P[v2][k]) {
					u2 = P[u2][k];
					v2 = P[v2][k];
				}
			}
			lca = P[u2][0];
		}
		motoD[i] = RD[u] + RD[v] - RD[lca] * 2;
		QE[i * 3 + 1] = mp(x, QH[u]);
		QH[u] = i * 3 + 1;
		QE[i * 3 + 2] = mp(x, QH[v]);
		QH[v] = i * 3 + 2;
		QE[i * 3 + 3] = mp(x, QH[lca]);
		QH[lca] = i * 3 + 3;
	}

	dfs2(1, 0);

	rep(i, Q) {
		motoD[i] -= QE[i * 3 + 1].first + QE[i * 3 + 2].first - 2 * QE[i * 3 + 3].first;
		motoD[i] += (QE[i * 3 + 1].second + QE[i * 3 + 2].second - 2 * QE[i * 3 + 3].second) * y[i];
		putint(motoD[i]);
	}

	fwrite_unlocked(dn, di - dn, 1, stdout);

	Would you please return 0;
}