#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool checkMax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T, typename... Args>
inline void checkMax(T &a, const Args... arg) {
  checkMax(a, max(arg...));
}
template <class T>
inline bool checkMin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T, typename... Args>
inline void checkMin(T &a, const Args... arg) {
  checkMin(a, min(arg...));
}
const int INF = 0x3f3f3f3f;
const long long llINF = 1e18;
const int MAXN = 1e5 + 5;
const int LOG = 17;
int n, Q, len, lastans;
int head[MAXN];
struct Edge {
  int to, next, w;
} e[MAXN << 1];
void add_edge(int u, int v, int w) {
  e[++len] = (Edge){v, head[u], w};
  head[u] = len;
}
struct Tree {
  int rt, tot;
  int dep[MAXN], son[MAXN], maxDep[MAXN], f[MAXN][LOG], top[MAXN], len[MAXN],
      belong[MAXN], sum[MAXN];
  vector<int> vec;
  void dfs1(int u, int fa) {
    if (dep[u] > dep[rt]) rt = u;
    for (int i = head[u]; i != -1; i = e[i].next) {
      int v = e[i].to;
      if (v == fa) continue;
      dep[v] = dep[u] + e[i].w;
      dfs1(v, u);
    }
  }
  void dfs2(int u, int fa) {
    f[u][0] = fa;
    for (int i = 1; i < LOG; i++) f[u][i] = f[f[u][i - 1]][i - 1];
    maxDep[u] = dep[u];
    for (int i = head[u]; i != -1; i = e[i].next) {
      int v = e[i].to;
      if (v == fa) continue;
      dep[v] = dep[u] + e[i].w;
      dfs2(v, u);
      checkMax(maxDep[u], maxDep[v]);
      if (maxDep[v] > maxDep[son[u]]) son[u] = v;
    }
  }
  void dfs3(int u, int t) {
    top[u] = t;
    if (son[u]) dfs3(son[u], t);
    for (int i = head[u]; i != -1; i = e[i].next) {
      int v = e[i].to;
      if (v != f[u][0] && v != son[u]) dfs3(v, v);
    }
  }
  void init(int x) {
    dfs1(x, 0);
    memset(dep, 0, sizeof(dep));
    dfs2(rt, 0);
    dfs3(rt, rt);
    for (int i = 1; i <= n; i++) {
      if (top[i] == i) {
        vec.push_back(i);
        len[i] = maxDep[i] - dep[f[i][0]];
      }
    }
    tot = vec.size();
    sort(vec.begin(), vec.end(),
         [&](const int &x, const int &y) { return len[x] > len[y]; });
    for (int i = 1; i <= tot; i++) {
      sum[i] = sum[i - 1] + len[vec[i - 1]];
      for (int u = vec[i - 1]; u; u = son[u]) belong[u] = i;
    }
  }
  int jump(int x, int y) {
    for (int i = LOG - 1; i >= 0; i--) {
      if (belong[f[x][i]] > y) x = f[x][i];
    }
    return f[x][0];
  }
  int query(int x, int y) {
    y = 2 * y - 1;
    if (y >= tot) return sum[tot];
    if (belong[x] <= y) return sum[y];
    int u = jump(x, y);
    int v = jump(x, y - 1);
    return max(sum[y] + maxDep[x] - maxDep[u], sum[y - 1] + maxDep[x] - dep[v]);
  }
} T1, T2;
int main() {
  memset(head, -1, sizeof(head));
  scanf("%d%d", &n, &Q);
  for (int i = 1, u, v, w; i < n; i++) {
    scanf("%d%d%d", &u, &v, &w);
    add_edge(u, v, w);
    add_edge(v, u, w);
  }
  T1.init(1);
  T2.init(T1.rt);
  while (Q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    x = (x + lastans - 1) % n + 1;
    y = (y + lastans - 1) % n + 1;
    printf("%d\n", lastans = max(T1.query(x, y), T2.query(x, y)));
  }
  return 0;
}
