#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
using ll = long long;
int n;
vector<pair<int, int>> v[N];
struct Tree {
  int dis[N], f[N][17], lson[N], lval[N], top[N], lend[N];
  pair<int, int> b[N];
  int bidx;
  void dfs(int pos, int fa) {
    f[pos][0] = fa;
    for (int i = 1; i <= 16; i++) f[pos][i] = f[f[pos][i - 1]][i - 1];
    for (auto &i : v[pos])
      if (i.first != fa) {
        dis[i.first] = dis[pos] + i.second, dfs(i.first, pos);
        if (lval[i.first] + i.second > lval[pos])
          lval[pos] = lval[i.first] + i.second, lson[pos] = i.first;
      }
  }
  void dfs2(int pos, int tp) {
    top[pos] = tp;
    if (!lson[pos]) {
      lend[tp] = pos, b[++bidx] = {dis[pos] - dis[f[tp][0]], tp};
      return;
    }
    dfs2(lson[pos], tp);
    for (auto &i : v[pos])
      if (i.first != f[pos][0] && i.first != lson[pos]) dfs2(i.first, i.first);
  }
  int mp[N], sum[N];
  void init(int rt) {
    dfs(rt, 0);
    dfs2(rt, rt);
    sort(b + 1, b + bidx + 1,
         [](pair<int, int> a, pair<int, int> b) { return a.first > b.first; });
    for (int i = 1; i <= bidx; i++)
      sum[i] = sum[i - 1] + b[i].first, mp[b[i].second] = i;
    for (int i = 1; i <= n; i++) mp[i] = mp[top[i]];
  }
  int query(int x, int y) {
    if (bidx / 2 + 1 <= y) return sum[bidx];
    y = (y - 1) * 2 + 1;
    if (mp[x] <= y) return sum[y];
    int z = x;
    for (int i = 16; i >= 0; --i)
      if (mp[f[z][i]] > y) z = f[z][i];
    z = f[z][0];
    return max(sum[y - 1], sum[y] - dis[lend[top[z]]] + dis[z]) +
           dis[lend[top[x]]] - dis[z];
  }
} a[2];
int dis[N];
void dfs(int pos, int fa) {
  for (auto &i : v[pos])
    if (i.first != fa) dis[i.first] = dis[pos] + i.second, dfs(i.first, pos);
}
int q;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> q;
  int t1, t2, t3;
  for (int i = 1; i < n; i++) {
    cin >> t1 >> t2 >> t3;
    v[t1].push_back({t2, t3});
    v[t2].push_back({t1, t3});
  }
  dfs(1, 0);
  a[0].init(max_element(dis + 1, dis + n + 1) - dis);
  a[1].init(max_element(a[0].dis + 1, a[0].dis + n + 1) - a[0].dis);
  int ans = 0;
  while (q--) {
    int t1, t2;
    cin >> t1 >> t2;
    t1 = (t1 + ans - 1) % n + 1;
    t2 = (t2 + ans - 1) % n + 1;
    ans = a[a[1].dis[t1] > a[0].dis[t1]].query(t1, t2);
    cout << ans << endl;
  }
  return 0;
}
