#include <bits/stdc++.h>
using namespace std;
struct tree {
  int root, curd, mxd, rnk;
  int ord[100005], baba[17][100005], mx[100005], der[100005], res[100005],
      sum[100005], val[100005];
  vector<pair<int, int> > v[100005];
  void add_edge(int x, int y, int z) {
    v[x].push_back({y, z});
    v[y].push_back({x, z});
  }
  void dfs(int node, int ata) {
    if (curd > mxd) {
      mxd = curd;
      root = node;
    }
    for (pair<int, int> i : v[node]) {
      if (i.first != ata) {
        curd += i.second;
        dfs(i.first, node);
        curd -= i.second;
      }
    }
  }
  void findroot(int node) {
    curd = 0;
    mxd = -1;
    dfs(node, 0);
  }
  void dfs2(int node, int ata) {
    baba[0][node] = ata;
    for (int i = 1; i < 17; i++) {
      baba[i][node] = baba[i - 1][baba[i - 1][node]];
    }
    mx[node] = node;
    for (pair<int, int> i : v[node]) {
      if (i.first != ata) {
        der[i.first] = der[node] + i.second;
        dfs2(i.first, node);
        if (der[mx[node]] < der[mx[i.first]]) {
          mx[node] = mx[i.first];
        }
      }
    }
  }
  void solve() {
    dfs2(root, 0);
    auto cmp = [&](int x, int y) { return val[x] < val[y]; };
    priority_queue<int, vector<int>, decltype(cmp)> q(cmp);
    q.push(root);
    ord[0] = -1;
    while (!q.empty()) {
      int node = q.top();
      q.pop();
      ++rnk;
      int low = node;
      while (!ord[node]) {
        ord[node] = rnk;
        for (pair<int, int> i : v[node]) {
          if (i.first == baba[0][node] || mx[i.first] == low) continue;
          val[mx[i.first]] = der[mx[i.first]] - der[node];
          q.push(mx[i.first]);
        }
        node = baba[0][node];
      }
      res[rnk] = der[low] - der[node];
      sum[rnk] = sum[rnk - 1] + res[rnk];
    }
  }
  int result(int node, int tot) {
    tot = min(tot, rnk);
    if (ord[node] <= tot) return sum[tot];
    int ans = der[mx[node]] + sum[tot];
    for (int i = 17 - 1; i >= 0; i--) {
      if (ord[baba[i][node]] >= tot) {
        node = baba[i][node];
      }
    }
    node = baba[0][node];
    ans -= der[node];
    ans -= min(res[tot], der[mx[node]] - der[node]);
    return ans;
  }
} t[2];
int n, q, x, y, z;
int main() {
  scanf("%d %d", &n, &q);
  for (int i = 1; i < n; i++) {
    scanf("%d %d %d", &x, &y, &z);
    t[0].add_edge(x, y, z);
    t[1].add_edge(x, y, z);
  }
  t[0].findroot(1);
  t[1].findroot(t[0].root);
  t[0].solve();
  t[1].solve();
  int ans = 0;
  while (q--) {
    scanf("%d %d", &x, &y);
    x = (x + ans - 1) % n + 1;
    y = (y + ans - 1) % n + 1;
    printf("%d\n", ans = max(t[0].result(x, 2 * y), t[1].result(x, 2 * y)));
  }
}
