#include <bits/stdc++.h>
using namespace std;
const int N = 100010, K = 17;
struct edge {
  int nxt, t, s;
} e[N << 1];
int head[N], edge_cnt;
void add_edge(int x, int y, int z) {
  e[edge_cnt] = (edge){head[x], y, z};
  head[x] = edge_cnt++;
}
struct Tree_For_Two_Root {
  struct node {
    int id, val;
    bool operator<(const node &_) const { return val > _.val; }
  } B[N];
  int h;
  int fa[N][K], dis[N], Mx[N], bot[N], son[N], Tim[N], Sum[N];
  void dfs_pre(int x, int f) {
    Mx[x] = 0;
    bot[x] = x;
    fa[x][0] = f;
    int i;
    for (i = 1; i < K; i++) {
      fa[x][i] = fa[fa[x][i - 1]][i - 1];
    }
    for (i = head[x]; ~i; i = e[i].nxt) {
      int to = e[i].t, val = e[i].s;
      if (to == f) {
        continue;
      }
      dis[to] = dis[x] + val;
      dfs_pre(to, x);
      if (Mx[to] + val > Mx[x]) {
        Mx[x] = Mx[to] + val;
        bot[x] = bot[to];
        son[x] = to;
      }
    }
    if (son[x] == 0) {
      return;
    }
    for (i = head[x]; ~i; i = e[i].nxt) {
      int to = e[i].t, val = e[i].s;
      if (to == f || to == son[x]) {
        continue;
      }
      B[++h] = (node){to, Mx[to] + val};
    }
  }
  void dfs_get_tim(int x) {
    if (son[x] != 0) {
      Tim[son[x]] = Tim[x];
    }
    int i;
    for (i = head[x]; ~i; i = e[i].nxt) {
      int to = e[i].t;
      if (to == fa[x][0]) {
        continue;
      }
      dfs_get_tim(to);
    }
  }
  void init(int rt) {
    dfs_pre(rt, 0);
    B[++h] = (node){rt, Mx[rt]};
    sort(B + 1, B + 1 + h);
    int i;
    for (i = 1; i <= h; i++) {
      Sum[i] = Sum[i - 1] + B[i].val;
      Tim[B[i].id] = i;
    }
    dfs_get_tim(rt);
  }
  int Query(int x, int y) {
    y = 2 * y - 1;
    if (y >= h) {
      return Sum[h];
    }
    if (Tim[x] <= y) {
      return Sum[y];
    }
    int t = x, i;
    for (i = K - 1; i >= 0; i--) {
      if (fa[t][i] != 0 && Tim[fa[t][i]] > y) {
        t = fa[t][i];
      }
    }
    t = fa[t][0];
    return Sum[y] - min(B[y].val, dis[bot[t]] - dis[t]) + dis[bot[x]] - dis[t];
  }
} Tree[2];
namespace init {
int Mx, rt;
void dfs(int x, int f, int d) {
  if (d > Mx) {
    Mx = d;
    rt = x;
  }
  int i;
  for (i = head[x]; ~i; i = e[i].nxt) {
    int to = e[i].t, val = e[i].s;
    if (to == f) {
      continue;
    }
    dfs(to, x, d + val);
  }
}
void Work() {
  Mx = -1;
  dfs(1, 0, 0);
  Tree[0].init(rt);
  Mx = -1;
  dfs(rt, 0, 0);
  Tree[1].init(rt);
}
}  // namespace init
int main() {
  int n, m, i;
  scanf("%d%d", &n, &m);
  memset(head + 1, -1, sizeof(int) * 1 * n);
  for (i = 1; i < n; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    add_edge(x, y, z);
    add_edge(y, x, z);
  }
  init::Work();
  int LasAns = 0;
  for (i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x = (x + LasAns - 1) % n + 1;
    y = (y + LasAns - 1) % n + 1;
    printf("%d\n", LasAns = max(Tree[0].Query(x, y), Tree[1].Query(x, y)));
  }
  return 0;
}
