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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using static System.Console;
using static System.Convert;
using static System.Math;
//using static System.Globalization.CultureInfo;
using System.Text;
class Program
{
    private static void chmin<T>(ref T num, T val) where T : IComparable<T>
        => num = num.CompareTo(val) == 1 ? val : num;
    private static void chmax<T>(ref T num, T val) where T : IComparable<T>
        => num = num.CompareTo(val) == -1 ? val : num;
    private static void swap<T>(ref T v1, ref T v2)
    { var t = v2; v2 = v1; v1 = t; }
    static void Main(string[] args)
    {
        var pr = new Program();
        var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);
        pr.Solve();
        Console.Out.Flush();
    }
    int[] dist;
    List<Pair<int, int, int>>[] edge;
    List<int>[] col;
    int[] coldist, colct;
    Dictionary<int,Pair<int,int>>[] dic;//頂点->色->dist,ct
    void Solve()
    {
        int num, q;
        Input.Make(out num, out q);
        edge = Input.edge<Pair<int, int, int>>(num);
        var lc = new HLTree(num);
        for(var i=0;i<num-1;i++)
        {
            var inp = Input.ar;
            inp[0]--;inp[1]--;
            lc.AddEdge(inp[0], inp[1]);
            for (var j = 0; j < 2; j++)
                edge[inp[j]].Add(new Pair<int, int, int>(inp[1^j], inp[2], inp[3]));
        }
        lc.Build();
        dist = new int[num];
        col = Input.edge<int>(num);
        coldist = new int[num];
        colct = new int[num];
        dic = Enumerable.Repeat(0, num).Select(_ => new Dictionary<int, Pair<int, int>>()).ToArray();
        var que = new int[q][];
        var lca = new int[q];
        for(var i = 0; i < q; i++)
        {
            que[i] = Input.ar;
            que[i][2]--;que[i][3]--;
            lca[i] = lc.GetLCA(que[i][2], que[i][3]);
            col[lca[i]].Add(que[i][0]);
            col[que[i][2]].Add(que[i][0]);
            col[que[i][3]].Add(que[i][0]);
        }
        dfs(0, -1);
        for(var i = 0; i < q; i++)
        {
            int u = que[i][2], v = que[i][3], c = que[i][0],y=que[i][1],p=lca[i];
            var d = dist[u] + dist[v] - 2 * dist[p];
            var sum = dic[u][c].v2 + dic[v][c].v2 - 2 * dic[p][c].v2;
            var ct = dic[u][c].v1 + dic[v][c].v1 - 2 * dic[p][c].v1;
            WriteLine(d - sum + y * ct);
        }
    }
    void dfs(int index,int pa)
    {
        foreach (var c in col[index])
            dic[index][c] = new Pair<int, int>(colct[c], coldist[c]);
        foreach (var ad in edge[index])
            if (ad.v1 != pa)
            {
                dist[ad.v1] = dist[index] + ad.v3;
                colct[ad.v2]++;
                coldist[ad.v2] += ad.v3;
                dfs(ad.v1, index);
                colct[ad.v2]--;
                coldist[ad.v2] -= ad.v3;
            }
    }

}

public class HLTree
{
    int n;
    List<int>[] G;
    int[] subtreeSize;
    int[] maxsize;
    int[] par;
    int[] d;

    Chain[] go;
    int[] pos;
    List<Chain> chains;
    public HLTree(int N)
    {
        n = N;
        G = new List<int>[n];
        chains = new List<Chain>();
        for (int i = 0; i < n; i++)
            G[i] = new List<int>();
        subtreeSize = new int[n];
        maxsize = new int[n];
        par = new int[n];
        d = new int[n];
        go = new Chain[n];
        pos = new int[n];
    }
    public void AddEdge(int u, int v)
    {
        G[u].Add(v); G[v].Add(u);
    }
    public void Build(int root = 0)
    {
        computeSubtreeSize(root);
        Decomposite(-1, root, 0);

    }
    void computeSubtreeSize(int r)
    {
        var stack = new Stack<int>();
        stack.Push(r);
        var iter = new int[n];
        par[r] = -1;
        //dfs
        while (stack.Count > 0)
        {
            var cur = stack.Peek();
            if (iter[cur] == G[cur].Count)
            {
                stack.Pop();
                subtreeSize[cur]++;
                if (par[cur] != -1)
                {
                    subtreeSize[par[cur]] += subtreeSize[cur];
                    maxsize[par[cur]] = Math.Max(maxsize[par[cur]], subtreeSize[cur]);
                }
                continue;
            }
            var next = G[cur][iter[cur]++];
            if (next == par[cur]) continue;
            par[next] = cur;
            d[next] = d[cur] + 1;
            stack.Push(next);
        }

    }
    void Decomposite(int from, int cur, int lv)
    {
        var chain = new Chain(from, lv);
        chains.Add(chain);
        chain.Add(cur);
        while (cur != from)
        {
            var next = cur;
            go[cur] = chain;
            pos[cur] = chain.Count;
            foreach (var to in G[cur])
            {
                if (to == from) continue;
                if (maxsize[cur] == subtreeSize[to])
                {
                    maxsize[cur] = -1;
                    next = to;
                    chain.Add(to);
                }
                else Decomposite(cur, to, lv + 1);
            }

            from = cur;
            cur = next;

        }
        chain.Init();

    }
    public int GetLCA(int u, int v)
    {
        while (go[u] != go[v])
        {
            if (go[u].Level < go[v].Level) v = go[v].From;
            else u = go[u].From;
        }
        if (d[u] <= d[v]) return u;
        else return v;
    }



}
public class Chain
{
    const bool DEBUG = true;
    public int From { get; set; }
    public int Level { get; set; }
    public int Count { get; private set; }
    List<int> heavy;
    public Chain(int from, int lv)
    {
        From = from;
        Level = lv;
        heavy = new List<int>();
    }
    public void Add(int v)
    {
        if (DEBUG)
            heavy.Add(v);
        Count++;
    }
    public void Init()
    {

    }
}


