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: Accepted)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
#[allow(unused_imports)]
use proconio::input;
#[allow(unused_imports)]
use proconio::marker::*;

fn main() {
    input! {
        n: usize,
        q: usize,
        abcd: [(Usize1, Usize1, Usize1, i64); n - 1],
        xyuv: [(Usize1, i64, Usize1, Usize1); q],
    }
    let mut t = vec![vec![]; n];
    for (a, b, c, d) in abcd {
        t[a].push((b, c, d));
        t[b].push((a, c, d));
    }

    let mx_k = kyopro_lib::misc::log2p1(n) as usize + 1;
    let mut parent = vec![vec![None; mx_k]; n];
    let mut depth = vec![0u64; n];
    let mut dist = vec![0; n];
    {
        let mut q = std::collections::VecDeque::new();
        q.push_back((0, 0));
        while let Some((v, p)) = q.pop_front() {
            for &(u, _, d) in &t[v] {
                if u == p {
                    continue;
                }
                parent[u][0] = Some(v);
                depth[u] = depth[v] + 1;
                dist[u] = dist[v] + d;
                q.push_back((u, v));
            }
        }
    }

    for k in 0..mx_k - 1 {
        for i in 0..n {
            if let Some(p) = parent[i][k] {
                parent[i][k + 1] = parent[p][k];
            }
        }
    }

    // println!("{}", mx_k);

    // println!("{}", (0..n).map(|i| depth[i]).max().unwrap());

    // println!("{:?}", parent[99620]);
    // println!("{:?}", parent[32122]);

    use std::collections::BTreeSet;
    let mut lca = vec![0; q];
    let mut memo = vec![BTreeSet::new(); n];

    for i in 0..q {
        let (x, _, u, v) = xyuv[i];
        let w = {
            let (mut u, mut v) = (u, v);
            if depth[u] > depth[v] {
                std::mem::swap(&mut u, &mut v);
            }
            for k in 0..mx_k {
                if (((depth[v] - depth[u]) >> k) & 1) == 1 {
                    v = parent[v][k].unwrap();
                }
            }
            if u == v {
                u
            } else {
                for k in (0..mx_k).rev() {
                    if parent[u][k] != parent[v][k] {
                        u = parent[u][k].unwrap();
                        v = parent[v][k].unwrap();
                    }
                }
                parent[u][0].unwrap()
            }
        };
        lca[i] = w;
        memo[u].insert(x);
        memo[v].insert(x);
        memo[w].insert(x);
    }

    use std::collections::BTreeMap;
    fn dfs(
        t: &Vec<Vec<(usize, usize, i64)>>,
        dist: &mut Vec<(i64, i64)>,
        memo: &Vec<BTreeSet<usize>>,
        res: &mut Vec<BTreeMap<usize, (i64, i64)>>,
        v: usize,
        p: usize,
    ) {
        for &x in &memo[v] {
            res[v].insert(x, dist[x]);
        }
        for &(u, c, d) in &t[v] {
            if u == p {
                continue;
            }
            dist[c].0 += 1;
            dist[c].1 += d;
            dfs(t, dist, memo, res, u, v);
            dist[c].0 -= 1;
            dist[c].1 -= d;
        }
    }
    let mut res = vec![BTreeMap::new(); n];
    dfs(&t, &mut vec![(0, 0); n], &memo, &mut res, 0, 0);

    let f = |v: usize, c: usize, y: i64| -> i64 {
        let (n, d) = res[v].get(&c).unwrap();
        dist[v] - d + n * y
    };

    for i in 0..q {
        let (x, y, u, v) = xyuv[i];
        let w = lca[i];
        println!("{}", f(u, x, y) + f(v, x, y) - f(w, x, y) * 2)
    }
}
*/

fn main() {
    let exe = "/tmp/binB417834F";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";