#include<bits/stdc++.h>
using namespace std;
void *wmem;
template<class S, class T> inline S min_L(S a,T b){
  return a<=b?a:b;
}
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){
  int i;
  pair<T1, T2> *arr=(pair<T1, T2> *) mem;
  for(i=0;i<N;i++){
    arr[i].first = a[i];
    arr[i].second = b[i];
  }
  sort(arr, arr+N);
  for(i=0;i<N;i++){
    a[i] = arr[i].first;
    b[i] = arr[i].second;
  }
}
template<class T1, class T2, class T3, class T4> void sortA_L(int N, T1 a[], T2 b[], T3 c[], T4 d[], void *mem = wmem){
  int i;
  pair<pair<T1, T2>, pair<T3, T4> > *arr=(pair<pair<T1, T2>, pair<T3, T4> > *) mem;
  for(i=0;i<N;i++){
    arr[i].first.first = a[i];
    arr[i].first.second = b[i];
    arr[i].second.first = c[i];
    arr[i].second.second = d[i];
  }
  sort(arr, arr+N);
  for(i=0;i<N;i++){
    a[i] = arr[i].first.first;
    b[i] = arr[i].first.second;
    c[i] = arr[i].second.first;
    d[i] = arr[i].second.second;
  }
}
inline void rd(int &x){
  int k, m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(long long x){
  char f[20];
  int m=0, s=0;
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return a;
}
template<class T> struct segtree{
  T *addval, *fixval, *mn, *sum;
  char *fixed;
  int N, logN, *mnind;
  void malloc(int maxN, int once = 0){
    int i;
    for(i=1;i<maxN;i*=2){
      ;
    }
    sum = (T*)std::malloc(sizeof(T)*2*i);
    mn = (T*)std::malloc(sizeof(T)*2*i);
    mnind = (int*)std::malloc(sizeof(T)*2*i);
    fixval = (T*)std::malloc(sizeof(T)*i);
    addval = (T*)std::malloc(sizeof(T)*i);
    fixed = (char*)std::malloc(sizeof(char)*i);
    if(once){
      setN(maxN);
    }
  }
  void walloc(int maxN, int once = 0, void **mem = &wmem){
    int i;
    for(i=1;i<maxN;i*=2){
      ;
    }
    sum = (T*)(*mem);
    mn = sum + 2*i;
    mnind = (int*)(mn + 2*i);
    fixval = (T*)(mnind + 2*i);
    addval = fixval + i;
    fixed = (char*)(addval + i);
    *mem = fixed + i;
    if(once){
      setN(maxN);
    }
  }
  void free(void){
    std::free(sum);
    std::free(mn);
    std::free(mnind);
    std::free(fixval);
    std::free(addval);
    std::free(fixed);
  }
  T& operator[](int i){
    return sum[N+i];
  }
  void setN(int n, int zerofill = 1, int dobuild = 1){
    int i;
    for(i=1,logN=0;i<n;i*=2,logN++){
      ;
    }
    N = i;
    if(zerofill){
      for(i=0;i<N;i++){
        sum[N+i] = 0;
      }
    }
    if(dobuild){
      build();
    }
  }
  void build(void){
    int i;
    for(i=0;i<N;i++){
      mn[N+i] = sum[N+i];
      mnind[N+i] = i;
    }
    for(i=N-1;i;i--){
      sum[i] = sum[2*i] + sum[2*i+1];
      if(mn[2*i] <= mn[2*i+1]){
        mn[i] = mn[2*i];
        mnind[i] = mnind[2*i];
      }
      else{
        mn[i] = mn[2*i+1];
        mnind[i] = mnind[2*i+1];
      }
    }
    for(i=1;i<N;i++){
      fixed[i] = 0;
    }
    for(i=1;i<N;i++){
      addval[i] = 0;
    }
  }
  inline void push_one(int a, int sz, int st){
    if(fixed[a]){
      if(sz > 1){
        fixed[a*2] = fixed[a*2+1] = 1;
        fixval[a*2] = fixval[a*2+1] = fixval[a];
        sum[a*2] = sum[a*2+1] = sz * fixval[a];
        mn[a*2] = mn[a*2+1] = fixval[a];
        mnind[a*2] = st;
        mnind[a*2+1] = st + sz;
      }
      else{
        sum[a*2] = sum[a*2+1] = sz * fixval[a];
        mn[a*2] = mn[a*2+1] = fixval[a];
        mnind[a*2] = st;
        mnind[a*2+1] = st + sz;
      }
      fixed[a] = 0;
      addval[a] = 0;
      return;
    }
    if(addval[a] != 0){
      if(sz > 1){
        if(fixed[a*2]){
          fixval[a*2] += addval[a];
        }
        else{
          addval[a*2] += addval[a];
        }
        if(fixed[a*2+1]){
          fixval[a*2+1] += addval[a];
        }
        else{
          addval[a*2+1] += addval[a];
        }
        sum[a*2] += sz * addval[a];
        sum[a*2+1] += sz * addval[a];
        mn[a*2] += addval[a];
        mn[a*2+1] += addval[a];
      }
      else{
        sum[a*2] += sz * addval[a];
        sum[a*2+1] += sz * addval[a];
        mn[a*2] += addval[a];
        mn[a*2+1] += addval[a];
      }
      addval[a] = 0;
      return;
    }
  }
  inline void push(int a){
    int aa=a - N, i, nd, st, sz;
    for(i=logN;i;i--){
      nd = a>>i;
      sz = 1<<(i-1);
      st = 2 * sz * (aa>>i);
      push_one(nd, sz, st);
    }
  }
  inline void build(int a){
    int st=a - N, sz=1;
    while(a > 1){
      if(a%2){
        st += sz;
      }
      a /= 2;
      sz *= 2;
      if(fixed[a]){
        sum[a] = sz * fixval[a];
        mn[a] = fixval[a];
      }
      else{
        sum[a] = sum[a*2] + sum[a*2+1];
        if(mn[a*2] <= mn[a*2+1]){
          mn[a] = mn[a*2];
          mnind[a] = mnind[a*2];
        }
        else{
          mn[a] = mn[a*2+1];
          mnind[a] = mnind[a*2+1];
        }
        if(addval[a] != 0){
          mn[a] += addval[a];
          sum[a] += sz * addval[a];
        }
      }
    }
  }
  inline void change(int a, int b, T val){
    int aa, bb, st_a=a, st_b=b, sz=1;
    if(a >= b){
      return;
    }
    aa = (a += N);
    bb = (b += N);
    push(a);
    push(b-1);
    if(a%2){
      sum[a] = mn[a] = val;
      a++;
      st_a += sz;
    }
    if(b%2){
      b--;
      st_b -= sz;
      sum[b] = mn[b] = val;
    }
    a /= 2;
    b /= 2;
    while(a < b){
      sz *= 2;
      if(a%2){
        fixed[a]=1;
        fixval[a]=val;
        sum[a] = sz * val;
        mn[a] = val;
        mnind[a] = st_a;
        a++;
        st_a += sz;
      }
      if(b%2){
        b--;
        st_b -= sz;
        fixed[b]=1;
        fixval[b]=val;
        sum[b] = sz * val;
        mn[b] = val;
        mnind[b] = st_b;
      }
      a /= 2;
      b /= 2;
    }
    build(aa);
    build(bb-1);
  }
  inline void add(int a, int b, T val){
    int aa, bb, sz=1;
    if(a >= b){
      return;
    }
    aa = (a += N);
    bb = (b += N);
    push(a);
    push(b-1);
    if(a%2){
      sum[a] += val;
      mn[a] += val;
      a++;
    }
    if(b%2){
      b--;
      sum[b] += val;
      mn[b] += val;
    }
    a /= 2;
    b /= 2;
    while(a < b){
      sz *= 2;
      if(a%2){
        if(fixed[a]){
          fixval[a] += val;
        }
        else{
          addval[a] += val;
        }
        sum[a] += sz * val;
        mn[a] += val;
        a++;
      }
      if(b%2){
        b--;
        if(fixed[b]){
          fixval[b] += val;
        }
        else{
          addval[b] += val;
        }
        sum[b] += sz * val;
        mn[b] += val;
      }
      a /= 2;
      b /= 2;
    }
    build(aa);
    build(bb-1);
  }
  inline pair<T,int> getMin(int a, int b){
    int sz=1;
    pair<T,int> res;
    a += N;
    b += N;
    push(a);
    push(b-1);
    res.first = numeric_limits<T>::max();
    res.second = -1;
    while(a < b){
      if(a%2){
        chmin(res, make_pair(mn[a], mnind[a]));
        a++;
      }
      if(b%2){
        b--;
        chmin(res, make_pair(mn[b], mnind[b]));
      }
      a /= 2;
      b /= 2;
    }
    return res;
  }
  inline T getMinVal(int a, int b){
    return getMin(a,b).first;
  }
  inline int getMinInd(int a, int b){
    return getMin(a,b).second;
  }
  inline T getSum(int a, int b){
    T res;
    int sz=1;
    a += N;
    b += N;
    push(a);
    push(b-1);
    res = 0;
    while(a < b){
      if(a%2){
        res += sum[a++];
      }
      if(b%2){
        res += sum[--b];
      }
      a /= 2;
      b /= 2;
    }
    return res;
  }
}
;
struct graph{
  int N, **edge, *es;
  void setEdge(int N__, int M, int A[], int B[], void **mem = &wmem){
    int i;
    N = N__;
    es = (int*)(*mem);
    edge = (int**)(es+N);
    edge[0] = (int*)(edge+N);
    for(i=0;i<N;i++){
      es[i] = 0;
    }
    for(i=0;i<M;i++){
      es[A[i]]++;
      es[B[i]]++;
    }
    for(i=1;i<N;i++){
      edge[i] = edge[i-1] + es[i-1];
    }
    (*mem) = edge[N-1] + es[N-1];
    for(i=0;i<N;i++){
      es[i] = 0;
    }
    for(i=0;i<M;i++){
      edge[A[i]][es[A[i]]++] = B[i];
      edge[B[i]][es[B[i]]++] = A[i];
    }
  }
  void setDirectEdge(int N__, int M, int A[], int B[], void **mem = &wmem){
    int i;
    N = N__;
    es = (int*)(*mem);
    edge = (int**)(es+N);
    edge[0] = (int*)(edge+N);
    for(i=0;i<N;i++){
      es[i] = 0;
    }
    for(i=0;i<M;i++){
      es[A[i]]++;
    }
    for(i=1;i<N;i++){
      edge[i] = edge[i-1] + es[i-1];
    }
    (*mem) = edge[N-1] + es[N-1];
    for(i=0;i<N;i++){
      es[i] = 0;
    }
    for(i=0;i<M;i++){
      edge[A[i]][es[A[i]]++] = B[i];
    }
  }
  graph reverse(void **mem = &wmem){
    graph g;
    int i, j, k;
    g.N = N;
    g.es = (int*)(*mem);
    g.edge = (int**)(g.es + N);
    g.edge[0] = (int*)(g.edge + N);
    for(i=0;i<N;i++){
      g.es[i] = 0;
    }
    for(i=0;i<N;i++){
      for(j=0;j<es[i];j++){
        g.es[edge[i][j]]++;
      }
    }
    for(i=1;i<N;i++){
      g.edge[i] = g.edge[i-1] + g.es[i-1];
    }
    *mem = g.edge[N-1] + g.es[N-1];
    for(i=0;i<N;i++){
      g.es[i] = 0;
    }
    for(i=0;i<N;i++){
      for(j=0;j<es[i];j++){
        k = edge[i][j];
        g.edge[k][g.es[k]++] = i;
      }
    }
    return g;
  }
  graph reduce(int tn, int ind[], int self_e = 0, int dep_e = 0, void **mem = &wmem){
    graph g;
    int M=0, i, j, k, x, y;
    pair<int,int> *A;
    for(i=0;i<N;i++){
      M += es[i];
    }
    A = (pair<int,int>*)((int*)((int**)(*mem) + tn) + tn + M);
    M = 0;
    for(i=0;i<N;i++){
      x = ind[i];
      if(x < 0){
        continue;
      }
      for(j=0;j<es[i];j++){
        y = ind[edge[i][j]];
        if(y < 0){
          continue;
        }
        if(self_e==0 && x==y){
          continue;
        }
        A[M++] = make_pair(x, y);
      }
    }
    if(dep_e==0){
      sort(A, A+M);
      k = 0;
      for(i=0;i<M;i++){
        if(k && A[k-1]==A[i]){
          continue;
        }
        A[k++] = A[i];
      }
      M = k;
    }
    g.N = tn;
    g.es = (int*)(*mem);
    g.edge = (int**)(g.es + tn);
    g.edge[0] = (int*)(g.edge + tn);
    for(i=0;i<tn;i++){
      g.es[i] = 0;
    }
    for(i=0;i<M;i++){
      g.es[A[i].first]++;
    }
    for(i=1;i<tn;i++){
      g.edge[i] = g.edge[i-1] + g.es[i-1];
    }
    *mem = g.edge[tn-1] + g.es[tn-1];
    for(i=0;i<tn;i++){
      g.es[i] = 0;
    }
    for(i=0;i<M;i++){
      j = A[i].first;
      k = A[i].second;
      g.edge[j][g.es[j]++] = k;
    }
    return g;
  }
  void getDist(int root, int res[], void *mem = wmem){
    int i, j, k, *q, s, z;
    q=(int*)mem;
    for(i=0;i<N;i++){
      res[i]=-1;
    }
    res[root]=0;
    s=0;
    z=1;
    q[0]=root;
    while(z){
      i=q[s++];
      z--;
      for(j=0;j<es[i];j++){
        k=edge[i][j];
        if(res[k]>=0){
          continue;
        }
        res[k]=res[i]+1;
        q[s+z++]=k;
      }
    }
  }
  inline int sccDFS(int num[], int st, int mx){
    int i, j;
    num[st]=-2;
    for(i=0;i<es[st];i++){
      j=edge[st][i];
      if(num[j]==-1){
        mx=sccDFS(num,j,mx);
      }
    }
    num[st]=mx;
    return mx+1;
  }
  int scc(int res[], void *mem = wmem){
    graph r;
    int i, j, k, *nrv, *num, ret=0, *st, st_size;
    r = reverse(&mem);
    st = (int*)mem;
    num = st+N;
    nrv = num + N;
    for(i=0;i<N;i++){
      res[i] = num[i] = -1;
    }
    k = 0;
    for(i=0;i<N;i++){
      if(num[i]==-1){
        k = sccDFS(num,i,k);
      }
    }
    for(i=0;i<N;i++){
      nrv[num[i]] = i;
    }
    for(k=N-1;k>=0;k--){
      i=nrv[k];
      if(res[i]>=0){
        continue;
      }
      res[i]=ret;
      st_size=0;
      st[st_size++]=i;
      while(st_size){
        i=st[--st_size];
        for(j=0;j<r.es[i];j++){
          if(res[r.edge[i][j]]==-1){
            res[r.edge[i][j]]=ret;
            st[st_size++]=r.edge[i][j];
          }
        }
      }
      ret++;
    }
    return ret;
  }
  inline void bccDFS(int v, int u, int *res, int *rt, int &rts, int *S, int &Ss, int *inS, int *num, int &tm){
    int i, k;
    num[v] = ++tm;
    S[Ss++] = v;
    inS[v] = 1;
    rt[rts++] = v;
    for(i=0;i<es[v];i++){
      int w=edge[v][i];
      if(!num[w]){
        bccDFS(w, v, res, rt, rts, S, Ss, inS, num, tm);
      }
      else if(u != w && inS[w]){
        while(num[rt[rts-1]] > num[w]){
          rts--;
        }
      }
    }
    if(v == rt[rts-1]){
      k = S[Ss-1];
      for(;;){
        int w=S[--Ss];
        inS[w] = 0;
        res[w] = k;
        if(v==w){
          break;
        }
      }
      rts--;
    }
  }
  int bcc(int res[], void *mem=wmem){
    int *S, Ss=0, i, *inS, k, *num, *rt, rts=0, tm=0;
    pair<int,int> *arr;
    num = (int*)mem;
    rt = num + N;
    S = rt + N;
    inS = S + N;
    memset(num, 0, sizeof(int)*N);
    memset(inS, 0, sizeof(int)*N);
    for(i=0;i<N;i++){
      if(!num[i]){
        bccDFS(i, N, res, rt, rts, S, Ss, inS, num, tm);
      }
    }
    arr = (pair<int,int>*)mem;
    for(i=0;i<N;i++){
      arr[i].first = res[i];
      arr[i].second = i;
    }
    sort(arr, arr+N);
    k = 0;
    for(i=0;i<N;i++){
      if(i && arr[i].first != arr[i-1].first){
        k++;
      }
      res[arr[i].second] = k;
    }
    return k+1;
  }
  int shortestPath(const int s, const int t, int res[], void *mem=wmem){
    int *b, i, j, k, *q, qe=0, qs=0;
    b = (int*)mem;
    q = b + N;
    for(i=0;i<N;i++){
      b[i] = -1;
    }
    b[s] = -2;
    q[qe++] = s;
    while(qe > qs){
      i = q[qs++];
      for(j=0;j<es[i];j++){
        k = edge[i][j];
        if(b[k]!=-1){
          continue;
        }
        b[k] = i;
        q[qe++] = k;
      }
      if(b[t]!=-1){
        break;
      }
    }
    if(b[t]==-1){
      return -1;
    }
    k = 0;
    res[k] = i = t;
    while(i != s){
      res[++k] = (i = b[i]);
    }
    std::reverse(res, res+k+1);
    return k;
  }
}
;
struct HLD{
  int N, **edge, *es, *group, *groupDepth, **groupNode, groupNum, *groupSize, *groupUpNode, *groupind;
  void init(graph g, void **mem = &wmem){
    init(g.N, g.es, g.edge, mem);
  }
  void init(int N__, int *es__, int **edge__, void **mem = &wmem){
    char *vis;
    int i, j, k, mx, *q, q_ed, q_st, *sz, x, y;
    N = N__;
    es = es__;
    edge = edge__;
    group = (int*)(*mem);
    groupind = group + N;
    *mem = groupind + N;
    q = (int*)(*mem);
    sz = q + N;
    vis = (char*)(sz + N);
    for(i=0;i<N;i++){
      vis[i] = 0;
    }
    q_st = 0;
    q_ed = 1;
    q[0] = 0;
    vis[0] = 1;
    while(q_st < q_ed){
      i = q[q_st++];
      for(j=0;j<es[i];j++){
        k = edge[i][j];
        if(!vis[k]){
          vis[k] = 1;
          q[q_ed++] = k;
        }
      }
    }
    for(i=0;i<N;i++){
      sz[i] = 0;
    }
    for(j=N-1;j>=0;j--){
      i = q[j];
      sz[i] = 1;
      for(k=0;k<es[i];k++){
        sz[i] += sz[edge[i][k]];
      }
    }
    for(i=0;i<N;i++){
      group[i] = -1;
    }
    groupNum = 0;
    for(j=0;j<N;j++){
      i = q[j];
      if(group[i]>=0){
        continue;
      }
      group[i] = groupNum++;
      groupind[i] = 0;
      for(;;){
        mx = -1;
        for(k=0;k<es[i];k++){
          if(group[edge[i][k]] != -1){
            continue;
          }
          if(mx==-1){
            mx = k;
          }
          else if(sz[edge[i][k]] > sz[edge[i][mx]]){
            mx = k;
          }
        }
        if(mx==-1){
          break;
        }
        group[edge[i][mx]] = group[i];
        groupind[edge[i][mx]] = groupind[i]+1;
        i = edge[i][mx];
      }
    }
    groupSize = (int*)(*mem);
    groupUpNode = groupSize + groupNum;
    groupDepth = groupUpNode + groupNum;
    for(i=0;i<groupNum;i++){
      groupSize[i] = 0;
    }
    for(i=0;i<N;i++){
      groupSize[group[i]]++;
    }
    groupNode = (int**)(groupDepth + groupNum);
    groupNode[0] = (int*)(groupNode + groupNum);
    for(i=1;i<groupNum;i++){
      groupNode[i] = groupNode[i-1] + groupSize[i-1];
    }
    *mem = groupNode[groupNum-1] + groupSize[groupNum-1];
    for(i=0;i<N;i++){
      groupNode[group[i]][groupind[i]] = i;
    }
    for(i=0;i<groupNum;i++){
      groupDepth[i] = -1;
    }
    groupUpNode[0] = -1;
    groupDepth[0] = 0;
    for(x=0;x<groupNum;x++){
      for(y=0;y<groupSize[x];y++){
        i = groupNode[x][y];
        for(j=0;j<es[i];j++){
          k = edge[i][j];
          if(x != group[k] && groupDepth[group[k]]==-1){
            groupUpNode[group[k]] = i;
            groupDepth[group[k]] = groupDepth[x] + 1;
          }
        }
      }
    }
  }
  int lca(int x, int y){
    int x1, x2, y1, y2;
    x1 = group[x];
    x2 = groupind[x];
    y1 = group[y];
    y2 = groupind[y];
    while(groupDepth[x1] > groupDepth[y1]){
      x = groupUpNode[x1];
      x1 = group[x];
      x2 = groupind[x];
    }
    while(groupDepth[x1] < groupDepth[y1]){
      y = groupUpNode[y1];
      y1 = group[y];
      y2 = groupind[y];
    }
    while(x1 != y1){
      x = groupUpNode[x1];
      x1 = group[x];
      x2 = groupind[x];
      y = groupUpNode[y1];
      y1 = group[y];
      y2 = groupind[y];
    }
    if(x2 <= y2){
      return x;
    }
    return y;
  }
  int depth(int x){
    int res=0, x1, x2;
    x1 = group[x];
    x2 = groupind[x];
    while(groupUpNode[x1] != -1){
      res += x2 + 1;
      x = groupUpNode[x1];
      x1 = group[x];
      x2 = groupind[x];
    }
    return res + x2;
  }
  int dist(int x, int y){
    int res=0, x1, x2, y1, y2;
    x1 = group[x];
    x2 = groupind[x];
    y1 = group[y];
    y2 = groupind[y];
    while(groupDepth[x1] > groupDepth[y1]){
      res += x2 + 1;
      x = groupUpNode[x1];
      x1 = group[x];
      x2 = groupind[x];
    }
    while(groupDepth[x1] < groupDepth[y1]){
      res += y2 + 1;
      y = groupUpNode[y1];
      y1 = group[y];
      y2 = groupind[y];
    }
    while(x1 != y1){
      res += x2 + y2 + 2;
      x = groupUpNode[x1];
      x1 = group[x];
      x2 = groupind[x];
      y = groupUpNode[y1];
      y1 = group[y];
      y2 = groupind[y];
    }
    if(x2 <= y2){
      return res + y2 - x2;
    }
    return res + x2 - y2;
  }
  int up(int x){
    int x1=group[x], x2=groupind[x];
    if(x2==0){
      return groupUpNode[x1];
    }
    return groupNode[x1][x2-1];
  }
  int up(int x, int d){
    int x1=group[x], x2=groupind[x];
    while(d > x2){
      if(groupUpNode[x1]==-1){
        return -1;
      }
      d -= x2 + 1;
      x = groupUpNode[x1];
      x1 = group[x];
      x2 = groupind[x];
    }
    return groupNode[x1][x2-d];
  }
}
;
template<class T> struct HLD_segtree{
  HLD *hld;
  segtree<T> *seg;
  void init(HLD *hld__, T initval[], void **mem = &wmem){
    int i, j;
    hld = hld__;
    seg = (segtree<T>*)(*mem);
    *mem = seg + hld->groupNum;
    for(i=0;i<hld->groupNum;i++){
      seg[i].walloc(hld->groupSize[i], 0, mem);
      seg[i].setN(hld->groupSize[i], 0, 0);
      if(initval!=NULL){
        for(j=0;j<hld->groupSize[i];j++){
          seg[i][j] = initval[ hld->groupNode[i][j] ];
        }
      }
      else{
        for(j=0;j<hld->groupSize[i];j++){
          seg[i][j] = 0;
        }
      }
      seg[i].build();
    }
  }
  inline void change(int u, int v, T val){
    int ug, ui, vg, vi;
    ug = hld->group[u];
    vg = hld->group[v];
    while(ug != vg){
      if(hld->groupDepth[ug] < hld->groupDepth[vg]){
        swap(u, v);
        swap(ug, vg);
      }
      seg[ug].change(0, hld->groupind[u]+1, val);
      u = hld->groupUpNode[ug];
      ug = hld->group[u];
    }
    ui = hld->groupind[u];
    vi = hld->groupind[v];
    seg[ug].change(min_L(ui, vi),max_L(ui, vi)+1, val);
  }
  inline void add(int u, int v, T val){
    int ug, ui, vg, vi;
    ug = hld->group[u];
    vg = hld->group[v];
    while(ug != vg){
      if(hld->groupDepth[ug] < hld->groupDepth[vg]){
        swap(u, v);
        swap(ug, vg);
      }
      seg[ug].add(0, hld->groupind[u]+1, val);
      u = hld->groupUpNode[ug];
      ug = hld->group[u];
    }
    ui = hld->groupind[u];
    vi = hld->groupind[v];
    seg[ug].add(min_L(ui, vi),max_L(ui, vi)+1, val);
  }
  inline pair<T,int> getMin(int u, int v){
    int ug, ui, vg, vi;
    pair<T,int> res, tmp;
    ug = hld->group[u];
    vg = hld->group[v];
    res.first = numeric_limits<T>::max();
    res.second = -1;
    while(ug != vg){
      if(hld->groupDepth[ug] < hld->groupDepth[vg]){
        swap(u, v);
        swap(ug, vg);
      }
      tmp = seg[ug].getMin(0, hld->groupind[u]+1);
      tmp.second = hld->groupNode[ug][tmp.second];
      chmin(res, tmp);
      u = hld->groupUpNode[ug];
      ug = hld->group[u];
    }
    ui = hld->groupind[u];
    vi = hld->groupind[v];
    tmp = seg[ug].getMin(min_L(ui, vi),max_L(ui, vi)+1);
    tmp.second = hld->groupNode[ug][tmp.second];
    chmin(res, tmp);
    return res;
  }
  inline T getMinVal(int u, int v){
    return getMin(u,v).first;
  }
  inline int getMinInd(int u, int v){
    return getMin(u,v).second;
  }
  inline T getSum(int u, int v){
    T res;
    int ug, ui, vg, vi;
    ug = hld->group[u];
    vg = hld->group[v];
    res = 0;
    while(ug != vg){
      if(hld->groupDepth[ug] < hld->groupDepth[vg]){
        swap(u, v);
        swap(ug, vg);
      }
      res += seg[ug].getSum(0, hld->groupind[u]+1);
      u = hld->groupUpNode[ug];
      ug = hld->group[u];
    }
    ui = hld->groupind[u];
    vi = hld->groupind[v];
    res += seg[ug].getSum(min_L(ui, vi),max_L(ui, vi)+1);
    return res;
  }
}
;
char memarr[96000000];
int N;
int Q;
int A[100000];
int B[100000];
int C[100000];
int D[100000];
int X[100000];
int Y[100000];
int U[100000];
int V[100000];
int ind[100000];
int val[100000];
long long res[100000];
graph g;
HLD hld;
HLD_segtree<long long> t;
int up[100000];
void change(int eind, long long val){
  if(up[A[eind]] == B[eind]){
    t.change(A[eind], A[eind], val);
  }
  else{
    t.change(B[eind], B[eind], val);
  }
}
long long calc(int a, int b){
  int k;
  long long res;
  k = hld.lca(a,b);
  res = t.getSum(a,b) - t.getSum(k,k);
  return res;
}
int main(){
  int bk, cr, i, ii, j, k;
  long long r;
  wmem = memarr;
  rd(N);
  rd(Q);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=0;Lj4PdHRW<N-1;Lj4PdHRW++){
      rd(A[Lj4PdHRW]);A[Lj4PdHRW] += (-1);
      rd(B[Lj4PdHRW]);B[Lj4PdHRW] += (-1);
      rd(C[Lj4PdHRW]);
      rd(D[Lj4PdHRW]);
    }
  }
  {
    int KL2GvlyY;
    for(KL2GvlyY=0;KL2GvlyY<Q;KL2GvlyY++){
      rd(X[KL2GvlyY]);
      rd(Y[KL2GvlyY]);
      rd(U[KL2GvlyY]);U[KL2GvlyY] += (-1);
      rd(V[KL2GvlyY]);V[KL2GvlyY] += (-1);
    }
  }
  sortA_L(N-1, C, A, B, D);
  for(i=0;i<Q;i++){
    val[i] = X[i];
  }
  for(i=0;i<Q;i++){
    ind[i] = i;
  }
  sortA_L(Q, val, ind);
  g.setEdge(N, N-1, A, B);
  hld.init(g);
  t.init(&hld, NULL);
  for(i=0;i<N;i++){
    up[i] = hld.up(i);
  }
  for(i=0;i<N-1;i++){
    change(i, D[i]);
  }
  bk = cr = 0;
  for(ii=0;ii<Q;ii++){
    i = ind[ii];
    while(cr < N-1 && C[cr] <= X[i]){
      change(cr, 10000000000LL);
      cr++;
    }
    while(bk < N-1 && C[bk] < X[i]){
      change(bk, D[bk]);
      bk++;
    }
    r = calc(U[i], V[i]);
    res[i] = (r%10000000000LL) + (r/10000000000LL) * Y[i];
  }
  for(i=0;i<Q;i++){
    wt_L(res[i]);
    wt_L('\n');
  }
  return 0;
}
// cLay varsion 20190707-1

