open System
open System.Collections.Generic

[<AutoOpen>]
module Cin =
    let read f = stdin.ReadLine() |> f
    let reada f = stdin.ReadLine().Split() |> Array.map f
    let readChars() = read string |> Seq.toArray
    let readInts() = readChars() |> Array.map (fun x -> Convert.ToInt32(x.ToString()))

[<AutoOpen>]
module Cout =
    let writer = new IO.StreamWriter(new IO.BufferedStream(Console.OpenStandardOutput()))
    let print (s: string) = writer.Write s
    let println (s: string) = writer.WriteLine s
    let inline puts (s: ^a) = string s |> println

type PriorityQueue<'T when 'T: comparison>(comparer: IComparer<'T>, isDescending: bool) =
    let _heap = ResizeArray<'T>()
    let _comparer = comparer
    let _isDescending = isDescending

    let parent i = (i - 1) / 2
    let leftChild i = (i <<< 1) + 1
    let rightChild i = (i <<< 1) + 2

    member self.Compare(x, y) =
        match _isDescending with
        | true -> _comparer.Compare(y, x) // 降順
        | false -> _comparer.Compare(x, y) // 昇順

    member self.Any() = _heap.Count > 0

    member self.Swap(x: int, y: int) =
        let tmp = _heap.[x]
        _heap.[x] <- _heap.[y]
        _heap.[y] <- tmp







    member self.Enque(item: 'T) =
        _heap.Add(item)
        let mutable i = _heap.Count - 1

        let rec loop() =
            match i <= 0 with
            | true -> ()
            | false ->
                let p = parent i
                match self.Compare(_heap.[i], _heap.[p]) >= 0 with
                | true -> ()
                | false ->
                    self.Swap(i, p)
                    i <- p
                    loop()
        loop()







    member self.Deque() =
        let res = _heap.[0]
        let last = _heap.Count - 1
        let lastItem = _heap.[last]

        let mutable p = 0

        let rec loop() =
            let mutable l = leftChild p
            match l >= last with
            | true -> ()
            | false ->
                let r = rightChild p
                if r < last && self.Compare(_heap.[r], _heap.[l]) < 0 then l <- r
                let l = l
                match self.Compare(_heap.[l], lastItem) >= 0 with
                | true -> ()
                | false ->
                    self.Swap(p, l)
                    p <- l
                    loop()
        loop()

        _heap.[p] <- lastItem
        _heap.RemoveAt(last)
        res

    member self.Debug() = printfn "%s" (String.Join(" ", _heap))


[<CustomComparison; CustomEquality>]
type Edge =
    { toNode: int
      cost: int64 }

    interface IComparable<Edge> with
        member self.CompareTo other = compare self.cost other.cost

    interface IComparable with
        member self.CompareTo(other: obj) =
            match other with
            | :? Edge as o -> compare self.cost o.cost
            | _ -> invalidArg "other" "Must be of type Edge"

    interface IEquatable<Edge> with
        member self.Equals(o: Edge) = self.toNode = o.toNode && self.cost = o.cost

type EdgeComparer() =
    interface IComparer<Edge> with
        member __.Compare(x, y) = compare x y

let dijkstra (graph: Edge [,]) (start: int) =
    let inf = Int64.MaxValue
    let n = graph.GetLength(0)

    let dist = Array.init n (fun _ -> inf)
    dist.[start] <- 0L
    let que = PriorityQueue<Edge>(EdgeComparer(), false)

    let s =
        { toNode = start
          cost = dist.[start] }
    que.Enque(s)
    let rec loop() =
        printfn "hoge"
        if que.Any() |> not then
            ()
        else
            let e = que.Deque()
            let from, cost = e.toNode, e.cost
            match dist.[from] < cost with
            | true -> ()
            | false ->
                for i in 0 .. n - 1 do
                    let next = graph.[from, i]
                    let nextCost = next.cost + e.cost
                    if dist.[next.toNode] > nextCost then
                        dist.[next.toNode] <- nextCost
                        que.Enque
                            ({ toNode = next.toNode
                               cost = nextCost })
    loop()
    dist

let solve() =
    let N = read int
    let graph = Array2D.zeroCreate N N
    for i in 0 .. N - 1 do
        let [| a; b; c |] = reada int
        let a, b, c = a - 1, b - 1, int64 c
        graph.[a, b] <- { toNode = b
                          cost = c }
        graph.[b, a] <- { toNode = a
                          cost = c }
    let [| Q; K |] = reada int
    let K = K - 1
    let dist = dijkstra graph K
    for q in 0 .. Q - 1 do
        let [| x; y |] = reada int
        let x, y = x - 1, y - 1
        let ans = dist.[x] + dist.[y]
        puts ans

    ()

[<EntryPoint>]
let main _ =
    try
        solve()
    with e -> printfn "%s" (e.ToString())
    writer.Close()
    0 // return an integer exit code
