#include <bits/stdc++.h>
using namespace std;
template <typename T>
T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
template <typename T>
inline T POW(T B, T P) {
  if (P == 0) return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return (POW(B, P / 2) * POW(B, P / 2));
}
template <typename T>
inline T Gcd(T a, T b) {
  if (a < 0) return Gcd(-a, b);
  if (b < 0) return Gcd(a, -b);
  return (b == 0) ? a : Gcd(b, a % b);
}
template <typename T>
inline T Lcm(T a, T b) {
  if (a < 0) return Lcm(-a, b);
  if (b < 0) return Lcm(a, -b);
  return a * (b / Gcd(a, b));
}
long long Bigmod(long long base, long long power, long long MOD) {
  long long ret = 1;
  while (power) {
    if (power & 1) ret = (ret * base) % MOD;
    base = (base * base) % MOD;
    power >>= 1;
  }
  return ret;
}
bool isVowel(char ch) {
  ch = toupper(ch);
  if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
    return true;
  return false;
}
template <typename T>
long long int isLeft(T a, T b, T c) {
  return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
long long ModInverse(long long number, long long MOD) {
  return Bigmod(number, MOD - 2, MOD);
}
bool isConst(char ch) {
  if (isalpha(ch) && !isVowel(ch)) return true;
  return false;
}
int toInt(string s) {
  int sm;
  stringstream second(s);
  second >> sm;
  return sm;
}
vector<pair<int, int> > A[500007];
int Tot[500007], ChainNo, Pos[500007], now;
int St[500007], IdNo[500007], Par[500007], Lev[500007];
int ar[500007];
int br[500007];
void Dfs(int u, int p, int L) {
  Par[u] = p;
  Tot[u] = 1;
  Lev[u] = L;
  for (int i = 0; i < A[u].size(); i++) {
    int v = A[u][i].first;
    if (v == p) continue;
    ar[v] = A[u][i].second;
    Dfs(v, u, L + 1);
    Tot[u] += Tot[v];
  }
  return;
}
vector<pair<int, int> > tst;
void Hld(int u, int p) {
  if (St[ChainNo] == -1) {
    St[ChainNo] = u;
  }
  Pos[u] = ++now;
  br[now] = ar[u];
  tst.push_back(make_pair(br[now], now));
  IdNo[u] = ChainNo;
  int Special, mx = -1;
  for (int i = 0; i < A[u].size(); i++) {
    int v = A[u][i].first;
    if (v == p) continue;
    if (mx < Tot[v]) {
      mx = Tot[v];
      Special = v;
    }
  }
  if (mx != -1) Hld(Special, u);
  for (int i = 0; i < A[u].size(); i++) {
    int v = A[u][i].first;
    if (v == p || v == Special) continue;
    ChainNo++;
    Hld(v, u);
  }
}
int Lca(int u, int v) {
  while (true) {
    int unom = IdNo[u], vnom = IdNo[v];
    int ustart = St[unom], vstart = St[vnom];
    if (unom == vnom)
      return (Lev[u] < Lev[v] ? u : v);
    else if (Lev[ustart] < Lev[vstart])
      v = Par[vstart];
    else
      u = Par[ustart];
  }
}
int fr[500007];
struct z {
  int l, r, x, id;
  z() {}
  z(int p, int q, int y, int c) { l = p, r = q, x = y, id = c; }
  bool operator<(const z &p) const { return (x < p.x); }
};
vector<z> V;
struct data {
  int prf, suf, tot;
  long long int an;
  data() {}
  data(int x, int y, int z, long long int o) {
    prf = x, suf = y, tot = z;
    an = o;
  }
};
data Tree[500007 * 4];
long long int FindAns(data a) {
  if (a.prf == a.tot) {
    return a.an + fr[a.prf];
  }
  return a.an + fr[a.prf] + fr[a.suf];
}
data mix(data a, data b) {
  data ans;
  ans.an = a.an + b.an;
  ans.prf = a.prf;
  ans.suf = b.suf;
  int fl = 0;
  if (a.prf == a.tot) {
    ans.prf = a.prf + b.prf;
    fl = 1;
  }
  if (b.suf == b.tot) {
    ans.suf = b.suf + a.suf;
    fl = 1;
  }
  ans.tot = a.tot + b.tot;
  if (!fl) {
    ans.an += fr[a.suf + b.prf];
  }
  return ans;
}
void wow(int id, int l, int r) {
  if (l == r) {
    Tree[id] = data(1, 1, 1, 0);
    return;
  }
  int mid = (l + r) / 2, lft = 2 * id, rgt = lft + 1;
  wow(lft, l, mid);
  wow(rgt, mid + 1, r);
  Tree[id] = mix(Tree[lft], Tree[rgt]);
}
void Upd(int id, int l, int r, int pos) {
  if (l == r) {
    Tree[id] = data(0, 0, 1, 0);
    return;
  }
  int mid = (l + r) / 2, lft = 2 * id, rgt = lft + 1;
  if (pos <= mid)
    Upd(lft, l, mid, pos);
  else
    Upd(rgt, mid + 1, r, pos);
  Tree[id] = mix(Tree[lft], Tree[rgt]);
}
data Qry(int id, int l, int r, int st, int ed) {
  if (st <= l && ed >= r) return Tree[id];
  int mid = (l + r) / 2, lft = 2 * id, rgt = lft + 1;
  if (ed <= mid)
    return Qry(lft, l, mid, st, ed);
  else if (st > mid)
    return Qry(rgt, mid + 1, r, st, ed);
  else {
    data a = Qry(lft, l, mid, st, ed);
    data b = Qry(rgt, mid + 1, r, st, ed);
    return mix(a, b);
  }
}
vector<pair<int, int> > tmp[2];
void ClimpChainFromU(int u, int v, int f) {
  while (true) {
    int uchain = IdNo[u], vchain = IdNo[v];
    int st = St[uchain];
    if (uchain == vchain) {
      int stt = Pos[v] + 1, ed = Pos[u];
      if (stt <= ed) {
        tmp[f].push_back(make_pair(stt, ed));
      }
      return;
    }
    int stt = Pos[st], ed = Pos[u];
    tmp[f].push_back(make_pair(stt, ed));
    u = Par[st];
  }
}
long long int Ans[500007];
int main() {
  int n, q;
  n = in<int>(), q = in<int>();
  for (int i = 1; i < n; i++) {
    fr[i] = in<int>();
  }
  for (int i = 1; i < n; i++) {
    int a, b, c;
    a = in<int>(), b = in<int>(), c = in<int>();
    A[a].push_back(make_pair(b, c));
    A[b].push_back(make_pair(a, c));
  }
  ChainNo = 1;
  for (int i = 1; i <= n; i++) {
    St[i] = -1;
  }
  Dfs(1, 0, 1);
  Hld(1, 0);
  wow(1, 1, n);
  for (int i = 1; i < q + 1; i++) {
    int l, r, x;
    l = in<int>(), r = in<int>(), x = in<int>();
    V.push_back(z(l, r, x, i));
  }
  sort(V.begin(), V.end());
  int ps = 0;
  sort(tst.begin(), tst.end());
  for (int i = 0; i < V.size(); i++) {
    int u = V[i].l;
    int v = V[i].r;
    int x = V[i].x;
    while (ps < tst.size() && tst[ps].first < x) {
      Upd(1, 1, n, tst[ps].second);
      ps++;
    }
    int id = V[i].id;
    int lc = Lca(u, v);
    ClimpChainFromU(u, lc, 0);
    ClimpChainFromU(v, lc, 1);
    reverse(tmp[0].begin(), tmp[0].end());
    reverse(tmp[1].begin(), tmp[1].end());
    data ans = data(0, 0, 0, 0);
    for (int j = 0; j < tmp[0].size(); j++) {
      ans = mix(ans, Qry(1, 1, n, tmp[0][j].first, tmp[0][j].second));
    }
    swap(ans.prf, ans.suf);
    for (int j = 0; j < tmp[1].size(); j++) {
      ans = mix(ans, Qry(1, 1, n, tmp[1][j].first, tmp[1][j].second));
    }
    Ans[id] = FindAns(ans);
    tmp[1].clear();
    tmp[0].clear();
  }
  for (int i = 1; i < q + 1; i++) {
    printf("%lld\n", Ans[i]);
  }
  return 0;
}

### Prompt

In CPP, your task is to solve the following problem:

Tavas lives in Tavaspolis. Tavaspolis has n cities numbered from 1 to n connected by n - 1 bidirectional roads. There exists a path between any two cities. Also each road has a length.

<image>

Tavas' favorite strings are binary strings (they contain only 0 and 1). For any binary string like s = s1s2... sk, T(s) is its Goodness. T(s) can be calculated as follows:

Consider there are exactly m blocks of 1s in this string (a block of 1s in s is a maximal consecutive substring of s that only contains 1) with lengths x1, x2, ..., xm.

Define <image> where f is a given sequence (if m = 0, then T(s) = 0).

Tavas loves queries. He asks you to answer q queries. In each query he gives you numbers v, u, l and you should print following number:

Consider the roads on the path from city v to city u: e1, e2, ..., ex.

Build the binary string b of length x such that: bi = 1 if and only if l ≤ w(ei) where w(e) is the length of road e.

You should print T(b) for this query.

Input

The first line of input contains integers n and q (2 ≤ n ≤ 105 and 1 ≤ q ≤ 105).

The next line contains n - 1 space separated integers f1, f2, ..., fn - 1 (|fi| ≤ 1000).

The next n - 1 lines contain the details of the roads. Each line contains integers v, u and w and it means that there's a road between cities v and u of length w (1 ≤ v, u ≤ n and 1 ≤ w ≤ 109).

The next q lines contain the details of the queries. Each line contains integers v, u, l (1 ≤ v, u ≤ n, v ≠ u and 1 ≤ l ≤ 109).

Output

Print the answer of each query in a single line.

Examples

Input

2 3
10
1 2 3
1 2 2
1 2 3
1 2 4


Output

10
10
0


Input

6 6
-5 0 0 2 10
1 2 1
2 3 2
3 4 5
4 5 1
5 6 5
1 6 1
1 6 2
1 6 5
3 6 5
4 6 4
1 4 2


Output

10
-5
-10
-10
-5
0

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
template <typename T>
inline T POW(T B, T P) {
  if (P == 0) return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return (POW(B, P / 2) * POW(B, P / 2));
}
template <typename T>
inline T Gcd(T a, T b) {
  if (a < 0) return Gcd(-a, b);
  if (b < 0) return Gcd(a, -b);
  return (b == 0) ? a : Gcd(b, a % b);
}
template <typename T>
inline T Lcm(T a, T b) {
  if (a < 0) return Lcm(-a, b);
  if (b < 0) return Lcm(a, -b);
  return a * (b / Gcd(a, b));
}
long long Bigmod(long long base, long long power, long long MOD) {
  long long ret = 1;
  while (power) {
    if (power & 1) ret = (ret * base) % MOD;
    base = (base * base) % MOD;
    power >>= 1;
  }
  return ret;
}
bool isVowel(char ch) {
  ch = toupper(ch);
  if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
    return true;
  return false;
}
template <typename T>
long long int isLeft(T a, T b, T c) {
  return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
long long ModInverse(long long number, long long MOD) {
  return Bigmod(number, MOD - 2, MOD);
}
bool isConst(char ch) {
  if (isalpha(ch) && !isVowel(ch)) return true;
  return false;
}
int toInt(string s) {
  int sm;
  stringstream second(s);
  second >> sm;
  return sm;
}
vector<pair<int, int> > A[500007];
int Tot[500007], ChainNo, Pos[500007], now;
int St[500007], IdNo[500007], Par[500007], Lev[500007];
int ar[500007];
int br[500007];
void Dfs(int u, int p, int L) {
  Par[u] = p;
  Tot[u] = 1;
  Lev[u] = L;
  for (int i = 0; i < A[u].size(); i++) {
    int v = A[u][i].first;
    if (v == p) continue;
    ar[v] = A[u][i].second;
    Dfs(v, u, L + 1);
    Tot[u] += Tot[v];
  }
  return;
}
vector<pair<int, int> > tst;
void Hld(int u, int p) {
  if (St[ChainNo] == -1) {
    St[ChainNo] = u;
  }
  Pos[u] = ++now;
  br[now] = ar[u];
  tst.push_back(make_pair(br[now], now));
  IdNo[u] = ChainNo;
  int Special, mx = -1;
  for (int i = 0; i < A[u].size(); i++) {
    int v = A[u][i].first;
    if (v == p) continue;
    if (mx < Tot[v]) {
      mx = Tot[v];
      Special = v;
    }
  }
  if (mx != -1) Hld(Special, u);
  for (int i = 0; i < A[u].size(); i++) {
    int v = A[u][i].first;
    if (v == p || v == Special) continue;
    ChainNo++;
    Hld(v, u);
  }
}
int Lca(int u, int v) {
  while (true) {
    int unom = IdNo[u], vnom = IdNo[v];
    int ustart = St[unom], vstart = St[vnom];
    if (unom == vnom)
      return (Lev[u] < Lev[v] ? u : v);
    else if (Lev[ustart] < Lev[vstart])
      v = Par[vstart];
    else
      u = Par[ustart];
  }
}
int fr[500007];
struct z {
  int l, r, x, id;
  z() {}
  z(int p, int q, int y, int c) { l = p, r = q, x = y, id = c; }
  bool operator<(const z &p) const { return (x < p.x); }
};
vector<z> V;
struct data {
  int prf, suf, tot;
  long long int an;
  data() {}
  data(int x, int y, int z, long long int o) {
    prf = x, suf = y, tot = z;
    an = o;
  }
};
data Tree[500007 * 4];
long long int FindAns(data a) {
  if (a.prf == a.tot) {
    return a.an + fr[a.prf];
  }
  return a.an + fr[a.prf] + fr[a.suf];
}
data mix(data a, data b) {
  data ans;
  ans.an = a.an + b.an;
  ans.prf = a.prf;
  ans.suf = b.suf;
  int fl = 0;
  if (a.prf == a.tot) {
    ans.prf = a.prf + b.prf;
    fl = 1;
  }
  if (b.suf == b.tot) {
    ans.suf = b.suf + a.suf;
    fl = 1;
  }
  ans.tot = a.tot + b.tot;
  if (!fl) {
    ans.an += fr[a.suf + b.prf];
  }
  return ans;
}
void wow(int id, int l, int r) {
  if (l == r) {
    Tree[id] = data(1, 1, 1, 0);
    return;
  }
  int mid = (l + r) / 2, lft = 2 * id, rgt = lft + 1;
  wow(lft, l, mid);
  wow(rgt, mid + 1, r);
  Tree[id] = mix(Tree[lft], Tree[rgt]);
}
void Upd(int id, int l, int r, int pos) {
  if (l == r) {
    Tree[id] = data(0, 0, 1, 0);
    return;
  }
  int mid = (l + r) / 2, lft = 2 * id, rgt = lft + 1;
  if (pos <= mid)
    Upd(lft, l, mid, pos);
  else
    Upd(rgt, mid + 1, r, pos);
  Tree[id] = mix(Tree[lft], Tree[rgt]);
}
data Qry(int id, int l, int r, int st, int ed) {
  if (st <= l && ed >= r) return Tree[id];
  int mid = (l + r) / 2, lft = 2 * id, rgt = lft + 1;
  if (ed <= mid)
    return Qry(lft, l, mid, st, ed);
  else if (st > mid)
    return Qry(rgt, mid + 1, r, st, ed);
  else {
    data a = Qry(lft, l, mid, st, ed);
    data b = Qry(rgt, mid + 1, r, st, ed);
    return mix(a, b);
  }
}
vector<pair<int, int> > tmp[2];
void ClimpChainFromU(int u, int v, int f) {
  while (true) {
    int uchain = IdNo[u], vchain = IdNo[v];
    int st = St[uchain];
    if (uchain == vchain) {
      int stt = Pos[v] + 1, ed = Pos[u];
      if (stt <= ed) {
        tmp[f].push_back(make_pair(stt, ed));
      }
      return;
    }
    int stt = Pos[st], ed = Pos[u];
    tmp[f].push_back(make_pair(stt, ed));
    u = Par[st];
  }
}
long long int Ans[500007];
int main() {
  int n, q;
  n = in<int>(), q = in<int>();
  for (int i = 1; i < n; i++) {
    fr[i] = in<int>();
  }
  for (int i = 1; i < n; i++) {
    int a, b, c;
    a = in<int>(), b = in<int>(), c = in<int>();
    A[a].push_back(make_pair(b, c));
    A[b].push_back(make_pair(a, c));
  }
  ChainNo = 1;
  for (int i = 1; i <= n; i++) {
    St[i] = -1;
  }
  Dfs(1, 0, 1);
  Hld(1, 0);
  wow(1, 1, n);
  for (int i = 1; i < q + 1; i++) {
    int l, r, x;
    l = in<int>(), r = in<int>(), x = in<int>();
    V.push_back(z(l, r, x, i));
  }
  sort(V.begin(), V.end());
  int ps = 0;
  sort(tst.begin(), tst.end());
  for (int i = 0; i < V.size(); i++) {
    int u = V[i].l;
    int v = V[i].r;
    int x = V[i].x;
    while (ps < tst.size() && tst[ps].first < x) {
      Upd(1, 1, n, tst[ps].second);
      ps++;
    }
    int id = V[i].id;
    int lc = Lca(u, v);
    ClimpChainFromU(u, lc, 0);
    ClimpChainFromU(v, lc, 1);
    reverse(tmp[0].begin(), tmp[0].end());
    reverse(tmp[1].begin(), tmp[1].end());
    data ans = data(0, 0, 0, 0);
    for (int j = 0; j < tmp[0].size(); j++) {
      ans = mix(ans, Qry(1, 1, n, tmp[0][j].first, tmp[0][j].second));
    }
    swap(ans.prf, ans.suf);
    for (int j = 0; j < tmp[1].size(); j++) {
      ans = mix(ans, Qry(1, 1, n, tmp[1][j].first, tmp[1][j].second));
    }
    Ans[id] = FindAns(ans);
    tmp[1].clear();
    tmp[0].clear();
  }
  for (int i = 1; i < q + 1; i++) {
    printf("%lld\n", Ans[i]);
  }
  return 0;
}

```