#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, M = 18;
int n, q, tot, deg[N], head[N], to[N << 1], nxt[N << 1], w[N << 1];
void add(int x, int y, int z) {
  to[++tot] = y, w[tot] = z, nxt[tot] = head[x], head[x] = tot;
  ++deg[y];
}
struct T {
  int rt;
  int dep[N], d[N], f[M][N], g[M][N], son[N], l[N], s[N], len, rk[N], dd[N];
  void dfs(int x, int pre) {
    if (d[x] > d[rt]) rt = x;
    for (int i = head[x]; i; i = nxt[i])
      if (to[i] ^ pre) {
        d[to[i]] = d[x] + w[i];
        dfs(to[i], x);
      }
  }
  void dfs2(int x, int pre) {
    f[0][x] = pre;
    for (int i = 1;; ++i)
      if (f[i - 1][x]) {
        f[i][x] = f[i - 1][f[i - 1][x]];
        g[i][x] = g[i - 1][x] + g[i - 1][f[i - 1][x]];
      } else
        break;
    for (int i = head[x]; i; i = nxt[i])
      if (to[i] ^ pre) {
        g[0][to[i]] = w[i];
        dfs2(to[i], x);
        if (dep[to[i]] + w[i] > dep[x])
          dep[x] = dep[to[i]] + w[i], son[x] = to[i];
      }
    for (int i = head[x]; i; i = nxt[i])
      if ((to[i] ^ pre) && (to[i] ^ son[x])) {
        dd[l[++len] = to[i]] = dep[to[i]] + w[i];
      }
  }
  void getrt(int x) {
    dfs(x, 0);
    dfs2(rt, 0);
    dd[l[++len] = rt] = dep[rt];
    sort(l + 1, l + len + 1, [&](int x, int y) { return dd[x] > dd[y]; });
    for (int i = 1; i <= len; ++i) {
      s[i] = s[i - 1] + dd[l[i]];
      int x = l[i];
      while (x) {
        rk[x] = i;
        x = son[x];
      }
    }
  }
  int solve1(int x, int y) {
    int ret = dep[x];
    for (int i = M - 1; i >= 0; --i)
      if (rk[f[i][x]] >= y) ret += g[i][x], x = f[i][x];
    return s[y - 1] + ret + g[0][x];
  }
  int solve2(int x, int y) {
    int ret = dep[x];
    for (int i = M - 1; i >= 0; --i)
      if (rk[f[i][x]] > y) ret += g[i][x], x = f[i][x];
    return s[y] - dep[f[0][x]] + ret + g[0][x];
  }
  int query(int x, int y) {
    y = y + y - 1;
    return rk[x] <= y ? s[y] : max(solve1(x, y), solve2(x, y));
  }
} t[2];
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> q;
  int s = 0, lf = 0;
  for (int i = 1; i < n; ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    add(x, y, z), add(y, x, z);
    s += z;
  }
  for (int i = 1; i <= n; ++i) lf += (deg[i] == 1);
  t[0].getrt(1);
  t[1].getrt(t[0].rt);
  int lst = 0;
  while (q--) {
    int x, y;
    cin >> x >> y;
    x = (x + lst - 1) % n + 1, y = (y + lst - 1) % n + 1;
    cout << (lst = (y * 2 >= lf ? s : max(t[0].query(x, y), t[1].query(x, y))))
         << endl;
  }
  return 0;
}
