#include <bits/stdc++.h>
using namespace std;
const long long linf = 1e18 + 5;
const int mod = (int)1e9 + 7;
const int logN = 18;
const int inf = 1e9;
const int N = 2e5 + 5;
int n, m, q, x, y, z, mx, a, b, lca[2][logN + 1][N];
vector<pair<int, int> > v[N];
int pre[2][N], up[2][N], jonsnow[2][N], w[2][N], val[2][N], mx_depth[2][N],
    wh[2][N];
class tree {
 public:
  int ww, root;
  vector<pair<int, int> > leafs;
  tree(int r, int ww) {
    root = r;
    this->ww = ww;
  }
  int dfs(int node, int root) {
    int mx = 0;
    lca[ww][0][node] = root;
    for (__typeof(v[node].begin()) it = v[node].begin(); it != v[node].end();
         it++)
      if (it->first != root) {
        up[ww][it->first] = up[ww][node] + it->second;
        int xx = dfs(it->first, node) + it->second;
        if (xx >= mx) {
          w[ww][node] = it->first;
          mx = xx;
        }
      }
    mx_depth[ww][node] = mx;
    return mx;
  }
  void dfs2(int node, int root, int dis) {
    bool flag = false;
    wh[ww][node] = dis;
    for (__typeof(v[node].begin()) it = v[node].begin(); it != v[node].end();
         it++)
      if (it->first != root) {
        flag = true;
        if (w[ww][node] != it->first)
          dfs2(it->first, node, node);
        else
          dfs2(it->first, node, dis);
      }
    if (flag == false)
      leafs.push_back(make_pair(up[ww][node] - up[ww][dis], node));
  }
  void dfs3(int node, int root) {
    if (!jonsnow[ww][node]) jonsnow[ww][node] = inf;
    for (__typeof(v[node].begin()) it = v[node].begin(); it != v[node].end();
         it++)
      if (it->first != root) {
        dfs3(it->first, node);
        jonsnow[ww][node] = min(jonsnow[ww][it->first], jonsnow[ww][node]);
      }
  }
  void init() {
    dfs(root, 0);
    for (int i = 1; i <= logN; i++)
      for (int j = 1; j <= n; j++)
        lca[ww][i][j] = lca[ww][i - 1][lca[ww][i - 1][j]];
    dfs2(root, 0, 0);
    leafs.push_back(make_pair(2 * inf, 2 * inf));
    sort(leafs.begin(), leafs.end(), greater<pair<int, int> >());
    for (int i = 1; i <= (int)leafs.size() - 1; i++)
      jonsnow[ww][leafs[i].second] = i,
      pre[ww][i] = pre[ww][i - 1] + leafs[i].first;
    dfs3(root, 0);
  }
  int query(int node, int need) {
    if (leafs.size() - 1 <= need) return pre[ww][leafs.size() - 1];
    if (jonsnow[ww][node] <= need) return pre[ww][need];
    int u = node, ans = pre[ww][need];
    for (int i = logN; i >= 0; i--)
      if (lca[ww][i][u] && jonsnow[ww][lca[ww][i][u]] > need) u = lca[ww][i][u];
    u = lca[ww][0][u];
    ans += mx_depth[ww][node] + up[ww][node] - up[ww][u];
    if (!u) return ans - leafs[need].first;
    ans -= min(leafs[need].first, mx_depth[ww][u]);
    return ans;
  }
};
void lp(int node, int root, int dist) {
  if (dist >= mx) {
    mx = dist;
    a = node;
  }
  for (__typeof(v[node].begin()) it = v[node].begin(); it != v[node].end();
       it++)
    if (it->first != root) lp(it->first, node, dist + it->second);
}
int main() {
  scanf("%d %d", &n, &q);
  for (int i = 2; i <= n; i++) {
    scanf("%d %d %d", &x, &y, &z);
    v[x].push_back(make_pair(y, z));
    v[y].push_back(make_pair(x, z));
  }
  lp(1, 0, 0);
  b = a;
  lp(a, 0, 0);
  tree A(a, 0), B(b, 1);
  A.init(), B.init();
  int last = 0;
  for (int i = 1; i <= q; i++) {
    scanf("%d %d", &x, &y);
    if (i > 1) x = (x + last - 1) % n + 1, y = (y + last - 1) % n + 1;
    last = max(A.query(x, y * 2 - 1), B.query(x, y * 2 - 1));
    printf("%d\n", last);
  }
  return 0;
}
