#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int n, q, de[N];
int tot, first[N], nxt[N * 2], point[N * 2], len[N * 2];
inline void ckmax(int &a, int b) { a = ((a > b) ? a : b); }
inline int read() {
  int f = 1, x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
  return x * f;
}
inline void add_edge(int x, int y, int z) {
  nxt[++tot] = first[x];
  first[x] = tot;
  point[tot] = y;
  len[tot] = z;
}
struct tree {
  int dep[N], de[N], son[N], bot[N], fa[N][21], top[N];
  int st[N], w, rt, v[N], sum[N], id[N];
  int tot, first[N], nxt[N * 2], point[N * 2], len[N * 2];
  inline void add_edge(int x, int y, int z) {
    nxt[++tot] = first[x];
    first[x] = tot;
    point[tot] = y;
    len[tot] = z;
  }
  void dfs(int x, int father) {
    fa[x][0] = father;
    for (int i = first[x]; i != -1; i = nxt[i]) {
      int u = point[i];
      if (u == father) continue;
      de[u] = de[x] + len[i];
      dfs(u, x);
      dep[u] += len[i];
      if (dep[son[x]] < dep[u]) son[x] = u;
    }
    dep[x] = dep[son[x]];
  }
  void dfs1(int x, int t) {
    top[x] = t;
    if (son[x])
      dfs1(son[x], t), bot[x] = bot[son[x]];
    else
      bot[x] = x;
    for (int i = first[x]; i != -1; i = nxt[i]) {
      int u = point[i];
      if (u == son[x] || u == fa[x][0]) continue;
      dfs1(u, u);
    }
  }
  void init_edge() {
    tot = -1;
    memset(first, -1, sizeof(first));
  }
  void init() {
    de[rt] = 0;
    dfs(rt, rt);
    dfs1(rt, rt);
    for (int j = 1; j <= 20; j++)
      for (int i = 1; i <= n; i++) fa[i][j] = fa[fa[i][j - 1]][j - 1];
    for (int i = 1; i <= n; i++)
      if (bot[i] == i) st[++w] = i, v[i] = de[i] - de[fa[top[i]][0]];
    sort(st + 1, st + 1 + w, [&](int i, int j) { return v[i] > v[j]; });
    for (int i = 1; i <= w; i++) sum[i] = sum[i - 1] + v[st[i]], id[st[i]] = i;
  }
  int query(int x, int y) {
    if (y == 1) return de[bot[x]];
    if (2 * y - 1 >= w) return sum[w];
    y = 2 * y - 1;
    if (id[bot[x]] <= y) return sum[y];
    int last, now = bot[x];
    for (int i = 20; i >= 0; i--)
      if (id[bot[fa[x][i]]] > y) x = fa[x][i];
    x = fa[x][0];
    last = sum[y - 1] + de[now] - de[x];
    ckmax(last, sum[y] + de[now] - de[bot[x]]);
    return last;
  }
} A;
tree B;
void dfs(int x, int fa) {
  for (int i = first[x]; i != -1; i = nxt[i]) {
    int u = point[i];
    if (u == fa) continue;
    de[u] = de[x] + len[i];
    dfs(u, x);
  }
}
signed main() {
  tot = -1;
  memset(first, -1, sizeof(first));
  A.init_edge();
  B.init_edge();
  n = read();
  q = read();
  for (int i = 1; i < n; i++) {
    int u = read(), v = read(), w = read();
    add_edge(u, v, w);
    add_edge(v, u, w);
    A.add_edge(u, v, w), A.add_edge(v, u, w);
    B.add_edge(u, v, w), B.add_edge(v, u, w);
  }
  int MAX = 0, rt, tmp;
  dfs(1, 1);
  for (int i = 1; i <= n; i++)
    if (de[i] > MAX) MAX = de[i], rt = i;
  de[rt] = 0;
  dfs(rt, rt);
  tmp = rt;
  MAX = 0;
  for (int i = 1; i <= n; i++)
    if (de[i] > MAX) MAX = de[i], rt = i;
  A.rt = rt;
  B.rt = tmp;
  A.init();
  B.init();
  int last = 0;
  while (q--) {
    int x = read(), y = read();
    x = (x + last - 1) % n + 1;
    y = (y + last - 1) % n + 1;
    last = max(A.query(x, y), B.query(x, y));
    printf("%d\n", last);
  }
}