public class Input
{
    public static string read => ReadLine().Trim();
    public static int[] ar => read.Split(' ').Select(int.Parse).ToArray();
    public static int num => ToInt32(read);
    public static long[] arL => read.Split(' ').Select(long.Parse).ToArray();
    public static long numL => ToInt64(read);
    public static T[] create<T>(int n, Func<int, T> f)
        => Enumerable.Repeat(0, n).Select(f).ToArray();
    public static char[][] grid(int h)
        => create(h, _ => read.ToCharArray());
    public static int[] ar1D(int n)
        => create(n, _ => num);
    public static long[] arL1D(int n)
        => create(n, _ => numL);
    public static string[] strs(int n)
        => create(n, _ => read);
    public static int[][] ar2D(int n)
        => create(n, _ => ar);
    public static long[][] arL2D(int n)
        => create(n, _ => arL);
    public static List<T>[] edge<T>(int n)
        => create(n, _ => new List<T>());
    public static T GetValue<T>(string g)
    {
        var t = typeof(T);
        if (t == typeof(int))
            return (T)(object)int.Parse(g);
        if (t == typeof(long))
            return (T)(object)long.Parse(g);
        if (t == typeof(string))
            return (T)(object)g;
        if (t == typeof(char))
            return (T)(object)char.Parse(g);
        if (t == typeof(double))
            return (T)(object)double.Parse(g);
        if (t == typeof(bool))
            return (T)(object)bool.Parse(g);
        return default(T);
    }
    public static void Make<T1, T2>(out T1 v1, out T2 v2)
    {
        v1 = Next<T1>();
        v2 = Next<T2>();
    }
    public static void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3)
    {
        Make(out v1, out v2);
        v3 = Next<T3>();
    }
    public static void Make<T1, T2, T3, T4>(out T1 v1, out T2 v2, out T3 v3, out T4 v4)
    {
        Make(out v1, out v2, out v3);
        v4 = Next<T4>();
    }
    public static void Make<T1, T2, T3, T4, T5>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5)
    {
        Make(out v1, out v2, out v3, out v4);
        v5 = Next<T5>();
    }
    public static void Make<T1, T2, T3, T4, T5, T6>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6)
    {
        Make(out v1, out v2, out v3, out v4, out v5);
        v6 = Next<T6>();
    }
    private static Queue<string> sc;
    public static T Next<T>() { sc = sc ?? new Queue<string>(); if (sc.Count == 0) foreach (var item in read.Split(' ')) sc.Enqueue(item); return GetValue<T>(sc.Dequeue()); }
    public static void Next<T>(ref T val) => val = Next<T>();
    public const long Inf = (long)1e18;
    public const double eps = 1e-6;
    public const string Alfa = "abcdefghijklmnopqrstuvwxyz";
    public const int MOD = 1000000007;
}

public class Pair<T1, T2> : IComparable<Pair<T1, T2>>
{
    public T1 v1 { get; set; }
    public T2 v2 { get; set; }
    public Pair() { v1 = Input.Next<T1>(); v2 = Input.Next<T2>(); }
    public Pair(T1 v1, T2 v2)
    { this.v1 = v1; this.v2 = v2; }

    public int CompareTo(Pair<T1, T2> p)
    {
        var c = Comparer<T1>.Default.Compare(v1, p.v1);
        if (c == 0)
            c = Comparer<T2>.Default.Compare(v2, p.v2);
        return c;
    }
    public override string ToString()
        => $"{v1.ToString()} {v2.ToString()}";
    public override bool Equals(object obj)
        => this == (Pair<T1, T2>)obj;
    public override int GetHashCode()
        => v1.GetHashCode() ^ v2.GetHashCode();
    public static bool operator ==(Pair<T1, T2> p1, Pair<T1, T2> p2)
        => p1.CompareTo(p2) == 0;
    public static bool operator !=(Pair<T1, T2> p1, Pair<T1, T2> p2)
        => p1.CompareTo(p2) != 0;
    public static bool operator >(Pair<T1, T2> p1, Pair<T1, T2> p2)
        => p1.CompareTo(p2) == 1;
    public static bool operator >=(Pair<T1, T2> p1, Pair<T1, T2> p2)
        => p1.CompareTo(p2) != -1;
    public static bool operator <(Pair<T1, T2> p1, Pair<T1, T2> p2)
        => p1.CompareTo(p2) == -1;
    public static bool operator <=(Pair<T1, T2> p1, Pair<T1, T2> p2)
        => p1.CompareTo(p2) != 1;
}

public class Pair<T1, T2, T3> : Pair<T1, T2>, IComparable<Pair<T1, T2, T3>>
{
    public T3 v3 { get; set; }
    public Pair() : base() { v3 = Input.Next<T3>(); }
    public Pair(T1 v1, T2 v2, T3 v3) : base(v1, v2)
    { this.v3 = v3; }

    public int CompareTo(Pair<T1, T2, T3> p)
    {
        var c = base.CompareTo(p);
        if (c == 0)
            c = Comparer<T3>.Default.Compare(v3, p.v3);
        return c;
    }
    public override string ToString()
        => $"{base.ToString()} {v3.ToString()}";
}