Problem:
Score : 600 points

Problem Statement

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.

Sample Input 1

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

Sample Output 1

130
200
60

The graph in this input is as follows:

Here the edges of Color 1 are shown as solid red lines, the edge of Color 2 is shown as a bold green line, and the edge of Color 4 is shown as a blue dashed line.

Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130.

Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200.

Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths.

Solution in LuaJIT (2.0.4) (judge verdict: Wrong Answer)

local mfl, mce, mmi = math.floor, math.ceil, math.min
local mab = math.abs
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getValue = function(self, pos)
  return self.stage[self.stagenum][pos]
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, q = io.read("*n", "*n")
local edge = {}
local asked = {}
local depth = {}
local childstep = {}
local segpos = {}
for i = 1, n do
  edge[i] = {}
  asked[i] = false
  depth[i] = 0
  childstep[i] = 0
  segpos[i] = 0
end
depth[n + 1] = n + 1
local color = {}
local value = {}
for i = 1, 2 * n - 1 do
  color[i] = 0
  value[i] = 0
end
local colormap = {}
for i = 1, n - 1 do
  local x, y, c, d = io.read("*n", "*n", "*n", "*n")
  table.insert(edge[x], {y, c, d})
  table.insert(edge[y], {x, c, d})
  colormap[i] = {}
end

local stDepth = SegTree.new(2 * n - 1, function(x, y) return depth[x] < depth[y] and x or y end, n + 1)
local stLen = SegTree.new(2 * n - 1, function(x, y) return x + y end, 0)
local tasks = {{1, 0, 0}}
local done = 0
while done < 2 * n - 1 do
  done = done + 1
  local src, sval, scol = tasks[#tasks][1], tasks[#tasks][2], tasks[#tasks][3]
  table.remove(tasks)
  asked[src] = true
  stDepth:setValue(done, src, true)
  stLen:setValue(done, sval, true)
  segpos[src] = done
  if 1 < done then table.insert(colormap[scol], done) end
  local cs = childstep[src]
  while cs < #edge[src] do
    cs = cs + 1
    childstep[src] = cs
    local dst = edge[src][cs][1]
    if not asked[dst] then
      depth[dst] = depth[src] + 1
      table.insert(tasks, {src, edge[src][cs][3], edge[src][cs][2]})
      table.insert(tasks, {dst, -edge[src][cs][3], edge[src][cs][2]})
      break
    end
  end
end
stDepth:updateAll()
stLen:updateAll()
local qlist = {}
for i = 1, q do
  qlist[i] = {i, io.read("*n", "*n", "*n", "*n")}
end
table.sort(qlist, function(x, y) return x[2] < y[2] end)
local curcol = 0
local storeval = {}
for i = 1, q do
  local tmp = qlist[i]
  local ccol, cval, u, v = tmp[2], tmp[3], tmp[4], tmp[5]
  if curcol ~= ccol then
    -- restore
    for j = 1, #storeval do
      stLen:setValue(storeval[j][1], storeval[j][2])
    end
    -- changecolor
    local cmap = colormap[ccol]
    storeval = {}
    for j = 1, #cmap do
      storeval[j] = {cmap[j], stLen:getValue(cmap[j])}
      if storeval[j][2] < 0 then
        stLen:setValue(cmap[j], -cval)
      else
        stLen:setValue(cmap[j], cval)
      end
    end
    curcol = ccol
  end
  -- get parent
  local spu, spv = segpos[u], segpos[v]
  if spv < spu then spu, spv = spv, spu end
  local spp = stDepth:getRange(spu, spv)
  local ret = 0
  if spv < spp then
    ret = ret + mab(stLen:getRange(spv + 1, spp))
  elseif spp < spv then
    ret = ret + mab(stLen:getRange(spp + 1, spv))
  end
  if spu < spp then
    ret = ret + mab(stLen:getRange(spu + 1, spp))
  elseif spp < spu then
    ret = ret + mab(stLen:getRange(spp + 1, spu))
  end
  qlist[i][6] = ret
end

table.sort(qlist, function(x, y) return x[1] < y[1] end)
for i = 1, q do
  print(qlist[i][6])
end