#include<bits/stdc++.h>
#define N 100005
using namespace std;
bool DEBUG;
int n,q,now;
int sze[N],top[N],son[N],dep[N],fa[N][20],dis[N];
int head[N],id;
struct edge{
	int to,clo,len,nxt;
}E[N<<1];
inline void add_edge(int a,int b,int c,int d){
	E[++id]=(edge){b,c,d,head[a]};
	head[a]=id;
}
struct Query{
	int x,y,u,v;
}Q[N];
inline void Rd(int &res){
	char c;res=0;
	while(c=getchar(),c<48);
	do res=(res<<3)+(res<<1)+(c^48);
	while(c=getchar(),c>47);
	return;
}
struct Tree{
	#define M 10000000
	int nw;
	int rt[M],ls[M],rs[M],cnt[M],sum[M];
	void update(int l,int r,int x,int v,int &p,int od){
		p=++nw;
		if(l==r){
			cnt[p]=cnt[od]+1;
			sum[p]=sum[od]+v;
			return;
		}
		ls[p]=ls[od];rs[p]=rs[od];
		int mid=(l+r)>>1;
		if(mid>=x)update(l,mid,x,v,ls[p],ls[od]);
		else update(mid+1,r,x,v,rs[p],rs[od]);
	}
	int query(int l,int r,int x,int p,int od){
//		if(DEBUG&&l==r)printf("!!%d\n",cnt[p]);
		if(l==r)return now*(cnt[p]-cnt[od])-sum[p]+sum[od];
		int mid=(l+r)>>1;
		if(mid>=x)return query(l,mid,x,ls[p],ls[od]);
		return query(mid+1,r,x,rs[p],rs[od]);
	}
}tree;
void dfs(int x,int f,int d){
	dep[x]=d;sze[x]=1;
	for(int i=head[x];i;i=E[i].nxt){
		int v=E[i].to;
		if(v==f)continue;
		fa[v][0]=x;
		dis[v]=dis[x]+E[i].len;
		dfs(v,x,d+1);
		if(sze[v]>sze[son[x]])son[x]=v;
		sze[x]+=sze[v];
	}
}
void re_dfs(int x,int f,int tp){
	top[x]=tp;
	if(son[x])re_dfs(son[x],x,tp);
	for(int i=head[x];i;i=E[i].nxt){
		int v=E[i].to;
		if(v==f||v==son[x])continue;
		re_dfs(v,x,v);
	}
}
void dfs_init(int x,int f){
	for(int i=head[x];i;i=E[i].nxt){
		int v=E[i].to;
		if(v==f)continue;
		tree.update(1,n-1,E[i].clo,E[i].len,tree.rt[v],tree.rt[x]);
		dfs_init(v,x);
	}
}
inline int LCA(int a,int b){
	while(top[a]!=top[b]){
		if(dep[top[a]]>dep[top[b]])a=fa[top[a]][0];
		else b=fa[top[b]][0];
	}
	return dep[a]<dep[b]?a:b;
}
int main(){
	Rd(n);Rd(q);
	for(int i=1,a,b,c,d;i<n;i++){
		Rd(a);Rd(b);Rd(c);Rd(d);
		add_edge(a,b,c,d);
		add_edge(b,a,c,d);
	}
	dfs(1,0,1);
	re_dfs(1,0,1);
	dfs_init(1,0);
	for(int i=1,x,y,u,v;i<=q;i++){
		Rd(x);Rd(y);Rd(u);Rd(v);
		int ans=0,lca=LCA(u,v);now=y;
		ans=dis[u]-dis[lca]+dis[v]-dis[lca];
		ans+=tree.query(1,n-1,x,tree.rt[u],tree.rt[lca]);
//		if(i==1)DEBUG=1;
		ans+=tree.query(1,n-1,x,tree.rt[v],tree.rt[lca]);
//		if(i==1)DEBUG=0;
		printf("%d\n",ans);
	}
	return 0;
}
### Prompt

