Language: C++

#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
using PP = pair<int, long long>;

int N, Q, K;
vector<int> G[100000];
long long dist[100000];


long long calc(int l, int r) {
  return dist[l] + dist[r];
}

void dfs(int i, int prev) {
  for(PP p : G[i]) {
    int to = p.first;
    if(to == prev) continue;
    dist[to] = dist[i] + p.second;
    dfs(to, i);
  }
}

void solve() {
  fill(dist, dist + N, 1ll << 62);
  dist[K] = 0;
  dfs(K, -1);
}

int main() {
  cin >> N;
  for(int i = 0; i < N - 1; ++i) {
    int a,b,c;
    cin >> a >> b >> c;
    --a; --b;
    G[a].push_back(PP(b, c));
    G[b].push_back(PP(a, c));
  }
  
  cin >> Q >> K;
  --K;
  solve();
  for(int i = 0; i < Q; ++i) {
    int l, r;
    cin >> l >> r;
    --l; --r;
    cout << calc(l, r) << endl;  
  }
  return 0;
}


Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:18:17: error: conversion from 'int' to non-scalar type 'PP' {aka 'std::pair<int, long long int>'} requested
   18 |   for(PP p : G[i]) {
      |                 ^
a.cc: In function 'int main()':
a.cc:38:19: error: no matching function for call to 'std::vector<int>::push_back(PP)'
   38 |     G[a].push_back(PP(b, c));
      |     ~~~~~~~~~~~~~~^~~~~~~~~~
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator<int>; value_type = int]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:35: note:   no known conversion for argument 1 from 'PP' {aka 'std::pair<int, long long int>'} to 'const std::vector<int>::value_type&' {aka 'const int&'}
 1283 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = int; _Alloc = std::allocator<int>; value_type = int]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:30: note:   no known conversion for argument 1 from 'PP' {aka 'std::pair<int, long long int>'} to 'std::vector<int>::value_type&&' {aka 'int&&'}
 1300 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~
a.cc:39:19: error: no matching function for call to 'std::vector<int>::push_back(PP)'
   39 |     G[b].push_back(PP(a, c));
      |     ~~~~~~~~~~~~~~^~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator<int>; value_type = int]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:35: note:   no known conversion for argument 1 from 'PP' {aka 'std::pair<int, long long int>'} to 'const std::vector<int>::value_type&' {aka 'const int&'}
 1283 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = int; _Alloc = std::allocator<int>; value_type = int]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:30: note:   no known conversion for argument 1 from 'PP' {aka 'std::pair<int, long long int>'} to 'std::vector<int>::value_type&&' {aka 'int&&'}
 1300 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~