#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii pair<pii, int>
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define f first
#define s second
#define oo 1000000001
#define eb emplace_back
#define pb push_back
#define mpr make_pair
#define size(v) (int)v.size()
#define ln '\n'
#define ull unsigned long long
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct edge {
int x, y, v, c, s;
int other(int node) {
return (x == node ? y : x);
}
};
const int lg = 18;
const int up = 1e5 + 5;
vi g[up];
int val[up], nval[up], cost[up];
int parent[lg][up], stMin[lg][up];
int level[up];
edge e[up];
void dfs(int node, int par) {
level[node] = level[par] + 1;
parent[0][node] = par;
stMin[0][node] = nval[node];
for (int i = 1; i < lg; ++i) {
parent[i][node] = parent[i - 1][parent[i - 1][node]];
stMin[i][node] = min(stMin[i - 1][node], stMin[i - 1][parent[i - 1][node]]);
}
for (int i : g[node]) {
int to = e[i].other(node);
if (to != par) {
val[to] = e[i].v;
nval[to] = e[i].s;
cost[to] = e[i].c;
dfs(to, node);
}
}
}
int lca(int x, int y) {
if (level[x] < level[y]) {
swap(x, y);
}
for (int i = lg - 1; i >= 0; --i) {
if (level[x] - (1 << i) >= level[y]) {
x = parent[i][x];
}
}
if (x == y) return x;
for (int i = lg - 1; i >= 0; --i) {
if (parent[i][x] != parent[i][y]) {
x = parent[i][x];
y = parent[i][y];
}
}
return parent[0][x];
}
int getMin(int x, int y) {
if (level[x] < level[y]) {
swap(x, y);
}
int res = oo;
for (int i = lg - 1; i >= 0; --i) {
if (level[x] - (1 << i) >= level[y]) {
res = min(res, stMin[i][x]);
x = parent[i][x];
}
}
if (x != y) {
for (int i = lg - 1; i >= 0; --i) {
if (parent[i][x] != parent[i][y]) {
res = min(res, stMin[i][x]);
res = min(res, stMin[i][y]);
x = parent[i][x];
y = parent[i][y];
}
}
res = min(res, stMin[0][x]);
res = min(res, stMin[0][y]);
}
return res;
}
const int MAX = 3e6 + 6;
int nxt = 1;
int root[MAX], L[MAX], R[MAX];
ll tree[MAX];
pii pr[up];
int pos[up];
void build(int v, int l, int r) {
if (l != r) {
int mid = (l + r) >> 1;
L[v] = ++nxt;
R[v] = ++nxt;
build(L[v], l, mid);
build(R[v], mid + 1, r);
}
}
int update(int v, int l, int r, int _pos, int _cost) {
int idx = ++nxt;
if (l == r) {
tree[idx] = tree[v] + _cost;
return idx;
}
int mid = (l + r) >> 1;
L[idx] = L[v];
R[idx] = R[v];
if (_pos <= mid) {
L[idx] = update(L[idx], l, mid, _pos, _cost);
} else {
R[idx] = update(R[idx], mid + 1, r, _pos, _cost);
}
tree[idx] = tree[L[idx]] + tree[R[idx]];
return idx;
}
void dfs2(int node, int par, int n) {
root[node] = update(root[par], 1, n, pos[node], cost[node]);
for (int i : g[node]) {
int to = e[i].other(node);
if (to != par) {
dfs2(to, node, n);
}
}
}
int query(int _a, int _b, int _lca, int l, int r, ll e) {
if (l == r) {
return l;
}
int mid = (l + r) >> 1;
ll _cost = tree[L[_a]] + tree[L[_b]] - tree[L[_lca]] * 2;
if (_cost > e) {
return query(L[_a], L[_b], L[_lca], l, mid, e);
} else {
return query(R[_a], R[_b], R[_lca], mid + 1, r, e - _cost);
}
}
void solve() {
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
cin >> e[i].x >> e[i].y >> e[i].v >> e[i].c >> e[i].s;
g[e[i].x].eb(i);
g[e[i].y].eb(i);
}
val[1] = nval[1] = oo;
cost[1] = 0;
dfs(1, 1);
for (int i = 1; i <= n; ++i) {
pr[i].f = val[i];
pr[i].s = i;
}
sort(pr + 1, pr + n + 1);
for (int i = 1; i <= n; ++i) {
pos[pr[i].s] = i;
}
build(1, 1, n);
root[0] = 1;
dfs2(1, 0, n);
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
int _a, _b;
ll _e;
cin >> _a >> _b >> _e;
int _lca = lca(_a, _b);
int _pos = query(root[_a], root[_b], root[_lca], 1, n, _e);
int res = min(pr[_pos].f, getMin(_a, _b));
cout << res << ln;
}
}
int main() {
ios_base::sync_with_stdio(false);
solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgcGlpIHBhaXI8aW50LCBpbnQ+CiNkZWZpbmUgcGlpaSBwYWlyPHBpaSwgaW50PgojZGVmaW5lIHZpIHZlY3RvcjxpbnQ+CiNkZWZpbmUgdmxsIHZlY3RvcjxsbD4KI2RlZmluZSB2dmkgdmVjdG9yPHZpPgojZGVmaW5lIHZwaWkgdmVjdG9yPHBpaT4KI2RlZmluZSB2dnBpaSB2ZWN0b3I8dnBpaT4KI2RlZmluZSBmIGZpcnN0CiNkZWZpbmUgcyBzZWNvbmQKI2RlZmluZSBvbyAxMDAwMDAwMDAxCiNkZWZpbmUgZWIgZW1wbGFjZV9iYWNrCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbXByIG1ha2VfcGFpcgojZGVmaW5lIHNpemUodikgKGludCl2LnNpemUoKQojZGVmaW5lIGxuICdcbicKI2RlZmluZSB1bGwgdW5zaWduZWQgbG9uZyBsb25nCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgYWxsKHYpIHYuYmVnaW4oKSwgdi5lbmQoKQoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCm10MTk5Mzcgcm5nKGNocm9ubzo6c3RlYWR5X2Nsb2NrOjpub3coKS50aW1lX3NpbmNlX2Vwb2NoKCkuY291bnQoKSk7CgpzdHJ1Y3QgZWRnZSB7CiAgaW50IHgsIHksIHYsIGMsIHM7CiAgaW50IG90aGVyKGludCBub2RlKSB7CiAgICByZXR1cm4gKHggPT0gbm9kZSA/IHkgOiB4KTsKICB9Cn07Cgpjb25zdCBpbnQgbGcgPSAxODsKY29uc3QgaW50IHVwID0gMWU1ICsgNTsKdmkgZ1t1cF07CmludCB2YWxbdXBdLCBudmFsW3VwXSwgY29zdFt1cF07CmludCBwYX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