#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long mod) {
  long long res = 1;
  while (y) {
    if (y & 1) res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
const int nn = 200010;
vector<pair<int, int> > e[nn];
int N, Q;
int ww[nn];
bool cmp(int x, int y) { return ww[x] > ww[y]; }
struct tree {
  int rt, cnt;
  int h[nn], fu[nn], oo[nn], S[nn], p[nn], w[nn];
  int d[nn][21];
  void dfs(int x, int ft) {
    fu[x] = x;
    for (int i = 0; i <= ((int)e[x].size()) - 1; ++i) {
      int j = e[x][i].first;
      if (j == ft) continue;
      h[j] = h[x] + e[x][i].second;
      d[j][0] = x;
      dfs(j, x);
      if (h[fu[x]] < h[fu[j]]) fu[x] = fu[j];
    }
  }
  void sou(int x, int tp, int fa) {
    w[x] = h[x] - h[tp];
    for (int i = 0; i <= ((int)e[x].size()) - 1; ++i) {
      int j = e[x][i].first;
      int k = e[x][i].second;
      if (j == fa) continue;
      if (fu[j] == fu[x])
        sou(j, tp, x);
      else
        sou(j, x, x);
    }
  }
  void init(int _rt) {
    rt = _rt;
    dfs(rt, 0);
    for (int j = 1; j <= 20; ++j)
      for (int i = 1; i <= N; ++i) d[i][j] = d[d[i][j - 1]][j - 1];
    sou(rt, rt, 0);
    cnt = 0;
    for (int i = 1; i <= N; ++i)
      if (((int)e[i].size()) == 1) p[++cnt] = i;
    for (int i = 1; i <= N; ++i) ww[i] = w[i];
    sort(p + 1, p + 1 + cnt, cmp);
    for (int i = 1; i <= cnt; ++i) S[i] = S[i - 1] + w[p[i]], oo[p[i]] = i;
  }
  int query(int x, int y) {
    y = y * 2 - 1;
    if (y > cnt) y = cnt;
    int res = S[y];
    x = fu[x];
    if (oo[x] <= y) return res;
    int xx = x;
    for (int i = 20; i >= 0; --i)
      if (d[xx][i] && oo[fu[d[xx][i]]] > y) xx = d[xx][i];
    xx = d[xx][0];
    int res1 = res + h[x] - h[xx] - w[p[y]];
    int res2 = res + h[x] - h[xx] - (h[fu[xx]] - h[xx]);
    return max(res1, res2);
  }
} A, B;
int L[nn], h[nn];
void Dfs(int x, int ft) {
  L[x] = x;
  for (int i = 0; i <= ((int)e[x].size()) - 1; ++i) {
    int j = e[x][i].first, k = e[x][i].second;
    if (j == ft) continue;
    h[j] = h[x] + k;
    Dfs(j, x);
    if (h[L[x]] < h[L[j]]) L[x] = L[j];
  }
}
int main() {
  scanf("%d%d", &N, &Q);
  for (int i = 1; i <= N - 1; ++i) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    e[x].push_back(make_pair(y, z));
    e[y].push_back(make_pair(x, z));
  }
  Dfs(1, 0);
  A.init(L[1]);
  int xx = L[1];
  Dfs(xx, 0);
  B.init(L[xx]);
  int yy = L[xx];
  int last = 0;
  while (Q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    x = (x + last - 1) % N + 1;
    y = (y + last - 1) % N + 1;
    printf("%d\n", last = max(A.query(x, y), B.query(x, y)));
  }
  return 0;
}
