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 Rust (1.15.1) (judge verdict: Wrong Answer)

macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( iter = $ iter : ident , $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut $ iter = s . split_whitespace ( ) ; input_inner ! { $ iter , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut iter = s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; }
macro_rules ! input_inner { ( $ iter : expr ) => { } ; ( $ iter : expr , ) => { } ; ( $ iter : expr , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , mut $ var : usize $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , $ var : usize $ ( $ r ) * } } ; }
macro_rules ! read_value { ( $ iter : expr , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ iter , $ t ) ) ,* ) } ; ( $ iter : expr , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ iter , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ iter : expr , { chars : $ base : expr } ) => { read_value ! ( $ iter , String ) . chars ( ) . map ( | c | ( c as u8 - $ base as u8 ) as usize ) . collect ::< Vec < usize >> ( ) } ; ( $ iter : expr , { char : $ base : expr } ) => { read_value ! ( $ iter , { chars : $ base } ) [ 0 ] } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , char ) => { read_value ! ( $ iter , chars ) [ 0 ] } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; }
pub trait Monoid {
    type T: Clone + PartialEq;
    fn mempty(&self) -> Self::T;
    fn mappend(&self, x: &Self::T, y: &Self::T) -> Self::T;
}
pub struct SegmentTree<M: Monoid> {
    n: usize,
    seg: Vec<M::T>,
    m: M,
}
impl<M: Monoid> SegmentTree<M> {
    pub fn new(n: usize, m: M) -> SegmentTree<M> {
        let n = 1 << format!("{:b}", n - 1).len();
        let seg = vec![m.mempty(); 2 * n - 1];
        SegmentTree {
            n: n,
            seg: seg,
            m: m,
        }
    }
    pub fn from_vec(v: Vec<M::T>, m: M) -> SegmentTree<M> {
        let n = 1 << format!("{:b}", v.len() - 1).len();
        let mut seg = vec![m.mempty(); 2 * n - 1];
        for (i, x) in v.into_iter().enumerate() {
            seg[i + n - 1] = x;
        }
        for i in (0..n - 1).rev() {
            seg[i] = m.mappend(&seg[2 * i + 1], &seg[2 * i + 2]);
        }
        SegmentTree {
            n: n,
            seg: seg,
            m: m,
        }
    }
    pub fn update(&mut self, k: usize, x: M::T) {
        let mut k = k + self.n - 1;
        self.seg[k] = x;
        while k > 0 {
            k = (k - 1) / 2;
            self.seg[k] = self.m.mappend(&self.seg[2 * k + 1], &self.seg[2 * k + 2]);
        }
    }
    pub fn query(&self, l: usize, r: usize) -> M::T {
        let mut l = l + self.n - 1;
        let mut r = r + self.n - 1;
        let mut res = self.m.mempty();
        while l < r {
            if r & 1 == 0 {
                r -= 1;
                res = self.m.mappend(&res, &self.seg[r]);
            }
            if l & 1 == 0 {
                res = self.m.mappend(&res, &self.seg[l]);
                l += 1;
            }
            l = (l - 1) / 2;
            r = (r - 1) / 2;
        }
        res
    }
}
pub struct MinMonoid<T: Ord + Clone> {
    min: T,
}
impl<T: Ord + Clone> MinMonoid<T> {
    pub fn new(min: T) -> MinMonoid<T> {
        MinMonoid { min: min }
    }
}
impl<T: Ord + Clone> Monoid for MinMonoid<T> {
    type T = T;
    fn mempty(&self) -> T {
        self.min.clone()
    }
    fn mappend(&self, x: &T, y: &T) -> T {
        std::cmp::min(x, y).clone()
    }
}
use std::collections::HashMap;
#[derive(Debug)]
pub struct TreeRec {
    n: usize,
    graph: Vec<Vec<(usize, usize, usize)>>,
    left: Vec<usize>,
    right: Vec<usize>,
    euler: Vec<(usize, usize)>,
    par: Vec<(usize, usize)>,
    query: Vec<(usize, usize, usize, usize)>,
    vc: Vec<HashMap<usize, (usize, usize)>>,
    acc: Vec<usize>,
}
impl TreeRec {
    pub fn new(n: usize, query: Vec<(usize, usize, usize, usize)>) -> TreeRec {
        let mut vc = (0..n).map(|_| HashMap::new()).collect::<Vec<_>>();
        for &(x, _y, u, v) in &query {
            vc[u].insert(x, (0, 0));
            vc[v].insert(x, (0, 0));
        }
        TreeRec {
            n: n,
            graph: vec![vec![]; n],
            left: vec![0; n],
            right: vec![0; n],
            euler: vec![],
            par: vec![(0, 0); n],
            query: query,
            vc: vc,
            acc: vec![0; n],
        }
    }
    pub fn add_edge(&mut self, u: usize, v: usize, c: usize, d: usize) {
        self.graph[u].push((v, c, d));
        self.graph[v].push((u, c, d));
    }
    pub fn dfs(&mut self, u: usize, p: usize, depth: usize) {
        self.left[u] = self.euler.len();
        self.euler.push((depth, u));
        for i in 0..self.graph[u].len() {
            let (v, _c, _d) = self.graph[u][i];
            if v != p {
                self.dfs(v, u, depth + 1);
                self.euler.push((depth, u));
            }
        }
        self.right[u] = self.euler.len() - 1;
    }
    pub fn dfs2(&mut self, u: usize, p: usize, depth: usize) {
        self.acc[u] = depth;
        let keys = self.vc[u].keys().cloned().collect::<Vec<_>>();
        for &c in &keys {
            self.vc[u].insert(c, self.par[c]);
        }
        for i in 0..self.graph[u].len() {
            let (v, c, d) = self.graph[u][i];
            if v != p {
                let (l, m) = self.par[c];
                self.par[c] = (l + d, m + 1);
                self.dfs2(v, u, depth + d);
                self.par[c] = (l, m);
            }
        }
    }
    pub fn solve(&mut self) {
        let n = self.n;
        self.dfs(0, n, 0);
        let seg = SegmentTree::from_vec(self.euler.clone(), MinMonoid::new((std::usize::MAX, 0)));
        for &(x, _y, u, v) in &self.query {
            let p = min(
                seg.query(self.left[u], self.right[v] + 1),
                seg.query(self.left[v], self.right[u] + 1),
            )
            .1;
            self.vc[p].insert(x, (0, 0));
        }
        self.dfs2(0, n, 0);
        for &(x, y, u, v) in &self.query {
            let p = min(
                seg.query(self.left[u], self.right[v] + 1),
                seg.query(self.left[v], self.right[u] + 1),
            )
            .1;
            let &(lp, mp) = self.vc[p].get(&x).unwrap();
            let &(lu, mu) = self.vc[u].get(&x).unwrap();
            let &(lv, mv) = self.vc[v].get(&x).unwrap();
            let dp = self.acc[p] + y * mp - lp;
            let du = self.acc[u] + y * mu - lu;
            let dv = self.acc[v] + y * mv - lv;
            let ans = du + dv - dp;
            println!("{}", ans);
        }
    }
}
use std::cmp::min;
fn main() {
    input! { n, q, abcd: [(usize1,usize1,usize1,usize); n-1], xyuv: [(usize1,usize,usize1,usize1); q] };
    let mut rec = TreeRec::new(n, xyuv);
    for &(a, b, c, d) in &abcd {
        rec.add_edge(a, b, c, d);
    }
    rec.solve();
}