Generate 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>
#define N 100005
using namespace std;
bool DEBUG;
int n,q,now;
int sze[N],top[N],son[N],dep[N],fa[N][20],dis[N];
int head[N],id;
struct edge{
	int to,clo,len,nxt;
}E[N<<1];
inline void add_edge(int a,int b,int c,int d){
	E[++id]=(edge){b,c,d,head[a]};
	head[a]=id;
}
struct Query{
	int x,y,u,v;
}Q[N];
inline void Rd(int &res){
	char c;res=0;
	while(c=getchar(),c<48);
	do res=(res<<3)+(res<<1)+(c^48);
	while(c=getchar(),c>47);
	return;
}
struct Tree{
	#define M 10000000
	int nw;
	int rt[M],ls[M],rs[M],cnt[M],sum[M];
	void update(int l,int r,int x,int v,int &p,int od){
		p=++nw;
		if(l==r){
			cnt[p]=cnt[od]+1;
			sum[p]=sum[od]+v;
			return;
		}
		ls[p]=ls[od];rs[p]=rs[od];
		int mid=(l+r)>>1;
		if(mid>=x)update(l,mid,x,v,ls[p],ls[od]);
		else update(mid+1,r,x,v,rs[p],rs[od]);
	}
	int query(int l,int r,int x,int p,int od){
//		if(DEBUG&&l==r)printf("!!%d\n",cnt[p]);
		if(l==r)return now*(cnt[p]-cnt[od])-sum[p]+sum[od];
		int mid=(l+r)>>1;
		if(mid>=x)return query(l,mid,x,ls[p],ls[od]);
		return query(mid+1,r,x,rs[p],rs[od]);
	}
}tree;
void dfs(int x,int f,int d){
	dep[x]=d;sze[x]=1;
	for(int i=head[x];i;i=E[i].nxt){
		int v=E[i].to;
		if(v==f)continue;
		fa[v][0]=x;
		dis[v]=dis[x]+E[i].len;
		dfs(v,x,d+1);
		if(sze[v]>sze[son[x]])son[x]=v;
		sze[x]+=sze[v];
	}
}
void re_dfs(int x,int f,int tp){
	top[x]=tp;
	if(son[x])re_dfs(son[x],x,tp);
	for(int i=head[x];i;i=E[i].nxt){
		int v=E[i].to;
		if(v==f||v==son[x])continue;
		re_dfs(v,x,v);
	}
}
void dfs_init(int x,int f){
	for(int i=head[x];i;i=E[i].nxt){
		int v=E[i].to;
		if(v==f)continue;
		tree.update(1,n-1,E[i].clo,E[i].len,tree.rt[v],tree.rt[x]);
		dfs_init(v,x);
	}
}
inline int LCA(int a,int b){
	while(top[a]!=top[b]){
		if(dep[top[a]]>dep[top[b]])a=fa[top[a]][0];
		else b=fa[top[b]][0];
	}
	return dep[a]<dep[b]?a:b;
}
int main(){
	Rd(n);Rd(q);
	for(int i=1,a,b,c,d;i<n;i++){
		Rd(a);Rd(b);Rd(c);Rd(d);
		add_edge(a,b,c,d);
		add_edge(b,a,c,d);
	}
	dfs(1,0,1);
	re_dfs(1,0,1);
	dfs_init(1,0);
	for(int i=1,x,y,u,v;i<=q;i++){
		Rd(x);Rd(y);Rd(u);Rd(v);
		int ans=0,lca=LCA(u,v);now=y;
		ans=dis[u]-dis[lca]+dis[v]-dis[lca];
		ans+=tree.query(1,n-1,x,tree.rt[u],tree.rt[lca]);
//		if(i==1)DEBUG=1;
		ans+=tree.query(1,n-1,x,tree.rt[v],tree.rt[lca]);
//		if(i==1)DEBUG=0;
		printf("%d\n",ans);
	}
	return 0;
}
```