#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse3,sse4,popcnt,avx,mmx,abm,tune=native")
using namespace std;
namespace output {
void __(short x) { cout << x; }
void __(unsigned x) { cout << x; }
void __(int x) { cout << x; }
void __(long long x) { cout << x; }
void __(unsigned long long x) { cout << x; }
void __(double x) { cout << x; }
void __(long double x) { cout << x; }
void __(char x) { cout << x; }
void __(const char* x) { cout << x; }
void __(const string& x) { cout << x; }
void __(bool x) { cout << (x ? "true" : "false"); }
template <class S, class T>
void __(const pair<S, T>& x) {
  __(1 ? "(" : ""), __(x.first), __(1 ? ", " : " "), __(x.second),
      __(1 ? ")" : "");
}
template <class T>
void __(const vector<T>& x) {
  __(1 ? "{" : "");
  bool _ = 0;
  for (const auto& v : x) __(_ ? 1 ? ", " : " " : ""), __(v), _ = 1;
  __(1 ? "}" : "");
}
template <class T>
void __(const set<T>& x) {
  __(1 ? "{" : "");
  bool _ = 0;
  for (const auto& v : x) __(_ ? 1 ? ", " : " " : ""), __(v), _ = 1;
  __(1 ? "}" : "");
}
template <class T>
void __(const multiset<T>& x) {
  __(1 ? "{" : "");
  bool _ = 0;
  for (const auto& v : x) __(_ ? 1 ? ", " : " " : ""), __(v), _ = 1;
  __(1 ? "}" : "");
}
template <class S, class T>
void __(const map<S, T>& x) {
  __(1 ? "{" : "");
  bool _ = 0;
  for (const auto& v : x) __(_ ? 1 ? ", " : " " : ""), __(v), _ = 1;
  __(1 ? "}" : "");
}
void pr() { cout << "\n"; }
template <class S, class... T>
void pr(const S& a, const T&... b) {
  __(a);
  if (sizeof...(b)) __(' ');
  pr(b...);
}
}  // namespace output
using namespace output;
const int MN = 1e5 + 5, LG = 20;
int N, Q, i, j, x, y, w, sp[LG][MN], dep[MN], rt, par[MN], vs[MN], ans[MN],
    sna[MN], t[MN], vis[MN][2], dis[MN], nxt;
long long lst;
struct pq {
  bool operator()(const pair<int, int>& i, const pair<int, int>& j) {
    return i.second > j.second;
  }
};
priority_queue<pair<int, int>, vector<pair<int, int> >, pq> q;
vector<pair<int, int> > adj[MN], go;
pair<int, int> a[MN], b[MN], pref[MN];
void dfs(int n, int p, int d) {
  dep[n] = d;
  par[n] = p;
  sp[0][n] = p;
  vis[n][0] = ++nxt;
  for (auto v : adj[n]) {
    if (v.first == p) continue;
    dis[v.first] = dis[n] + v.second;
    dfs(v.first, n, d + 1);
    pair<int, int> tmp = {a[v.first].first + v.second, v.first};
    if (tmp.first > a[n].first)
      b[n] = a[n], a[n] = tmp;
    else if (tmp.first > b[n].first)
      b[n] = tmp;
  }
  vis[n][1] = nxt;
}
void dfs2(int n, int up) {
  ans[n] = a[n].first + max(up, b[n].first);
  vector<pair<int, int> > sub;
  for (auto v : adj[n]) {
    if (v.first == par[n]) continue;
    if (v.first == a[n].second)
      dfs2(v.first, max(up, b[n].first) + v.second);
    else
      dfs2(v.first, max(up, a[n].first) + v.second);
    sub.push_back({pref[v.first].first + v.second, pref[v.first].second});
  }
  sort(sub.begin(), sub.end(),
       [](pair<int, int> i, pair<int, int> j) { return i.first > j.first; });
  if (sub.size()) {
    for (int i = 1; i < (int)sub.size(); i++) go.push_back(sub[i]);
    pref[n] = sub[0];
  } else
    pref[n] = {0, n};
}
inline void mrk(int x, int cur) {
  while (!t[x]) {
    t[x] = cur;
    x = par[x];
  }
}
inline bool con(int x, int y) {
  return vis[x][0] <= vis[y][0] && vis[x][1] >= vis[y][1];
}
int main() {
  scanf("%d%d", &N, &Q);
  for (i = 1; i < N; i++) {
    scanf("%d%d%d", &x, &y, &w);
    adj[x].push_back({y, w});
    adj[y].push_back({x, w});
  }
  q.push({1, 0});
  while (q.size()) {
    pair<int, int> cur = q.top();
    q.pop();
    if (vs[cur.first]) continue;
    vs[cur.first] = 1;
    rt = cur.first;
    for (auto v : adj[cur.first]) {
      if (vs[v.first]) continue;
      q.push({v.first, v.second + cur.second});
    }
  }
  dfs(rt, 0, 0);
  dfs2(rt, 0);
  for (j = 1; j < LG; j++) {
    for (i = 1; i <= N; i++) sp[j][i] = sp[j - 1][sp[j - 1][i]];
  }
  go.push_back(pref[rt]);
  int cur = 0;
  sort(go.begin(), go.end(),
       [](pair<int, int> i, pair<int, int> j) { return i.first > j.first; });
  int sub;
  for (auto v : adj[rt]) {
    if (con(v.first, go[0].second)) {
      sub = v.first;
      break;
    }
  }
  bool ok = 0;
  for (i = 1; i < (int)go.size(); i++) {
    if (!con(sub, go[i].second)) {
      pair<int, int> cur = go[i];
      ok = 1;
      go.erase(go.begin() + i);
      go.insert(go.begin() + 1, cur);
      break;
    }
  }
  if (!ok) go.insert(go.begin() + 1, make_pair(0, 0));
  for (i = 1; i <= N; i++) {
    sna[i] = sna[i - 1];
    for (j = 0; j < 2; j++) {
      if (cur < go.size()) {
        sna[i] += go[cur].first;
        mrk(go[cur++].second, i);
      }
    }
  }
  for (; Q; Q--) {
    scanf("%d%d", &x, &y);
    x = (x + lst - 1) % N + 1;
    y = (y + lst - 1) % N + 1;
    if (y == 1)
      lst = ans[x];
    else {
      if (t[x] <= y)
        lst = sna[y];
      else {
        int c = x;
        for (i = LG - 1; i >= 0; i--) {
          if (t[sp[i][c]] > y) c = sp[i][c];
        }
        c = sp[0][c];
        lst = sna[y] - min(go[2 * y - 1].first, min(pref[c].first, dis[c])) +
              a[x].first + dis[x] - dis[c];
      }
    }
    printf("%lld\n", lst);
  }
  return 0;
}
