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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;

public class Main {

	static PrintWriter out;
	static InputReader ir;
	static long[] f, fi;

	static void solve() {
		int n = ir.nextInt();
		int q = ir.nextInt();
		Graph[] g = new Graph[n];
		Graph[] col = new Graph[n];
		for (int i = 0; i < n; i++) {
			g[i] = new Graph();
			col[i] = new Graph();
		}
		for (int i = 0; i < n - 1; i++) {
			int u = ir.nextInt() - 1;
			int v = ir.nextInt() - 1;
			int c = ir.nextInt();
			int d = ir.nextInt();
			g[u].add(new int[] { v, c, d });
			g[v].add(new int[] { u, c, d });
		}
		int[] x = new int[q];
		int[] y = new int[q];
		int[] u = new int[q];
		int[] v = new int[q];
		int[] lca = new int[q];
		int[] pars = new int[n];
		int[] dep = new int[n];
		Arrays.fill(pars, -1);
		_dfs(0, -1, g, pars, dep);
		lca_init(n, pars);
		for (int i = 0; i < q; i++) {
			x[i] = ir.nextInt();
			y[i] = ir.nextInt();
			u[i] = ir.nextInt() - 1;
			v[i] = ir.nextInt() - 1;
			lca[i] = lca(u[i], v[i], dep);
			col[u[i]].add(new int[] { x[i] });
			col[v[i]].add(new int[] { x[i] });
			col[lca[i]].add(new int[] { x[i] });
		}
		int[] ct = new int[n];
		int[] dist = new int[n];
		long[] dd = new long[n];
		HashMap<Long, Integer> mp = new HashMap<>();
		HashMap<Long, Integer> mp2 = new HashMap<>();
		dfs(0, -1, g, col, ct, dist, dd, mp, mp2);
		for (int i = 0; i < q; i++) {
			long res = dd[u[i]] + dd[v[i]] - 2 * dd[lca[i]];
			res -= mp2.get((long) u[i] << 30L | x[i]);
			res -= mp2.get((long) v[i] << 30L | x[i]);
			res += 2 * (long) mp2.get((long) lca[i] << 30L | x[i]);
			res += (long) mp.get((long) u[i] << 30L | x[i]) * y[i];
			res += (long) mp.get((long) v[i] << 30L | x[i]) * y[i];
			res -= 2L * (long) mp.get((long) lca[i] << 30L | x[i]) * y[i];
			out.println(res);
		}
	}

	static void _dfs(int cur, int par, Graph[] g, int[] pars, int[] dep) {
		pars[cur] = par;
		for (int[] to : g[cur]) {
			if (to[0] == par)
				continue;
			dep[to[0]] = dep[cur] + 1;
			_dfs(to[0], cur, g, pars, dep);
		}
	}

	static void dfs(int cur, int par, Graph[] g, Graph[] col, int[] ct, int[] dist, long[] dd,
			HashMap<Long, Integer> mp, HashMap<Long, Integer> mp2) {
		int n = g.length;
		for (int[] cc : col[cur]) {
			int c = cc[0];
			mp.put((long) cur << 30L | (long) c, ct[c]);
			mp2.put((long) cur << 30L | (long) c, dist[c]);
		}
		for (int[] to : g[cur]) {
			if (to[0] == par)
				continue;
			ct[to[1]]++;
			dist[to[1]] += to[2];
			dd[to[0]] = dd[cur] + to[2];
			dfs(to[0], cur, g, col, ct, dist, dd, mp, mp2);
			ct[to[1]]--;
			dist[to[1]] -= to[2];
		}
	}

	static int MAX_V = (int) 1e5 + 1, MAX_LOG_V = 17;
	static int[][] lca_par = new int[MAX_LOG_V][MAX_V];

	public static void lca_init(int v, int[] par) {
		for (int i = 0; i < v; i++) {
			lca_par[0][i] = par[i];
		}
		for (int i = 0; i < MAX_LOG_V - 1; i++) {
			for (int j = 0; j < v; j++) {
				if (lca_par[i][j] < 0)
					lca_par[i + 1][j] = -1;
				else
					lca_par[i + 1][j] = lca_par[i][lca_par[i][j]];
			}
		}
	}

	public static int lca(int u, int v, int[] depth) {
		if (depth[u] > depth[v]) {
			int temp = u;
			u = v;
			v = temp;
		}
		for (int i = 0; i < MAX_LOG_V; i++) {
			if (((depth[v] - depth[u]) >> i & 1) != 0) {
				v = lca_par[i][v];
			}
		}
		if (u == v)
			return u;
		for (int i = MAX_LOG_V - 1; i >= 0; i--) {
			if (lca_par[i][v] != lca_par[i][u]) {
				v = lca_par[i][v];
				u = lca_par[i][u];
			}
		}
		return lca_par[0][u];
	}

	static class Graph extends ArrayList<int[]> {
	}

	public static void main(String[] args) {
		ir = new InputReader(System.in);
		out = new PrintWriter(System.out);
		solve();
		out.flush();
	}

	static class InputReader {

		private InputStream in;
		private byte[] buffer = new byte[1024];
		private int curbuf;
		private int lenbuf;

		public InputReader(InputStream in) {
			this.in = in;
			this.curbuf = this.lenbuf = 0;
		}

		public boolean hasNextByte() {
			if (curbuf >= lenbuf) {
				curbuf = 0;
				try {
					lenbuf = in.read(buffer);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return false;
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[curbuf++];
			else
				return -1;
		}

		private boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		private void skip() {
			while (hasNextByte() && isSpaceChar(buffer[curbuf]))
				curbuf++;
		}

		public boolean hasNext() {
			skip();
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (!isSpaceChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public int nextInt() {
			if (!hasNext())
				throw new NoSuchElementException();
			int c = readByte();
			while (isSpaceChar(c))
				c = readByte();
			boolean minus = false;
			if (c == '-') {
				minus = true;
				c = readByte();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res = res * 10 + c - '0';
				c = readByte();
			} while (!isSpaceChar(c));
			return (minus) ? -res : res;
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			int c = readByte();
			while (isSpaceChar(c))
				c = readByte();
			boolean minus = false;
			if (c == '-') {
				minus = true;
				c = readByte();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res = res * 10 + c - '0';
				c = readByte();
			} while (!isSpaceChar(c));
			return (minus) ? -res : res;
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

		public int[] nextIntArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		public long[] nextLongArray(int n) {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			return a;
		}

		public char[][] nextCharMap(int n, int m) {
			char[][] map = new char[n][m];
			for (int i = 0; i < n; i++)
				map[i] = next().toCharArray();
			return map;
		}
	}

	static void tr(Object... o) {
		out.println(Arrays.deepToString(o));
	}
}