#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "\n";
  err(++it, args...);
}
function<void(void)> ____ = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
};
template <typename T>
vector<T> &operator<<(vector<T> &__container, T x) {
  __container.push_back(x);
  return __container;
}
template <typename T>
ostream &operator<<(ostream &out, vector<T> &__container) {
  for (T _ : __container) out << _ << ' ';
  return out;
}
void sci() {}
template <typename... Args>
void sci(int &x, Args &...args) {
  scanf("%d", &x);
  sci(args...);
}
void scl() {}
template <typename... Args>
void scl(long long int &x, Args &...args) {
  scanf("%lld", &x);
  scl(args...);
}
const int MAXN = 2e5 + 7;
int n, q, dep[MAXN];
vector<pair<int, int> > G[MAXN];
void findroot(int u, int fa) {
  for (auto &e : G[u]) {
    int v = e.first, w = e.second;
    if (v == fa) continue;
    dep[v] = dep[u] + w;
    findroot(v, u);
  }
}
struct Solver {
  int root, bel[MAXN], maxd[MAXN], par[MAXN][20], son[MAXN], top[MAXN],
      depth[MAXN];
  vector<pair<int, int> > vec;
  vector<int> pre;
  void dfs(int u, int fa) {
    for (int i = 1; par[u][i - 1]; i++) par[u][i] = par[par[u][i - 1]][i - 1];
    for (auto &e : G[u]) {
      int v = e.first, w = e.second;
      if (v == fa) continue;
      depth[v] = maxd[v] = depth[u] + w;
      par[v][0] = u;
      dfs(v, u);
      if (maxd[v] > maxd[u]) son[u] = v, maxd[u] = maxd[v];
    }
  }
  void split(int u, int tp) {
    top[u] = tp;
    if (son[u]) split(son[u], tp);
    for (auto &e : G[u]) {
      int v = e.first;
      if (v == par[u][0] or v == son[u]) continue;
      split(v, v);
    }
  }
  void build() {
    dfs(root, 0);
    split(root, root);
    for (int i = 1; i <= n; i++)
      if (top[i] == i) vec.push_back(make_pair(maxd[i] - depth[par[i][0]], i));
    sort(vec.begin(), vec.end(),
         [&](const pair<int, int> &A, const pair<int, int> &B) {
           return A.first > B.first;
         });
    for (int i = 0; i < (int)vec.size(); i++) {
      int x = vec[i].second;
      while (x) {
        bel[x] = i + 1;
        x = son[x];
      }
    }
    if (vec.empty()) return;
    pre.resize(vec.size());
    pre[0] = vec[0].first;
    for (int i = 1; i < (int)vec.size(); i++)
      pre[i] = pre[i - 1] + vec[i].first;
  }
  int query(int x, int y) {
    int num = y * 2 - 1;
    if (num >= vec.size()) num = vec.size();
    int ret = num == 0 ? 0 : pre[num - 1];
    if (bel[x] <= num) return ret;
    int jp = x;
    for (int i = 19; ~i; i--)
      if (bel[par[jp][i]] > num) jp = par[jp][i];
    jp = par[jp][0];
    return ret + maxd[x] - depth[jp] -
           min(vec[num - 1].first, maxd[jp] - depth[jp]);
  }
} solver[2];
void solve() {
  sci(n, q);
  int tot = 0;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    sci(u, v, w);
    G[u] << pair<int, int>(v, w);
    G[v] << pair<int, int>(u, w);
    tot += w;
  }
  findroot(1, 0);
  solver[0].root = solver[1].root = 0;
  for (int i = 1; i <= n; i++)
    if (dep[i] > dep[solver[0].root]) solver[0].root = i;
  dep[solver[0].root] = 0;
  findroot(solver[0].root, 0);
  for (int i = 1; i <= n; i++)
    if (dep[i] > dep[solver[1].root]) solver[1].root = i;
  for (int i = 0; i < 2; i++) solver[i].build();
  int lastans = 0;
  while (q--) {
    int x, y;
    sci(x, y);
    x = (x + lastans - 1) % n + 1;
    y = (y + lastans - 1) % n + 1;
    cout << (lastans = max(solver[0].query(x, y), solver[1].query(x, y)))
         << "\n";
  }
}
int main() {
  solve();
  return 0;
}
