#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100100;
int n, q, k, s, ans;
vector<pair<int, int> > e[MAXN];
struct Tree {
  int rt, f[MAXN][21], g[MAXN][21];
  int d[MAXN], dep[MAXN], son[MAXN], top[MAXN], rk[MAXN];
  int l[MAXN], r[MAXN], s[MAXN], t;
  inline void dfs(int now, int pre) {
    for (pair<int, int> i : e[now])
      if (i.first != pre) d[i.first] = d[now] + i.second, dfs(i.first, now);
    return;
  }
  inline void dfs2(int now) {
    for (pair<int, int> i : e[now])
      if (i.first != f[now][0]) {
        f[i.first][0] = now, g[i.first][0] = i.second;
        for (int j = 1; j <= 20; j++)
          f[i.first][j] = f[f[i.first][j - 1]][j - 1],
          g[i.first][j] = g[i.first][j - 1] + g[f[i.first][j - 1]][j - 1];
        d[i.first] = d[now] + i.second, dfs2(i.first);
        if (dep[i.first] + i.second > dep[now])
          dep[now] = dep[i.first] + i.second, son[now] = i.first;
      }
    for (pair<int, int> i : e[now])
      if (i.first != f[now][0] && i.first != son[now])
        s[l[++t] = i.first] = dep[i.first] + i.second;
    return;
  }
  inline void getroot(int x) {
    dfs(x, 0), rt = x;
    for (int i = 1; i <= n; i++)
      if (d[i] > d[rt]) rt = i;
    d[rt] = 0, dfs2(rt), s[l[++t] = rt] = dep[rt];
    sort(l + 1, l + t + 1, [&](int i, int j) { return s[i] > s[j]; });
    for (int i = 1; i <= t; i++) r[i] = r[i - 1] + s[l[i]];
    for (int i = 1; i <= t; i++) {
      int now = l[i], top_ = now;
      while (now) top[now] = top_, rk[now] = i, now = son[now];
    }
    return;
  }
  inline int calc1(int x, int y) {
    int z = dep[x];
    for (int i = 20; ~i; i--)
      if (rk[f[x][i]] >= y) z = z + g[x][i], x = f[x][i];
    return r[y - 1] + z + g[x][0];
  }
  inline int calc2(int x, int y) {
    int z = dep[x];
    for (int i = 20; ~i; i--)
      if (rk[f[x][i]] > y) z = z + g[x][i], x = f[x][i];
    return r[y] - dep[f[x][0]] + z + g[x][0];
  }
  inline int calc(int x, int y) {
    y = (y << 1) - 1;
    return rk[x] <= y ? r[y] : max(calc1(x, y), calc2(x, y));
  }
};
Tree t[2];
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> q;
  for (int i = 1, x, y, z; i < n; i++)
    cin >> x >> y >> z, e[x].push_back(make_pair(y, z)),
        e[y].push_back(make_pair(x, z)), s += z;
  for (int i = 1; i <= n; i++) k += (e[i].size() == 1);
  t[0].getroot(1), t[1].getroot(t[0].rt);
  while (q--) {
    int x, y;
    cin >> x >> y;
    x = (x + ans - 1) % n + 1, y = (y + ans - 1) % n + 1;
    cout << (ans = ((y << 1) >= k ? s : max(t[0].calc(x, y), t[1].calc(x, y))))
         << "\n";
  }
  return 0;
}
