#include <bits/stdc++.h>
using namespace std;
template <typename T>
T MAX(T a, T b) {
  return (a > b) ? a : b;
}
template <typename T>
T MIN(T a, T b) {
  return (a < b) ? a : b;
}
template <typename T>
void UMAX(T &a, T b) {
  a = (a > b ? a : b);
}
template <typename T>
void UMIN(T &a, T b) {
  a = (a < b ? a : b);
}
int readint() {
  char c = getchar();
  int ret = 0;
  while (!(c >= '0' && c <= '9')) c = getchar();
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return ret;
}
void putint(int v) {
  if (!v) {
    putchar('0');
    return;
  }
  if (v >= 10) putint(v / 10);
  putchar('0' + (v % 10));
}
int n, q;
vector<pair<int, int> > adj[100005];
pair<int, int> getfar(int v, int f, int d) {
  int i;
  pair<int, int> ret = make_pair(d, v);
  for (i = 0; i < (int)adj[v].size(); ++i) {
    int u = adj[v][i].first, w = adj[v][i].second;
    if (u != f) UMAX(ret, getfar(u, v, d + w));
  }
  return ret;
}
int hl[100005];
bool cmp(int a, int b) { return hl[a] > hl[b]; }
class calc {
 public:
  int dep[100005], fa[20][100005], hei[100005], mxson[100005];
  void dfs(int v, int f, int d) {
    int i;
    dep[v] = d;
    fa[0][v] = f;
    hei[v] = 0;
    mxson[v] = -1;
    for (i = 0; i < (int)adj[v].size(); ++i) {
      int u = adj[v][i].first, w = adj[v][i].second;
      if (u != f) {
        dfs(u, v, d + w);
        if (hei[u] + w > hei[v]) {
          hei[v] = hei[u] + w;
          mxson[v] = u;
        }
      }
    }
  }
  int per[100005], ndc, ps[100005], covt[100005];
  void rdfs(int v, int f, int l) {
    int i;
    hl[v] = -1;
    if (mxson[v] != -1)
      rdfs(mxson[v], v, l);
    else {
      hl[v] = l;
      ++ndc;
    }
    for (i = 0; i < (int)adj[v].size(); ++i) {
      int u = adj[v][i].first, w = adj[v][i].second;
      if (u != f && u != mxson[v]) rdfs(u, v, hei[u] + w);
    }
  }
  int findlast(int s, int t) {
    if (covt[s] <= t) return s;
    int i;
    for (i = 19; i >= 0; --i) {
      if (fa[i][s] != -1 && covt[fa[i][s]] > t) s = fa[i][s];
    }
    return fa[0][s];
  }
  void build(int c) {
    int i, j, k;
    dfs(c, -1, 0);
    for (i = 1; i < 20; ++i) {
      for (j = 0; j < n; ++j) {
        if (fa[i - 1][j] == -1)
          fa[i][j] = -1;
        else
          fa[i][j] = fa[i - 1][fa[i - 1][j]];
      }
    }
    rdfs(c, -1, hei[c]);
    for (i = 0; i < n; ++i) per[i] = i;
    sort(per, per + n, cmp);
    for (i = 0; i < ndc; ++i) ps[i] = hl[per[i]] + (i ? ps[i - 1] : 0);
    for (i = 0; i < ndc; ++i) {
      for (j = per[i]; j != -1; j = fa[0][j]) {
        covt[j] = i;
        if (fa[0][j] == -1 || mxson[fa[0][j]] != j) break;
      }
    }
  }
  int qry(int x, int y) {
    if (y >= ndc) return ps[ndc - 1];
    if (covt[x] < y) return ps[y - 1];
    if (y == 1) return dep[x] + hei[x];
    int ls1 = findlast(x, y - 2), sol1 = ps[y - 2] + dep[x] + hei[x] - dep[ls1];
    int ls2 = findlast(x, y - 1), gt = per[covt[ls2]],
        sol2 = ps[y - 1] - dep[gt] + dep[x] + hei[x];
    return max(sol1, sol2);
  }
} S, T;
int main() {
  int i, j, k;
  scanf("%d%d", &n, &q);
  for (i = 0; i < n - 1; ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    --u;
    --v;
    adj[u].push_back(make_pair(v, w));
    adj[v].push_back(make_pair(u, w));
  }
  int s = getfar(0, -1, 0).second, t = getfar(s, -1, 0).second;
  S.build(s);
  T.build(t);
  int lans = 0;
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    x = (x + lans - 1) % n;
    y = (y + lans - 1) % n + 1;
    y = y * 2 - 1;
    printf("%d\n", lans = max(S.qry(x, y), T.qry(x, y)));
  }
  return 0;
}
