Language: C++

#include<algorithm>
#include<cmath>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<sstream>
#include<unordered_map>
#include<unordered_set>
#include<vector>
#include<cstdio>
using uint = unsigned int;
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#include"debug_deque.hpp"
#define vector DebugDeque
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(prints) FUNC(printe) FUNC(printw) FUNC(printew) FUNC(printb) FUNC(printd) FUNC(printde);
#endif

struct Graph{
    struct Edge{
        int cost, from, dest;
        Edge(int cost, int dest): cost(cost), from(-1), dest(dest){ }
        Edge(int cost, int from, int dest): cost(cost), from(from), dest(dest){ }
        ostream& operator >> (ostream& out){
            return out << '(' << cost << ':' << from << ' ' << dest << ')';
        }
    };
    struct Node{
        vector<Edge> edges;
    };
    vector<Node> nodes;
    Graph(int num): nodes(num){ }
    void addEdge(int from, int dest, int cost = 1){
        nodes[from].edges.emplace_back(cost, dest);
    }
    void addEdge2(int from, int dest, int cost = 1){
        addEdge(from, dest, cost);
        addEdge(dest, from, cost);
    }
    ll dijkstra2(int start, vector<ll> &lens){
        struct DElem{
            ll cost, dest;
            bool operator < (const DElem& e) const { return cost > e.cost; }
        };
        priority_queue<DElem> pq;
        pq.push({0, start});
        while(pq.size()){
            auto e = pq.top(); pq.pop();
            if(lens[e.dest] <= e.cost){ continue; }
            lens[e.dest] = e.cost;
            for(auto& edge : nodes[e.dest].edges){
                if(lens[edge.dest] > e.cost + edge.cost){
                    pq.push({e.cost + edge.cost, edge.dest});
                }
            }
        }
        return -1;
    }
};

char buf[1 << 22];

int main(){
    // cin.tie(0);
    // ios::sync_with_stdio(false);
    // cin.read(buf, sizeof(buf) - 1);
    read(STDIN_FILENO, buf, sizeof(buf) - 1);
    int idx = 0;
    auto next = [&](){
        int ret = 0;
        while(!isdigit(buf[idx])){ idx++; }
        while(isdigit(buf[idx])){ ret = ret * 10 + buf[idx++] - '0'; }
        return ret;
    };
    int n = 0;
    while(isdigit(buf[idx])){
        n = n * 10 + buf[idx++] - '0';
    }
    Graph g(n);
    for(int i = 0; i < n - 1; i++){
        int a = next(), b = next(), c = next();
        a--; b--;
        g.addEdge2(a, b, c);
    }
    int q = next(), k = next();
    k--;
    vector<ll> lens(n, MLL);
    g.dijkstra2(k, lens);
    prints(lens);
    for(int i = 0; i < q; i++){
        int x = next(), y = next();
        x--; y--;
        cout << lens[x] + lens[y] << '\n';
    }

    // int n; cin >> n;
    // Graph g(n);
    // for(int i = 0; i < n - 1; i++){
    //     int a, b, c;
    //     cin >> a >> b >> c;
    //     a--; b--;
    //     g.addEdge2(a, b, c);
    // }
    // int q, k;
    // cin >> q >> k;
    // k--;
    // vector<ll> lens(n, MLL);
    // g.dijkstra2(k, lens);
    // prints(lens);
    // for(int i = 0; i < q; i++){
    //     int x, y;
    //     cin >> x >> y;
    //     x--; y--;
    //     cout << lens[x] + lens[y] << '\n';
    // }
}


Compiler output:
a.cc: In function 'int main()':
a.cc:76:10: error: 'STDIN_FILENO' was not declared in this scope
   76 |     read(STDIN_FILENO, buf, sizeof(buf) - 1);
      |          ^~~~~~~~~~~~
a.cc:76:5: error: 'read' was not declared in this scope; did you mean 'fread'?
   76 |     read(STDIN_FILENO, buf, sizeof(buf) - 1);
      |     ^~~~
      |     fread