#include <bits/stdc++.h>

using namespace std;

int n,q,cnt;
int g[100010];
struct EDGE{
	int t,c,col,next;
}e[200010];

inline void add_edge(int x,int y,int col,int dis){
	e[++cnt].t=y;
	e[cnt].c=dis;
	e[cnt].col=col;
	e[cnt].next=g[x];
	g[x]=cnt;
}

int siz;
int root[100010];
int ls[3000000],rs[3000000],sum[3000000],CNT[3000000];

inline void build(int l,int r,int pre,int &now,int col,int len){
    now=++siz;CNT[now]=CNT[pre]+1;sum[now]=sum[pre]+len;
    if(l==r)return;
    ls[now]=ls[pre];rs[now]=rs[pre];

    int mid=(l+r)>>1;
    if(col<=mid)build(l,mid,ls[pre],ls[now],col,len);
    else build(mid+1,r,rs[pre],rs[now],col,len);
}
inline pair<int,int> query(int root,int l,int r,int pos){
	if(l==r)return make_pair(CNT[root],sum[root]);
	int mid=(l+r)>>1;

	if(pos<=mid)return query(ls[root],l,mid,pos);
	else return query(rs[root],mid+1,r,pos);
}

int fa[100010][20];
int dep[100010],dis[100010];

inline void dfs(int x){
	for(int i=1;i<20;i++)
		fa[x][i]=fa[fa[x][i-1]][i-1];
	for(int i=g[x];i;i=e[i].next)
		if(e[i].t!=fa[x][0]){
			fa[e[i].t][0]=x;
			dep[e[i].t]=dep[x]+1;
			dis[e[i].t]=dis[x]+e[i].c;
			build(1,n-1,root[x],root[e[i].t],e[i].col,e[i].c);
			dfs(e[i].t);
		}
}

inline void swim(int&x,int h){
	for(int i=0;h;i++){
		if(h&1)x=fa[x][i];
		h>>=1;
	}
}
inline int lca(int x,int y){
	if(dep[x]<dep[y])swap(x,y);
	swim(x,dep[x]-dep[y]);
	for(int i=19;x!=y;i--){
		if(fa[x][i]!=fa[y][i]||i==0){
			x=fa[x][i];
			y=fa[y][i];
		}
		if(!i)i=1;
	}
	return x;
}

inline pair<int,int> operator+(const pair<int,int>&x, const pair<int,int>&y) {
    return make_pair(x.first+y.first, x.second+y.second);
}
inline pair<int,int> operator-(const pair<int,int>&x, const pair<int,int>&y) {
    return make_pair(x.first-y.first, x.second-y.second);
}
inline pair<int,int> operator*(const pair<int,int>&x, const int&y) {
    return make_pair(x.first*y, x.second*y);
}

int main(){
	scanf("%d%d",&n,&q);
	for(int i=1;i<n;i++){
		int a,b,c,d;
		scanf("%d%d%d%d",&a,&b,&c,&d);
		add_edge(a,b,c,d);
		add_edge(b,a,c,d);
	}
	fa[1][0]=1;dep[1]=1;root[1]=0;
	dfs(1);

	for(;q;q--){
		int x,y,u,v;
		scanf("%d%d%d%d",&x,&y,&u,&v);

		int LCA=lca(u,v);
		int ans=dis[u]+dis[v]-dis[LCA]*2;

		auto q = query(root[u],1,n-1,x)+query(root[v],1,n-1,x)-query(root[LCA],1,n-1,x)*2;
		ans-=q.second;
		ans+=q.first*y;
		printf("%d\n",ans);
	}

	return 0;
}
### Prompt

Please create a solution in CPP 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;

int n,q,cnt;
int g[100010];
struct EDGE{
	int t,c,col,next;
}e[200010];

inline void add_edge(int x,int y,int col,int dis){
	e[++cnt].t=y;
	e[cnt].c=dis;
	e[cnt].col=col;
	e[cnt].next=g[x];
	g[x]=cnt;
}

int siz;
int root[100010];
int ls[3000000],rs[3000000],sum[3000000],CNT[3000000];

inline void build(int l,int r,int pre,int &now,int col,int len){
    now=++siz;CNT[now]=CNT[pre]+1;sum[now]=sum[pre]+len;
    if(l==r)return;
    ls[now]=ls[pre];rs[now]=rs[pre];

    int mid=(l+r)>>1;
    if(col<=mid)build(l,mid,ls[pre],ls[now],col,len);
    else build(mid+1,r,rs[pre],rs[now],col,len);
}
inline pair<int,int> query(int root,int l,int r,int pos){
	if(l==r)return make_pair(CNT[root],sum[root]);
	int mid=(l+r)>>1;

	if(pos<=mid)return query(ls[root],l,mid,pos);
	else return query(rs[root],mid+1,r,pos);
}

int fa[100010][20];
int dep[100010],dis[100010];

inline void dfs(int x){
	for(int i=1;i<20;i++)
		fa[x][i]=fa[fa[x][i-1]][i-1];
	for(int i=g[x];i;i=e[i].next)
		if(e[i].t!=fa[x][0]){
			fa[e[i].t][0]=x;
			dep[e[i].t]=dep[x]+1;
			dis[e[i].t]=dis[x]+e[i].c;
			build(1,n-1,root[x],root[e[i].t],e[i].col,e[i].c);
			dfs(e[i].t);
		}
}

inline void swim(int&x,int h){
	for(int i=0;h;i++){
		if(h&1)x=fa[x][i];
		h>>=1;
	}
}
inline int lca(int x,int y){
	if(dep[x]<dep[y])swap(x,y);
	swim(x,dep[x]-dep[y]);
	for(int i=19;x!=y;i--){
		if(fa[x][i]!=fa[y][i]||i==0){
			x=fa[x][i];
			y=fa[y][i];
		}
		if(!i)i=1;
	}
	return x;
}

inline pair<int,int> operator+(const pair<int,int>&x, const pair<int,int>&y) {
    return make_pair(x.first+y.first, x.second+y.second);
}
inline pair<int,int> operator-(const pair<int,int>&x, const pair<int,int>&y) {
    return make_pair(x.first-y.first, x.second-y.second);
}
inline pair<int,int> operator*(const pair<int,int>&x, const int&y) {
    return make_pair(x.first*y, x.second*y);
}

int main(){
	scanf("%d%d",&n,&q);
	for(int i=1;i<n;i++){
		int a,b,c,d;
		scanf("%d%d%d%d",&a,&b,&c,&d);
		add_edge(a,b,c,d);
		add_edge(b,a,c,d);
	}
	fa[1][0]=1;dep[1]=1;root[1]=0;
	dfs(1);

	for(;q;q--){
		int x,y,u,v;
		scanf("%d%d%d%d",&x,&y,&u,&v);

		int LCA=lca(u,v);
		int ans=dis[u]+dis[v]-dis[LCA]*2;

		auto q = query(root[u],1,n-1,x)+query(root[v],1,n-1,x)-query(root[LCA],1,n-1,x)*2;
		ans-=q.second;
		ans+=q.first*y;
		printf("%d\n",ans);
	}

	return 0;
}
```