#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
multiset<int> st[N];
int n, q, head[N], adj[N * 2], nxt[N * 2], len[N * 2], i, j, k, x, rt, mxdep[N],
    son[N], falen[N], p[N], col[N], cnt, s[N], fa[N][20], dis[N], ans;
void dfs(int x, int dad, int d) {
  if (d > j) rt = x, j = d;
  for (int y = head[x]; y; y = nxt[y])
    if (adj[y] != dad) dfs(adj[y], x, d + len[y]);
}
void Dfs(int x, int dad) {
  mxdep[x] = 0;
  for (int y = head[x]; y; y = nxt[y])
    if (adj[y] != dad) {
      fa[adj[y]][0] = x;
      falen[adj[y]] = len[y];
      dis[adj[y]] = dis[x] + len[y];
      Dfs(adj[y], x);
      if (mxdep[adj[y]] + len[y] > mxdep[x]) {
        mxdep[x] = mxdep[adj[y]] + len[y];
        son[x] = adj[y];
      }
    }
}
bool cmp(int a, int b) { return mxdep[a] + falen[a] > mxdep[b] + falen[b]; }
int main() {
  scanf("%d%d", &n, &q);
  for (i = 1; i < n; ++i) {
    scanf("%d%d%d", &j, &k, &len[i * 2]);
    adj[i * 2 - 1] = k;
    nxt[i * 2 - 1] = head[j];
    head[j] = i * 2 - 1;
    adj[i * 2] = j;
    nxt[i * 2] = head[k];
    head[k] = i * 2;
    len[i * 2 - 1] = len[i * 2];
  }
  rt = 1, j = 0;
  dfs(1, -1, 0);
  Dfs(rt, -1);
  for (i = 1; i <= n; ++i) p[i] = i;
  sort(p + 1, p + 1 + n, cmp);
  s[cnt = 1] = mxdep[rt];
  for (j = rt; j; j = son[j]) col[j] = 1;
  for (i = 1; i <= n; ++i)
    if (!col[p[i]]) {
      ++cnt;
      s[cnt] = s[cnt - 1] + mxdep[p[i]] + falen[p[i]];
      for (j = p[i]; j; j = son[j]) col[j] = cnt;
    }
  for (i = 1; (1 << i) < n; ++i)
    for (j = 1; j <= n; ++j)
      if (fa[j][i - 1]) fa[j][i] = fa[fa[j][i - 1]][i - 1];
  while (q--) {
    scanf("%d%d", &i, &j);
    i = (i + ans - 1 + n) % n + 1;
    j = (j + ans - 1 + n) % n + 1;
    j = min(cnt, j * 2 - 1);
    if (col[i] <= j) {
      printf("%d\n", ans = s[j]);
      continue;
    }
    ans = s[j - 1];
    for (k = i, x = 18; x >= 0; --x)
      if (col[fa[k][x]] >= j) k = fa[k][x];
    ans += dis[i] - dis[fa[k][0]] + mxdep[i];
    for (k = i, x = 18; x >= 0; --x)
      if (col[fa[k][x]] > j) k = fa[k][x];
    ans = max(ans, s[j] - min(mxdep[fa[k][0]], dis[fa[k][0]]) + dis[i] -
                       dis[fa[k][0]] + mxdep[i]);
    printf("%d\n", ans);
  }
  return 0;
}
