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: Time Limit Exceeded)

#include <bits/stdc++.h>

using namespace std;

#define int long long
// #define double long double
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, a, b) for(int i = (a); i > (b); --i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOREACH(x, a) for(auto &(x) : (a))
#define VECCIN(x)                                                              \
    for(auto &youso_ : (x)) cin >> youso_
#define bitcnt(x) __builtin_popcount(x)
#define lbit(x) __builtin_ffsll(x)
#define rbit(x) __builtin_clzll(x)
#define SZ(x) ((long long)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
#define rAll(a) (a).rbegin(), (a).rend()
#define cinfast() cin.tie(0), ios::sync_with_stdio(false)
#define PERM(c)                                                                \
    sort(All(c));                                                              \
    for(bool cp = true; cp; cp = next_permutation(All(c)))
#define MKORDER(n)                                                             \
    vector<int> od(n);                                                         \
    iota(All(od), 0LL);

template <typename T = long long> inline T IN() {
    T x;
    cin >> x;
    return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&... tail) {
    cin >> head;
    CIN(move(tail)...);
}
#define CCIN(...)                                                              \
    char __VA_ARGS__;                                                          \
    CIN(__VA_ARGS__)
#define DCIN(...)                                                              \
    double __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define LCIN(...)                                                              \
    long long __VA_ARGS__;                                                     \
    CIN(__VA_ARGS__)
#define SCIN(...)                                                              \
    string __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v)                                                              \
    {                                                                          \
        FOREACH(x, v) { cout << x << " "; }                                    \
        cout << "\n";                                                          \
    }
template <typename T = string> inline void eputs(T s) {
    cout << s << "\n";
    exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
    std::fill((T *)array, (T *)(array + N), val);
}

template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;

typedef long long ll;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<ll, ll> PL;
typedef vector<PL> VPL;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<string> VS;

const int INF = 1e9;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll LINF = 1e18;
const double PI = atan(1.0) * 4.0;
const ll dw[] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dh[] = {0, 1, 1, 1, 0, -1, -1, -1};
#define PI 3.141592653589793238

ll N, M;
struct edge {
    ll to, col, len;
};
vector<vector<edge>> g;
VL d;

// doubling build:n query:logn
struct LCA {
    int n, h;
    vector<vector<int>> G, par;
    vector<int> dep;
    LCA() {}
    LCA(int n) : n(n), G(n), dep(n) {
        h = 1;
        while((1 << h) <= n) h++;
        par.assign(h, vector<int>(n, -1));
    }

    void add_edge(int u, int v) {
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    void dfs(int v, int p, int d) {
        par[0][v] = p;
        dep[v] = d;
        for(int u : G[v])
            if(u != p) dfs(u, v, d + 1);
    }

    void build(int r = 0) {
        dfs(r, -1, 0);
        for(int k = 0; k + 1 < h; k++)
            for(int v = 0; v < n; v++)
                if(~par[k][v]) par[k + 1][v] = par[k][par[k][v]];
    }

    int lca(int u, int v) {
        if(dep[u] > dep[v]) swap(u, v);
        REP(k, h) {
            if(((dep[v] - dep[u]) >> k) & 1) v = par[k][v];
        }
        if(u == v) return u;

        for(int k = h - 1; k >= 0; k--)
            if(par[k][u] != par[k][v]) u = par[k][u], v = par[k][v];

        return par[0][u];
    }

    int distance(int u, int v) { return dep[u] + dep[v] - dep[lca(u, v)] * 2; }
};

ll ans[100100], clen[100100], ccnt[100100];
using tp = tuple<ll, ll, ll, ll>;
vector<vector<tp>> qs;

void dfs(ll now, ll par, ll d) {
    FOREACH(q, qs[now]) {
        ll idx, col, len, mul;
        tie(idx, col, len, mul) = q;
        ans[idx] += (d - clen[col] + ccnt[col] * len) * mul;
    }
    FOREACH(e, g[now]) {
        ll to = e.to, col = e.col, len = e.len;
        if(to == par) continue;
        clen[col] += len;
        ccnt[col]++;
        dfs(to, now, d + len);
        clen[col] -= len;
        ccnt[col]--;
    }
}

signed main() {
    cinfast();
    LCIN(N, Q);
    LCA lca(N);
    g.resize(N);
    REP(i, N - 1) {
        LCIN(a, b, c, d);
        a--, b--;
        g[a].push_back({b, c, d});
        g[b].push_back({a, c, d});
        lca.add_edge(a, b);
        lca.add_edge(b, a);
    }
    lca.build();
    qs.resize(N);
    REP(i, Q) {
        LCIN(x, y, u, v);
        u--, v--;
        qs[u].emplace_back(i, x, y, 1);
        qs[v].emplace_back(i, x, y, 1);
        qs[lca.lca(u, v)].emplace_back(i, x, y, -2);
    }
    dfs(0, -1, 0);
    REP(i, Q) { cout << ans[i] << "\n"; }
}