Language: C++

package contest;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		Integer N = Integer.parseInt(sc.next());
		Node v[] = new Node[N];
		int c[] = new int[N-1];
		for(int i=0;i<N;i++){
			v[i] = new Node(i);
		}
		Edge e[] = new Edge[N-1];
		for(int i=0;i<N-1;i++) {
			e[i] = new Edge(v[Integer.parseInt(sc.next())-1],v[Integer.parseInt(sc.next())-1],Integer.parseInt(sc.next()));
		}
		int Q = Integer.parseInt(sc.next());
		int K = Integer.parseInt(sc.next());
		int x[] = new int[Q];
		int y[] = new int[Q];
		for(int i=0;i<Q;i++) {
			x[i] = Integer.parseInt(sc.next());
			y[i] = Integer.parseInt(sc.next());
		}
		PriorityQueue<Node> searching = new PriorityQueue<>((u,w)->u.d>w.d?1:u.d<w.d?-1:0);
		PriorityQueue<Node> searched = new PriorityQueue<>((u,w)->u.d>w.d?1:u.d<w.d?-1:0);
		boolean used[] = new boolean[N];
		Arrays.fill(used, false);
		v[K-1].Reload_dist(0);
		used[K-1] = true;
		for(Edge connect:v[K-1].edges) {
			Node another = connect.Other_one(v[K-1]);
			another.Reload_dist(connect.cost);
			searching.add(another);
			used[another.id] = true;
		}
		searched.add(v[K-1]);

		Node vertex=null;
		while(searching.size()>0) {
			vertex = searching.poll();
			for(Edge connect:vertex.edges) {
				Node another = connect.Other_one(vertex);
				if(!used[another.id]) {
					another.Reload_dist(vertex.d + connect.cost);
					if(!searched.contains(another)){
						searching.add(another);
					}
					searched.add(vertex);
					used[another.id] = true;
				}
			}
		}
		long result = 0;
		for(int i=0;i<Q;i++) {
			result = v[x[i]-1].d + v[y[i]-1].d;
			out.println(result);
		}




		out.flush();
	}
}

class Node{
	int id;
	long d;
	Node prev = null;
	ArrayList<Edge> edges = new ArrayList<>();

	public Node(int id) {
		this.id = id;
		this.d = Integer.MAX_VALUE;
	}

	public void Reset() {
		this.d = Integer.MAX_VALUE;
		this.prev = null;
	}

	public void Reload_dist(long dist) {
		this.d = dist;
	}
//
//	public void Reload_prev(Node v) {
//		this.prev = v;
//	}

//	@Override public Node clone() {
//		return new Node(this.id);
//	}

	public void Connect(Edge e) {
		edges.add(e);
	}
}

class Edge{
	Node v1, v2;
	int cost;
	public Edge(Node id1,Node id2, int c) {
		this.v1 = id1;
		this.v2 = id2;
		this.cost = c;
		v1.Connect(this);
		v2.Connect(this);
	}

//	public Node First_one() {
//		return this.v1;
//	}

//	public Node Second_one() {
//		return this.v2;
//	}

	public Node Other_one(Node v) {
		if(v.equals(this.v1)) {
			return this.v2;
		}
		else {
			return this.v1;
		}
	}
}



Compiler output:
a.cc:1:1: error: 'package' does not name a type
    1 | package contest;
      | ^~~~~~~
a.cc:3:1: error: 'import' does not name a type
    3 | import java.io.PrintWriter;
      | ^~~~~~
a.cc:3:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:4:1: error: 'import' does not name a type
    4 | import java.util.ArrayList;
      | ^~~~~~
a.cc:4:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:5:1: error: 'import' does not name a type
    5 | import java.util.Arrays;
      | ^~~~~~
a.cc:5:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:6:1: error: 'import' does not name a type
    6 | import java.util.Comparator;
      | ^~~~~~
a.cc:6:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:7:1: error: 'import' does not name a type
    7 | import java.util.PriorityQueue;
      | ^~~~~~
a.cc:7:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:8:1: error: 'import' does not name a type
    8 | import java.util.Scanner;
      | ^~~~~~
