#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

const int MAXN = 1e5+10;
const int MAXL = 2*sqrt(MAXN)+10;
const int L = 18;

int n, q;
vector<pair<int,int>> adj[MAXN]; // < v, index of edge >
int a[MAXN], b[MAXN], c[MAXN], d[MAXN];

int depth[MAXN], be[MAXN], en[MAXN];
vector<int> edges;
vector<int> vert;
int vbe[MAXN], ven[MAXN];
int ans[MAXN];

int ql[MAXN], qr[MAXN], qc[MAXN], qd[MAXN];
bool comp(int i, int j) {
  return (ql[i]/MAXL < ql[j]/MAXL || (ql[i]/MAXL == ql[j]/MAXL && qr[i] < qr[j]));
}

void dfs(int u, int p=-1) {
  be[u] = edges.size();
  for(auto pp: adj[u]) {
    int v = pp.first;
    int id = pp.second;
    if (v==p) continue;
    depth[v] = depth[u]+1;
    edges.push_back(id);
    dfs(v,u);
    edges.push_back(id);
  }
  en[u] = edges.size()-1;
}

bool eset[MAXN];
ll ccnt[MAXN];
ll cold[MAXN];//sum of lengths for each c
ll totd;

void add(int i) {
  i = edges[i]; //d[i] and c[i]
  //cerr << "adding " << i << " " << c[i] << endl;
  int m = eset[i]? -1:1;
  totd+=m*d[i];
  cold[c[i]]+=m*d[i];
  ccnt[c[i]]+=m;
  eset[i]^=1;
}

int main(){
  cin.tie(0); ios_base::sync_with_stdio(0);
  cin >> n >> q;
  for(int i=0;i<n-1;i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    adj[a[i]].emplace_back(b[i],i);
    adj[b[i]].emplace_back(a[i],i);
  }
  dfs(1);
  //cerr << "EDGES " << endl; for(int i:edges) cerr << i << " "; cerr <<endl;
  //cerr << "be " << endl; for(int i=1;i<=n;i++) cerr << be[i] << " "; cerr <<endl;
  vector<int> qs;
  for(int i=0;i<q;i++) {
    cin >> qc[i] >> qd[i] >> ql[i] >> qr[i];
    ql[i] = be[ql[i]];
    qr[i] = be[qr[i]];
    qs.push_back(i);
  }
  sort(qs.begin(),qs.end(),comp);
  int l = 0, r = 0; // inclusive exclusive
  for(int q: qs) {
    int tl = ql[q];
    int tr = qr[q];
    int tc = qc[q];
    int td = qd[q];
    //cerr << "QUERY" << tl << " " << tr << " " << tc << " " << td << endl;
    while(l>tl) add(--l);
    while(l<tl) add(l++);
    while(r>tr) add(--r);
    while(r<tr) add(r++);
    //cerr << totd << " " << cold[tc] << " " << ccnt[tc] <<endl;
    //cerr << totd-cold[tc]+ccnt[tc]*td <<endl;
    ans[q] = totd-cold[tc]+ccnt[tc]*td;
  }
  for(int i=0;i<q;i++) cout << ans[i] <<endl;
}

### Prompt

Develop a solution in Cpp to the problem described below:

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.

Example

Input

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


Output

130
200
60

### Response

```cpp
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

const int MAXN = 1e5+10;
const int MAXL = 2*sqrt(MAXN)+10;
const int L = 18;

int n, q;
vector<pair<int,int>> adj[MAXN]; // < v, index of edge >
int a[MAXN], b[MAXN], c[MAXN], d[MAXN];

int depth[MAXN], be[MAXN], en[MAXN];
vector<int> edges;
vector<int> vert;
int vbe[MAXN], ven[MAXN];
int ans[MAXN];

int ql[MAXN], qr[MAXN], qc[MAXN], qd[MAXN];
bool comp(int i, int j) {
  return (ql[i]/MAXL < ql[j]/MAXL || (ql[i]/MAXL == ql[j]/MAXL && qr[i] < qr[j]));
}

void dfs(int u, int p=-1) {
  be[u] = edges.size();
  for(auto pp: adj[u]) {
    int v = pp.first;
    int id = pp.second;
    if (v==p) continue;
    depth[v] = depth[u]+1;
    edges.push_back(id);
    dfs(v,u);
    edges.push_back(id);
  }
  en[u] = edges.size()-1;
}

bool eset[MAXN];
ll ccnt[MAXN];
ll cold[MAXN];//sum of lengths for each c
ll totd;

void add(int i) {
  i = edges[i]; //d[i] and c[i]
  //cerr << "adding " << i << " " << c[i] << endl;
  int m = eset[i]? -1:1;
  totd+=m*d[i];
  cold[c[i]]+=m*d[i];
  ccnt[c[i]]+=m;
  eset[i]^=1;
}

int main(){
  cin.tie(0); ios_base::sync_with_stdio(0);
  cin >> n >> q;
  for(int i=0;i<n-1;i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    adj[a[i]].emplace_back(b[i],i);
    adj[b[i]].emplace_back(a[i],i);
  }
  dfs(1);
  //cerr << "EDGES " << endl; for(int i:edges) cerr << i << " "; cerr <<endl;
  //cerr << "be " << endl; for(int i=1;i<=n;i++) cerr << be[i] << " "; cerr <<endl;
  vector<int> qs;
  for(int i=0;i<q;i++) {
    cin >> qc[i] >> qd[i] >> ql[i] >> qr[i];
    ql[i] = be[ql[i]];
    qr[i] = be[qr[i]];
    qs.push_back(i);
  }
  sort(qs.begin(),qs.end(),comp);
  int l = 0, r = 0; // inclusive exclusive
  for(int q: qs) {
    int tl = ql[q];
    int tr = qr[q];
    int tc = qc[q];
    int td = qd[q];
    //cerr << "QUERY" << tl << " " << tr << " " << tc << " " << td << endl;
    while(l>tl) add(--l);
    while(l<tl) add(l++);
    while(r>tr) add(--r);
    while(r<tr) add(r++);
    //cerr << totd << " " << cold[tc] << " " << ccnt[tc] <<endl;
    //cerr << totd-cold[tc]+ccnt[tc]*td <<endl;
    ans[q] = totd-cold[tc]+ccnt[tc]*td;
  }
  for(int i=0;i<q;i++) cout << ans[i] <<endl;
}

```