// --- original code ---
// int N, Q, A[1d5], B[1d5], C[1d5], D[1d5], X[1d5], Y[1d5], U[1d5], V[1d5];
// 
// int ind[1d5], val[1d5];
// ll res[1d5];
// 
// graph g;
// HLD hld;
// HLD_segtree<ll> t;
// 
// int up[1d5];
// 
// void change(int eind, ll val){
//   if(up[A[eind]] == B[eind]){
//     t.change(A[eind], A[eind], val);
//   } else {
//     t.change(B[eind], B[eind], val);
//   }
// }
// 
// ll calc(int a, int b){
//   int k;
//   ll res;
//   k = hld.lca(a,b);
//   res = t.getSum(a,b) - t.getSum(k,k);
//   return res;
// }
// 
// {
//   int i, j, k;
//   int bk, cr;
//   int ii;
//   ll r;
//   
//   rd(N,Q,(A--,B--,C,D)(N-1),(X,Y,U--,V--)(Q));
// 
//   sortA(N-1, C, A, B, D);
//   
//   rep(i,Q) val[i] = X[i];
//   rep(i,Q) ind[i] = i;
//   sortA(Q, val, ind);
// 
//   g.setEdge(N, N-1, A, B);
//   hld.init(g);
//   t.init(&hld, NULL);
// 
//   rep(i,N) up[i] = hld.up(i);
//   rep(i,N-1) change(i, D[i]);
// 
//   bk = cr = 0;
//   rep(ii,Q){
//     i = ind[ii];
// 
//     while(cr < N-1 && C[cr] <= X[i]){
//       change(cr, 1d10);
//       cr++;
//     }
//     while(bk < N-1 && C[bk] < X[i]){
//       change(bk, D[bk]);
//       bk++;
//     }
// 
//     r = calc(U[i], V[i]);
//     res[i] = (r%1d10) + (r/1d10) * Y[i];
//   }
// 
//   rep(i,Q) wt(res[i]);
// }


Measured memory_bytes: 23552