#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
  int f = 0;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar()) f |= (ch == '-');
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  if (f) x = -x;
}
const int N = 200005;
int d[N], rt, rt2, n, Q, a, b, c, x, y, ans;
vector<pair<int, int> > e[N];
void getrt(int u, int fa) {
  for (auto v : e[u])
    if (v.first != fa) {
      d[v.first] = d[u] + v.second;
      getrt(v.first, u);
    }
}
struct T {
  int d[N], sn[N], fa[N], anc[N], f[N], g[N], h[N], res[N];
  int cnt;
  void dp(int u) {
    for (auto v : e[u])
      if (v.first != fa[u]) {
        fa[v.first] = u;
        d[v.first] = d[u] + v.second;
        dp(v.first);
        f[v.first] += v.second;
        if (f[v.first] > f[u]) {
          f[u] = f[v.first];
          sn[u] = v.first;
        }
      }
    for (auto v : e[u])
      if (v.first != fa[u] && v.first != sn[u]) h[++cnt] = v.first;
  }
  void dfs(int u, int top) {
    anc[u] = top;
    if (!g[u]) g[u] = g[fa[u]];
    if (sn[u]) dfs(sn[u], top);
    for (auto v : e[u])
      if (v.first != fa[u] && v.first != sn[u]) dfs(v.first, v.first);
  }
  int solve(int x, int y) {
    int tot = 2 * y - 1;
    if (g[x] <= tot) return res[min(cnt, tot)];
    int ans1 = res[tot - 1] + f[sn[x]] + d[x];
    int ans2 = res[tot] + f[sn[x]] + d[x], t = x;
    while (g[x] >= tot) x = fa[anc[x]];
    ans1 -= d[x];
    while (g[t] > tot) t = fa[anc[t]];
    ans2 = ans2 - d[t] - f[sn[t]];
    return max(ans1, ans2);
  }
  void rmain(int rt) {
    d[rt] = 0, dp(rt), h[++cnt] = rt;
    sort(h + 1, h + cnt + 1, [&](int a, int b) { return f[a] > f[b]; });
    for (int i = (1); i <= (cnt); i++) {
      g[h[i]] = i;
      res[i] = res[i - 1] + f[h[i]];
    }
    dfs(rt, rt);
  }
} A, B;
int main() {
  read(n), read(Q);
  for (int i = (1); i <= (n - 1); i++) {
    read(a), read(b), read(c);
    e[a].push_back(make_pair(b, c));
    e[b].push_back(make_pair(a, c));
  }
  getrt(1, 0), rt = 1;
  for (int i = (2); i <= (n); i++)
    if (d[i] > d[rt]) rt = i;
  getrt(rt, 0), rt2 = 1;
  for (int i = (2); i <= (n); i++)
    if (d[i] > d[rt2]) rt2 = i;
  A.rmain(rt), B.rmain(rt2);
  while (Q--) {
    read(x), read(y);
    x = (x + ans - 1) % n + 1;
    y = (y + ans - 1) % n + 1;
    ans = max(A.solve(x, y), B.solve(x, y));
    printf("%d\n", ans);
  }
  return 0;
}
