#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int s = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) s = s * 10 + ch - '0', ch = getchar();
  return s;
}
const int N = 1e5 + 5;
int n, q;
struct edge {
  int to, w;
};
vector<edge> G[N];
struct Tree {
  int rt, cnt;
  int d[N], fa[N][20], dis[N][20], son[N], dep[N], top[N], len[N], s[N], rnk[N];
  void dfs1(int u, int f) {
    for (edge e : G[u])
      if (e.to != f) {
        d[e.to] = d[u] + e.w;
        dfs1(e.to, u);
      }
    if (d[u] > d[rt]) rt = u;
  }
  void dfs2(int u, int f) {
    for (edge e : G[u]) {
      int v = e.to;
      if (e.to != f) {
        fa[v][0] = u, dis[v][0] = e.w;
        for (int i = 1; i < 20; i++) {
          fa[v][i] = fa[fa[v][i - 1]][i - 1];
          dis[v][i] = dis[v][i - 1] + dis[fa[v][i - 1]][i - 1];
        }
        dfs2(v, u);
        if (dep[v] + e.w > dep[u]) {
          dep[u] = dep[v] + e.w;
          son[u] = v;
        }
      }
    }
    for (edge e : G[u]) {
      int v = e.to;
      if (v != f && v != son[u]) {
        top[++cnt] = v;
        len[top[cnt]] = dep[v] + e.w;
      }
    }
    if (u == rt) {
      top[++cnt] = rt;
      len[top[cnt]] = dep[rt];
    }
  }
  void get_rt(int x) {
    rt = x;
    d[x] = 0;
    dfs1(x, 0);
    dfs2(rt, 0);
    sort(top + 1, top + cnt + 1, [&](int i, int j) { return len[i] > len[j]; });
    for (int i = 1; i <= cnt; i++) s[i] = s[i - 1] + len[top[i]];
    for (int i = 1; i <= cnt; i++) {
      int x = top[i];
      while (x) rnk[x] = i, x = son[x];
    }
  }
  int query1(int x, int y) {
    int t = dep[x];
    for (int i = 19; i >= 0; i--)
      if (rnk[fa[x][i]] >= y) t += dis[x][i], x = fa[x][i];
    return s[y - 1] + t + dis[x][0];
  }
  int query2(int x, int y) {
    int t = dep[x];
    for (int i = 19; i >= 0; i--)
      if (rnk[fa[x][i]] > y) t += dis[x][i], x = fa[x][i];
    return s[y] - dep[fa[x][0]] + t + dis[x][0];
  }
  int query(int x, int y) {
    y = 2 * y - 1;
    return rnk[x] <= y ? s[y] : max(query1(x, y), query2(x, y));
  }
} t1, t2;
int main() {
  n = read(), q = read();
  int sum = 0, cnt = 0;
  for (int i = 1; i < n; i++) {
    int u = read(), v = read(), w = read();
    G[u].push_back({v, w});
    G[v].push_back({u, w});
    sum += w;
  }
  for (int i = 1; i <= n; i++)
    if (G[i].size() == 1) cnt++;
  t1.get_rt(1);
  t2.get_rt(t1.rt);
  int ans = 0;
  for (int i = 1; i <= q; i++) {
    int x = read(), y = read();
    x = (x + ans - 1) % n + 1, y = (y + ans - 1) % n + 1;
    ans = (2 * y >= cnt) ? sum : max(t1.query(x, y), t2.query(x, y));
    printf("%d\n", ans);
  }
  return 0;
}
