#include <bits/stdc++.h>
using namespace std;
struct edge {
  int nx, to, w;
} e[400101];
int fi[400101], tot;
void ps(int x, int y, int z) {
  e[++tot] = (edge){fi[x], y, z};
  fi[x] = tot;
}
int n;
struct T {
  int f[400101][22], g[400101][22], son[400101], s;
  int ans[400101], rk[400101], ord[400101], len[400101], dep[400101];
  vector<int> vec;
  void dfs_1(int x) {
    for (int i = fi[x], y; (y = e[i].to); i = e[i].nx)
      if (y != f[x][0]) {
        f[y][0] = x;
        g[y][0] = e[i].w;
        dfs_1(y);
        if (dep[y] + e[i].w > dep[x]) dep[x] = dep[y] + e[i].w, son[x] = y;
      }
    for (int i = fi[x], y; (y = e[i].to); i = e[i].nx)
      if (y != f[x][0] && y != son[x]) {
        len[y] = dep[y] + e[i].w;
        vec.push_back(y);
      }
  }
  bool cmp(int x, int y) { return len[x] > len[y]; }
  int d[400101];
  void dfs_0(int x, int fa) {
    for (int i = fi[x], y; (y = e[i].to); i = e[i].nx)
      if (y != fa) d[y] = d[x] + e[i].w, dfs_0(y, x);
  }
  void init(int x) {
    dfs_0(x, 0);
    for (int i = 1; i <= n; i++)
      if (d[i] > d[x]) x = i;
    s = x;
    dfs_1(s);
    len[s] = dep[s];
    vec.push_back(s);
    for (int i = 1; i <= 20; i++)
      for (int j = 1; j <= n; j++) {
        f[j][i] = f[f[j][i - 1]][i - 1];
        g[j][i] = g[f[j][i - 1]][i - 1] + g[j][i - 1];
      }
    sort(vec.begin(), vec.end(), [&](int i, int j) { return len[i] > len[j]; });
    int t = 0;
    for (int i = 0; i < (int)vec.size(); i++) {
      ans[i + 1] = ans[i] + len[vec[i]];
      rk[vec[i]] = i + 1;
    }
    for (auto x : vec) {
      for (int y = x; y; y = son[y]) rk[y] = rk[x];
    }
  }
  int re1(int x, int l) {
    int r = dep[x];
    for (int i = 20; i >= 0; i--) {
      if (rk[f[x][i]] > l - 1) r += g[x][i], x = f[x][i];
    }
    return r + g[x][0] + ans[l - 1];
  }
  int re2(int x, int l) {
    int r = dep[x];
    for (int i = 20; i >= 0; i--) {
      if (rk[f[x][i]] > l) r += g[x][i], x = f[x][i];
    }
    return r + g[x][0] + ans[l] - dep[f[x][0]];
  }
  int ask(int x, int l) {
    l = l * 2 - 1;
    l = min(l, (int)vec.size());
    if (l >= rk[x]) return ans[l];
    return max(re1(x, l), re2(x, l));
  }
} T[2];
int main() {
  int Q;
  scanf("%d%d", &n, &Q);
  for (int i = 1, a, c, b; i < n; i++) {
    scanf("%d%d%d", &a, &b, &c);
    ps(a, b, c);
    ps(b, a, c);
  }
  T[0].init(1);
  T[1].init(T[0].s);
  int lt = 0;
  while (Q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    x = (x + lt - 1) % n + 1;
    y = (y + lt - 1) % n + 1;
    printf("%d\n", lt = max(T[0].ask(x, y), T[1].ask(x, y)));
  }
}