a.cc:8:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:10:1: error: expected unqualified-id before 'public'
   10 | public class Main {
      | ^~~~~~
a.cc:78:14: error: field 'prev' has incomplete type 'Node'
   78 |         Node prev = null;
      |              ^~~~
a.cc:75:7: note: definition of 'class Node' is not complete until the closing brace
   75 | class Node{
      |       ^~~~
a.cc:79:9: error: 'ArrayList' does not name a type
   79 |         ArrayList<Edge> edges = new ArrayList<>();
      |         ^~~~~~~~~
a.cc:81:15: error: expected ':' before 'Node'
   81 |         public Node(int id) {
      |               ^~~~~
      |               :
a.cc:86:15: error: expected ':' before 'void'
   86 |         public void Reset() {
      |               ^~~~~
      |               :
a.cc:91:15: error: expected ':' before 'void'
   91 |         public void Reload_dist(long dist) {
      |               ^~~~~
      |               :
a.cc:103:15: error: expected ':' before 'void'
  103 |         public void Connect(Edge e) {
      |               ^~~~~
      |               :
a.cc:103:29: error: 'Edge' has not been declared
  103 |         public void Connect(Edge e) {
      |                             ^~~~
a.cc:106:2: error: expected ';' after class definition
  106 | }
      |  ^
      |  ;
a.cc: In constructor 'Node::Node(int)':
a.cc:82:22: error: request for member 'id' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
   82 |                 this.id = id;
      |                      ^~
a.cc:83:22: error: request for member 'd' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
   83 |                 this.d = Integer.MAX_VALUE;
      |                      ^
a.cc:83:26: error: 'Integer' was not declared in this scope
   83 |                 this.d = Integer.MAX_VALUE;
      |                          ^~~~~~~
a.cc: In member function 'void Node::Reset()':
a.cc:87:22: error: request for member 'd' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
   87 |                 this.d = Integer.MAX_VALUE;
      |                      ^
a.cc:87:26: error: 'Integer' was not declared in this scope
   87 |                 this.d = Integer.MAX_VALUE;
      |                          ^~~~~~~
a.cc:88:22: error: request for member 'prev' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
   88 |                 this.prev = null;
      |                      ^~~~
a.cc:88:29: error: 'null' was not declared in this scope
   88 |                 this.prev = null;
      |                             ^~~~
a.cc: In member function 'void Node::Reload_dist(long int)':
a.cc:92:22: error: request for member 'd' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
   92 |                 this.d = dist;
      |                      ^
a.cc: In member function 'void Node::Connect(int)':
a.cc:104:17: error: 'edges' was not declared in this scope
  104 |                 edges.add(e);
      |                 ^~~~~
a.cc: At global scope:
a.cc:111:15: error: expected ':' before 'Edge'
  111 |         public Edge(Node id1,Node id2, int c) {
      |               ^~~~~
      |               :
a.cc:127:15: error: expected ':' before 'Node'
  127 |         public Node Other_one(Node v) {
      |               ^~~~~
      |               :
a.cc:135:2: error: expected ';' after class definition
  135 | }
      |  ^
      |  ;
a.cc: In constructor 'Edge::Edge(Node, Node, int)':
a.cc:111:47: error: no matching function for call to 'Node::Node()'
  111 |         public Edge(Node id1,Node id2, int c) {
      |                                               ^
a.cc:81:16: note: candidate: 'Node::Node(int)'
   81 |         public Node(int id) {
      |                ^~~~
a.cc:81:16: note:   candidate expects 1 argument, 0 provided
a.cc:75:7: note: candidate: 'constexpr Node::Node(const Node&)'
   75 | class Node{
      |       ^~~~
a.cc:75:7: note:   candidate expects 1 argument, 0 provided
a.cc:75:7: note: candidate: 'constexpr Node::Node(Node&&)'
a.cc:75:7: note:   candidate expects 1 argument, 0 provided
a.cc:111:47: error: no matching function for call to 'Node::Node()'
  111 |         public Edge(Node id1,Node id2, int c) {
      |                                               ^
a.cc:81:16: note: candidate: 'Node::Node(int)'
   81 |         public Node(int id) {
      |                ^~~~
a.cc:81:16: note:   candidate expects 1 argument, 0 provided
a.cc:75:7: note: candidate: 'constexpr Node::Node(const Node&)'
   75 | class Node{
      |       ^~~~
a.cc:75:7: note:   candidate expects 1 argument, 0 provided
a.cc:75:7: note: candidate: 'constexpr Node::Node(Node&&)'
a.cc:75:7: note:   candidate expects 1 argument, 0 provided
a.cc:112:22: error: request for member 'v1' in '(Edge*)this', which is of pointer type 'Edge*' (maybe you meant to use '->' ?)
  112 |                 this.v1 = id1;
      |                      ^~
a.cc:113:22: error: request for member 'v2' in '(Edge*)this', which is of pointer type 'Edge*' (maybe you meant to use '->' ?)
  113 |                 this.v2 = id2;
      |                      ^~
a.cc:114:22: error: request for member 'cost' in '(Edge*)this', which is of pointer type 'Edge*' (maybe you meant to use '->' ?)
  114 |                 this.cost = c;
      |                      ^~~~
a.cc:115:28: error: invalid conversion from 'Edge*' to 'int' [-fpermissive]
  115 |                 v1.Connect(this);
      |                            ^~~~
      |                            |
      |                            Edge*
a.cc:103:34: note:   initializing argument 1 of 'void Node::Connect(int)'
  103 |         public void Connect(Edge e) {
      |                             ~~~~~^
a.cc:116:28: error: invalid conversion from 'Edge*' to 'int' [-fpermissive]
  116 |                 v2.Connect(this);
      |                            ^~~~
      |                            |
      |                            Edge*
a.cc:103:34: note:   initializing argument 1 of 'void Node::Connect(int)'
  103 |         public void Connect(Edge e) {
      |                             ~~~~~^
a.cc: In member function 'Node Edge::Other_one(Node)':
a.cc:128:22: error: 'class Node' has no member named 'equals'
  128 |                 if(v.equals(this.v1)) {
      |                      ^~~~~~
a.cc:128:34: error: request for member 'v1' in '(Edge*)this', which is of pointer type 'Edge*' (maybe you meant to use '->' ?)
  128 |                 if(v.equals(this.v1)) {
      |                                  ^~
a.cc:129:37: error: request for member 'v2' in '(Edge*)this', which is of pointer type 'Edge*' (maybe you meant to use '->' ?)
  129 |                         return this.v2;
      |                                     ^~
a.cc:132:37: error: request for member 'v1' in '(Edge*)this', which is of pointer type 'Edge*' (maybe you meant to use '->' ?)
  132 |                         return this.v1;
      |                                     ^~