#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int LOG = 20;
int ans, n, q, a, b, dist[N], mx;
vector<pair<int, int> > ed[N];
void lp(int cur, int back, int d) {
  dist[cur] = d;
  if (d > dist[mx]) mx = cur;
  for (auto i : ed[cur])
    if (i.first != back) lp(i.first, cur, d + i.second);
}
struct tree {
  int dist[N], dad[LOG][N], maxval[N], pre[N], maxchi[N], maxleaf[N], last[N],
      rankval[N], leaves;
  pair<int, int> val[N];
  void dfs(int cur, int back, int d) {
    dad[0][cur] = back;
    for (int i = 1; i < LOG; i++) dad[i][cur] = dad[i - 1][dad[i - 1][cur]];
    dist[cur] = d;
    maxval[cur] = d;
    maxchi[cur] = maxleaf[cur] = cur;
    for (auto i : ed[cur]) {
      if (i.first == back) continue;
      dfs(i.first, cur, d + i.second);
      if (maxval[cur] < maxval[i.first]) {
        maxval[cur] = maxval[i.first];
        maxchi[cur] = i.first;
        maxleaf[cur] = maxleaf[i.first];
      }
    }
  }
  void dfs2(int cur, int back) {
    for (auto i : ed[cur]) {
      if (i.first == back) continue;
      if (maxchi[cur] == i.first)
        val[i.first].first = val[cur].first + i.second;
      else
        val[i.first].first = i.second;
      dfs2(i.first, cur);
    }
  }
  void add(int cur, int rank) {
    pre[rank] = pre[rank - 1] + val[rank].first;
    last[rank] = val[rank].first;
    while (dad[0][cur] && rankval[cur] == 0) {
      rankval[cur] = rank;
      cur = dad[0][cur];
    }
  }
  int query(int x, int y) {
    int rank = min(y, leaves), ans = pre[rank];
    if (rank >= rankval[x]) return ans;
    int u = x;
    for (int i = LOG - 1; i >= 0; i--)
      if (dad[i][u] != 0 && rankval[dad[i][u]] >= rank) u = dad[i][u];
    u = dad[0][u];
    int leaf = maxleaf[x];
    ans += dist[leaf] - dist[u];
    ans -= min(last[rank], maxval[u] - dist[u]);
    return ans;
  }
  void init(int cur) {
    dfs(cur, 0, 0);
    dfs2(cur, 0);
    for (int i = 1; i <= n; i++) {
      val[i].second = i;
      if (maxleaf[i] != i) val[i].first = 0;
    }
    sort(val + 1, val + n + 1, greater<pair<int, int> >());
    for (int i = 1; i <= n; i++) {
      if (val[i].first == 0) break;
      leaves++;
      add(val[i].second, i);
    }
  }
} t[2];
inline void pre_calc() {
  lp(1, -1, 0);
  a = mx;
  lp(a, -1, 0);
  b = mx;
  t[0].init(a);
  t[1].init(b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> q;
  for (int i = 1; i < n; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    ed[u].push_back({v, c});
    ed[v].push_back({u, c});
  }
  pre_calc();
  while (q--) {
    int x, y;
    cin >> x >> y;
    x = (x + ans - 1) % n + 1;
    y = (y + ans - 1) % n + 1;
    cout << (ans = max(t[0].query(x, 2 * y - 1), t[1].query(x, 2 * y - 1)))
         << "\n";
  }
}
