#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repp(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)

typedef long long LL;
typedef long double LD;

struct edge{
  int to;
  int c;
  int d;
};

struct query{
  int id;
  int c;
  LL y;
  int k;
};

vector<vector<edge>> e;
vector<vector<query>> que;
vector<int> depth;
vector<LL> ans;
vector<vector<int>> par;

void dfs(int x,int d){
  depth[x]=d;
  for(auto ch:e[x]){
    if(ch.to!=par[x][0]){
      par[ch.to][0]=x;
      dfs(ch.to,d+1);
    }
  }
}

int sz;
int lca(int x, int y){
  int d=depth[x]-depth[y];
  if(d>0){
    int a=0;
    while(d){
      if(d&1) x=par[x][a];
      a++;
      d>>=1;
    }
  }else if(d<0){
    d=-d;
    int a=0;
    while(d){
      if(d&1) y=par[y][a];
      a++;
      d>>=1;
    }
  }
  if(x==y) return x;
  per(i,sz){
    if(par[x][i]!=par[y][i]){
      x=par[x][i];
      y=par[y][i];
    }
  }
  return par[x][0];
}


LL ttt=0;
vector<LL> total;
vector<int> cnt;

void dfs2(int x){
  for(auto qc:que[x]){
    ans[qc.id]+=qc.k*(ttt-total[qc.c]+cnt[qc.c]*qc.y);
  }
  for(auto ch:e[x]){
    if(ch.to!=par[x][0]){
      ttt+=ch.d;
      total[ch.c]+=ch.d;
      cnt[ch.c]++;
      dfs2(ch.to);
      ttt-=ch.d;
      total[ch.c]-=ch.d;
      cnt[ch.c]--;
    }
  }
}

int main(){
  int n,q;
  cin >> n >> q;
  e=vector<vector<edge>>(n);
  int a,b,c,d;
  rep(i,n-1){
    cin >> a >> b >> c >> d;
    e[a-1].push_back({b-1,c,d});
    e[b-1].push_back({a-1,c,d});
  }
  sz=1;
  int aaa=1;
  while(n>aaa) aaa*=2,sz++;
  par=vector<vector<int>>(n,vector<int>(sz,-1));
  depth=vector<int>(n,0);
  par[0][0]=0;
  dfs(0,0);
  repp(j,1,sz){
    rep(i,n){
      par[i][j]=par[par[i][j-1]][j-1];
    }
  }
  que=vector<vector<query>>(n);
  rep(i,q){
    cin >> c >> d >> a >> b;
    que[a-1].push_back({i,c,d,1});
    que[b-1].push_back({i,c,d,1});
    que[lca(a-1,b-1)].push_back({i,c,d,-2});
  }
  ans=vector<LL>(q,0);
  cnt=vector<int>(n,0);
  total=vector<LL>(n,0);
  dfs2(0);
  rep(i,q){
    cout << ans[i] << endl;
  }
  return 0;
}

### Prompt

Please formulate a cpp solution to the following problem:

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;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repp(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)

typedef long long LL;
typedef long double LD;

struct edge{
  int to;
  int c;
  int d;
};

struct query{
  int id;
  int c;
  LL y;
  int k;
};

vector<vector<edge>> e;
vector<vector<query>> que;
vector<int> depth;
vector<LL> ans;
vector<vector<int>> par;

void dfs(int x,int d){
  depth[x]=d;
  for(auto ch:e[x]){
    if(ch.to!=par[x][0]){
      par[ch.to][0]=x;
      dfs(ch.to,d+1);
    }
  }
}

int sz;
int lca(int x, int y){
  int d=depth[x]-depth[y];
  if(d>0){
    int a=0;
    while(d){
      if(d&1) x=par[x][a];
      a++;
      d>>=1;
    }
  }else if(d<0){
    d=-d;
    int a=0;
    while(d){
      if(d&1) y=par[y][a];
      a++;
      d>>=1;
    }
  }
  if(x==y) return x;
  per(i,sz){
    if(par[x][i]!=par[y][i]){
      x=par[x][i];
      y=par[y][i];
    }
  }
  return par[x][0];
}


LL ttt=0;
vector<LL> total;
vector<int> cnt;

void dfs2(int x){
  for(auto qc:que[x]){
    ans[qc.id]+=qc.k*(ttt-total[qc.c]+cnt[qc.c]*qc.y);
  }
  for(auto ch:e[x]){
    if(ch.to!=par[x][0]){
      ttt+=ch.d;
      total[ch.c]+=ch.d;
      cnt[ch.c]++;
      dfs2(ch.to);
      ttt-=ch.d;
      total[ch.c]-=ch.d;
      cnt[ch.c]--;
    }
  }
}

int main(){
  int n,q;
  cin >> n >> q;
  e=vector<vector<edge>>(n);
  int a,b,c,d;
  rep(i,n-1){
    cin >> a >> b >> c >> d;
    e[a-1].push_back({b-1,c,d});
    e[b-1].push_back({a-1,c,d});
  }
  sz=1;
  int aaa=1;
  while(n>aaa) aaa*=2,sz++;
  par=vector<vector<int>>(n,vector<int>(sz,-1));
  depth=vector<int>(n,0);
  par[0][0]=0;
  dfs(0,0);
  repp(j,1,sz){
    rep(i,n){
      par[i][j]=par[par[i][j-1]][j-1];
    }
  }
  que=vector<vector<query>>(n);
  rep(i,q){
    cin >> c >> d >> a >> b;
    que[a-1].push_back({i,c,d,1});
    que[b-1].push_back({i,c,d,1});
    que[lca(a-1,b-1)].push_back({i,c,d,-2});
  }
  ans=vector<LL>(q,0);
  cnt=vector<int>(n,0);
  total=vector<LL>(n,0);
  dfs2(0);
  rep(i,q){
    cout << ans[i] << endl;
  }
  return 0;
}

```