Problem:
Score : 400 points

Problem Statement

You are given a tree with N vertices.

Here, a tree is a kind of graph, and more specifically, a connected undirected graph with N-1 edges, where N is the number of its vertices.

The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of c_i.

You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):

find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K.

Constraints

3≤N≤10^5

1≤a_i,b_i≤N (1≤i≤N-1)

1≤c_i≤10^9 (1≤i≤N-1)

The given graph is a tree.

1≤Q≤10^5

1≤K≤N

1≤x_j,y_j≤N (1≤j≤Q)

x_j≠y_j (1≤j≤Q)

x_j≠K,y_j≠K (1≤j≤Q)

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q}

Output

Print the responses to the queries in Q lines.

In the j-th line j(1≤j≤Q), print the response to the j-th query.

Sample Input 1

5
1 2 1
1 3 1
2 4 1
3 5 1
3 1
2 4
2 3
4 5

Sample Output 1

3
2
4

The shortest paths for the three queries are as follows:

Query 1: Vertex 2 → Vertex 1 → Vertex 2 → Vertex 4 : Length 1+1+1=3

Query 2: Vertex 2 → Vertex 1 → Vertex 3 : Length 1+1=2

Query 3: Vertex 4 → Vertex 2 → Vertex 1 → Vertex 3 → Vertex 5 : Length 1+1+1+1=4

Sample Input 2

7
1 2 1
1 3 3
1 4 5
1 5 7
1 6 9
1 7 11
3 2
1 3
4 5
6 7

Sample Output 2

5
14
22

The path for each query must pass Vertex K=2.

Sample Input 3

10
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000
6 7 1000000000
7 8 1000000000
8 9 1000000000
9 10 1000000000
1 1
9 10

Sample Output 3

17000000000

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)



import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
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);
		HashSet<Edge> used = new HashSet<>();
		v[K-1].Reload_dist(0);
		for(Edge connect:v[K-1].edges) {
			connect.Other_one(v[K-1]).Reload_dist(connect.cost);
			searching.add(connect.Other_one(v[K-1]));
			used.add(connect);
		}
		searched.add(v[K-1]);

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




		out.flush();
	}
}

class Node{
	int id;
	double 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(double 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;
		}
	}
}

class MyComparator implements Comparator<Node>{
	@Override public int compare(Node u,Node v) {

		int num1 = u.id;
		int num2 = v.id;
		if(num1 > num2) {
			return 1;
		}
		else if(num1 < num2) {
			return -1;
		}
		else {
			return 0;
		}
	}
}

class Graph{
	static Node[] nodes;
	static Edge[] edges;

	public Graph(Node[] v,Edge[] e) {
		Graph.nodes = v;
		Graph.edges = e;
	}

	public static int size() {
		return nodes.length;
	}

	public boolean Bellman_Ford(Node s) {
		boolean negative_loop[] = new boolean[size()];
		Arrays.fill(negative_loop, false);
		s.Reload_dist(0);
		for(int i=0;i<size();i++) {
			for(Node v : nodes) {
				for(Edge e : v.edges) {
					if((v.d != Long.MAX_VALUE && e.Other_one(v).d > v.d + e.cost)) {
						e.Other_one(v).Reload_dist(v.d + e.cost);
						negative_loop[e.Other_one(v).id] = true;
					}
					if(i==size()-1) {
						return true;
					}
				}
			}
		}
		return false;
	}

}