#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
const int N = 1e5 + 1000;
int n, T, tot;
int head[N];
struct node {
  int nex, to, val;
} e[N << 1];
void add(int f, int t, int v) {
  e[++tot].nex = head[f];
  e[tot].to = t;
  e[tot].val = v;
  head[f] = tot;
}
struct T {
  int rt, mxpos, c;
  int Ans[N], col[N], s[N];
  int dis[N], dep[N], maxdep[N], fa[N][17], son[N], top[N], len[N];
  vector<int> V[N];
  void dfs(int now, int Fa, int type) {
    if (type) {
      dep[now] = dep[fa[now][0] = Fa] + 1;
      for (int j = 1; 1 << j < dep[now]; j++)
        fa[now][j] = fa[fa[now][j - 1]][j - 1];
    }
    maxdep[now] = dis[now];
    if (!type && dis[mxpos] < dis[now]) mxpos = now;
    for (int i = head[now], to; i; i = e[i].nex) {
      to = e[i].to;
      if (to == Fa) continue;
      dis[to] = dis[now] + e[i].val;
      dfs(to, now, type);
      maxdep[now] = max(maxdep[now], maxdep[to]);
      if (type && maxdep[son[now]] < maxdep[to]) son[now] = to;
    }
  }
  void dfs1(int now, int low) {
    V[top[now] = low].push_back(now);
    if (son[now]) dfs1(son[now], low);
    for (int i = head[now], to; i; i = e[i].nex) {
      to = e[i].to;
      if (to == fa[now][0] || to == son[now]) continue;
      dfs1(to, to);
      len[to] = maxdep[to] - dis[now];
    }
  }
  int init(int x) {
    mxpos = dis[x] = 0;
    dfs(x, 0, 0);
    dis[rt = mxpos] = 0;
    dfs(rt, 0, 1);
    dfs1(rt, rt);
    len[rt] = maxdep[rt];
    for (int i = 1; i <= n; i++)
      if (top[i] == i) s[++c] = i;
    sort(s + 1, s + c + 1, [&](int i, int j) { return len[i] > len[j]; });
    for (int i = 1; i <= c; i++) {
      Ans[i] = Ans[i - 1] + len[s[i]];
      for (int j = s[i]; j; j = son[j]) col[j] = i;
    }
    return rt;
  }
  int plan(int x, int y, int *arr) {
    int now = x;
    for (int j = 16; j >= 0; j--)
      if (col[fa[now][j]] > y) now = fa[now][j];
    now = fa[now][0];
    return Ans[y] + maxdep[x] - arr[now];
  }
  int ask(int x, int y) {
    y = y * 2 - 1;
    if (y >= c) return Ans[c];
    if (col[x] <= y) return Ans[y];
    return max(plan(x, y - 1, dis), plan(x, y, maxdep));
  }
} t1, t2;
int main() {
  n = read();
  T = read();
  for (int i = 1, x, y, z; i < n; i++) {
    x = read();
    y = read();
    z = read();
    add(x, y, z);
    add(y, x, z);
  }
  t2.init(t1.init(1));
  int ans = 0, x, y;
  while (T--) {
    x = read();
    y = read();
    x = (x + ans - 1) % n + 1;
    y = (y + ans - 1) % n + 1;
    cout << (ans = max(t1.ask(x, y), t2.ask(x, y))) << "\n";
  }
  return 0;
}
