#include <bits/stdc++.h>
template <class T>
inline void read(T &res) {
  char ch;
  while (ch = getchar(), !isdigit(ch))
    ;
  res = ch ^ 48;
  while (ch = getchar(), isdigit(ch)) res = res * 10 + ch - 48;
}
template <class T>
inline void put(T x) {
  if (x > 9) put(x / 10);
  putchar(x % 10 + 48);
}
using std::pair;
using std::vector;
const int N = 1e5 + 5;
const int M = 2e5 + 5;
int que[N];
bool vis[N];
int n, q, z, tot, qr, opt;
struct Edge {
  int to, cst;
  Edge *nxt;
} p[M], *lst[N], *P = p;
inline void Link(int x, int y, int z) {
  (++P)->nxt = lst[x];
  lst[x] = P;
  P->to = y;
  P->cst = z;
  (++P)->nxt = lst[y];
  lst[y] = P;
  P->to = x;
  P->cst = z;
}
template <class T>
inline T Max(T x, T y) {
  return x > y ? x : y;
}
struct tree {
  pair<int, int> pos[N];
  int anc[N][20], dis[N];
  int cst[N], pre[N], mx[N], son[N];
  int ed[N], _rank[N], id[N], sum[N];
  int rt, T;
  inline void findRoot(int src) {
    rt = src;
    for (int i = 1; i <= n; ++i) vis[i] = false;
    que[qr = 1] = src;
    vis[src] = true;
    dis[src] = 0;
    for (int i = 1, x, y; i <= qr; ++i) {
      x = que[i];
      for (Edge *e = lst[x]; e; e = e->nxt)
        if (y = e->to, !vis[y]) {
          que[++qr] = y;
          vis[y] = true;
          dis[y] = dis[x] + e->cst;
          if (dis[y] > dis[rt]) rt = y;
        }
    }
  }
  inline void dfs1(int x) {
    for (int i = 0; anc[x][i]; ++i) anc[x][i + 1] = anc[anc[x][i]][i];
    for (Edge *e = lst[x]; e; e = e->nxt) {
      int y = e->to;
      if (y == anc[x][0]) continue;
      anc[y][0] = x;
      cst[y] = e->cst;
      dis[y] = dis[x] + cst[y];
      dfs1(y);
      int tmp = mx[y] + cst[y];
      if (tmp > mx[x]) mx[x] = tmp, son[x] = y;
    }
  }
  inline void dfs2(int x) {
    if (son[x]) {
      id[son[x]] = id[x];
      sum[id[x]] += cst[son[x]];
      dfs2(son[x]);
    } else
      ed[id[x]] = x;
    for (Edge *e = lst[x]; e; e = e->nxt) {
      int y = e->to;
      if (y == anc[x][0] || y == son[x]) continue;
      id[y] = ++T;
      sum[T] = cst[y];
      dfs2(y);
    }
  }
  inline void init() {
    dis[rt] = 0;
    dfs1(rt);
    id[rt] = T = 1;
    dfs2(rt);
    for (int i = 1; i <= T; ++i) pos[i] = std::make_pair(sum[i], i);
    std::sort(pos + 1, pos + T + 1);
    std::reverse(pos + 1, pos + T + 1);
    for (int i = 1; i <= T; ++i) pre[i] = pre[i - 1] + pos[i].first;
    for (int i = 1; i <= T; ++i) _rank[pos[i].second] = i;
  }
  inline int query(int x, int y) {
    int u = x;
    for (int i = 16; i >= 0; --i)
      if (anc[u][i] && _rank[id[anc[u][i]]] > y) u = anc[u][i];
    u = anc[u][0];
    return Max(pre[y - 1], pre[y] - mx[u]) + dis[ed[id[x]]] - dis[u];
  }
  inline int calc(int x, int y) {
    return _rank[id[x]] <= y ? pre[y] : query(x, y);
  }
} t1, t2;
int main() {
  read(n);
  read(q);
  opt = 1;
  for (int i = 1, x, y, z; i < n; ++i) {
    read(x);
    read(y);
    read(z);
    Link(x, y, z);
    tot += z;
  }
  t1.findRoot(1);
  t2.findRoot(t1.rt);
  t1.init();
  t2.init();
  int last_ans = 0, x, y;
  while (q--) {
    read(x);
    read(y);
    if (opt) {
      x = (x + last_ans - 1) % n + 1;
      y = (y + last_ans - 1) % n + 1;
    }
    y = (y << 1) - 1;
    put(last_ans = y >= t1.T ? tot : Max(t1.calc(x, y), t2.calc(x, y))),
        putchar('\n');
  }
  return 0;
}
