#include <bits/stdc++.h>
using namespace std;
const int N = 5e5;
int fst[N], nxt[N], mm, n, q, dis[N];
struct Edge {
  int v, w;
} e[N];
void ade(int u, int v, int w) {
  e[++mm] = (Edge){v, w}, nxt[mm] = fst[u], fst[u] = mm;
}
int dfs(int u, int fa) {
  int ans = u;
  for (int i = fst[u]; i; i = nxt[i]) {
    int v = e[i].v;
    if (v == fa) continue;
    dis[v] = dis[u] + e[i].w;
    int t = dfs(v, u);
    if (dis[t] > dis[ans]) ans = t;
  }
  return ans;
}
struct TREE {
  int dep[N], len[N], son[N], fa[N][20], id[N], sum[N], tn, tp[N];
  pair<int, int> tmp[N];
  void dfs1(int u, int Fa) {
    fa[u][0] = Fa;
    for (int i = 1; i <= 16; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
    for (int i = fst[u]; i; i = nxt[i]) {
      int v = e[i].v;
      if (v == Fa) continue;
      dep[v] = dep[u] + e[i].w;
      dfs1(v, u);
      len[v] += e[i].w;
      if (len[v] > len[u]) len[u] = len[v], son[u] = v;
    }
  }
  void dfs2(int u, int top) {
    tp[u] = top;
    if (son[u]) dfs2(son[u], top);
    for (int i = fst[u]; i; i = nxt[i]) {
      int v = e[i].v;
      if (v == son[u] || v == fa[u][0]) continue;
      dfs2(v, v);
    }
  }
  static int cmp(const pair<int, int> &a, const pair<int, int> &b) {
    return a > b;
  }
  void build(int root) {
    dfs1(root, 0);
    dfs2(root, root);
    tn = 0;
    for (int i = 1; i <= n; i++)
      if (tp[i] == i) tmp[++tn] = make_pair(len[i], i);
    sort(tmp + 1, tmp + tn + 1, cmp);
    for (int i = 1; i <= tn; i++) sum[i] = sum[i - 1] + tmp[i].first;
    for (int i = 1; i <= tn; i++) id[tmp[i].second] = i;
    for (int i = 1; i <= n; i++) id[i] = id[tp[i]];
  }
  int getfa(int u, int K) {
    for (int i = 16; i >= 0; i--)
      if (fa[u][i] && id[fa[u][i]] > K) u = fa[u][i];
    return fa[u][0];
  }
  int query(int x, int y) {
    y = 2 * y - 1;
    if (id[x] <= y) return sum[min(y, tn)];
    int u = getfa(x, y - 1), ans = sum[y - 1] + dep[x] - dep[u] + len[son[x]];
    u = getfa(x, y);
    return max(ans, sum[y] - len[son[u]] + dep[x] - dep[u] + len[son[x]]);
  }
} s1, s2;
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1, u, v, w; i < n; i++)
    scanf("%d%d%d", &u, &v, &w), ade(u, v, w), ade(v, u, w);
  int L = dfs(1, 0), R = dfs(L, 0);
  s1.build(L), s2.build(R);
  int lstans = 0;
  for (int i = 1, x, y; i <= q; i++) {
    scanf("%d%d", &x, &y);
    x = (x + lstans - 1) % n + 1, y = (y + lstans - 1) % n + 1;
    printf("%d\n", lstans = max(s1.query(x, y), s2.query(x, y)));
  }
}
