#![allow(unused_parens)]
#![allow(unused_imports)]
#![allow(non_upper_case_globals)]

#[allow(unused_macros)]
macro_rules! invec {
    ( $ t : ty ) => {{
        let mut s = String::new();
        match std::io::stdin().read_line(&mut s) {
            Ok(0) => Vec::<$t>::new(),
            Ok(n) => s
                .trim()
                .split_whitespace()
                .map(|s| s.parse::<$t>().unwrap())
                .collect::<Vec<$t>>(),
            Err(_) => Vec::<$t>::new(),
        }
    }};
}

#[allow(unused_macros)]
macro_rules! get {
      ($t:ty) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              line.trim().parse::<$t>().unwrap()
          }
      };
      ($($t:ty),*) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              let mut iter = line.split_whitespace();
              (
                  $(iter.next().unwrap().parse::<$t>().unwrap(),)*
              )
          }
      };
      ($t:ty; $n:expr) => {
          (0..$n).map(|_|
              get!($t)
          ).collect::<Vec<_>>()
      };
      ($($t:ty),*; $n:expr) => {
          (0..$n).map(|_|
              get!($($t),*)
          ).collect::<Vec<_>>()
      };
      ($t:ty ;;) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              line.split_whitespace()
                  .map(|t| t.parse::<$t>().unwrap())
                  .collect::<Vec<_>>()
          }
      };
      ($t:ty ;; $n:expr) => {
          (0..$n).map(|_| get!($t ;;)).collect::<Vec<_>>()
      };
}

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [$t:tt]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[cfg(debug_assertions)]
macro_rules! mydbg {
    //($arg:expr) => (dbg!($arg))
    //($arg:expr) => (println!("{:?}",$arg));
      ($($a:expr),*) => {
          eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
      }
}
#[cfg(not(debug_assertions))]
macro_rules! mydbg {
    ($($arg:expr),*) => {};
}

use std::cmp;
use std::collections::HashMap;

#[allow(dead_code)]
static INF_I64: i64 = 9223372036854775807;
#[allow(dead_code)]
static INF_I32: i32 = 2147483647;
#[allow(dead_code)]
static INF_USIZE: usize = 18446744073709551615;

pub struct Dikstra {
    dist: Vec<i64>,
    start: usize,
}
impl Dikstra {
    //vec<(重み,接続先の頂点)>
    pub fn new(g: &Vec<Vec<(i64, usize)>>, start: usize) -> Self {
        // let mut graph = vec![vec![INF_USIZE;g.len()];g.len()];
        // for i in 0..g.len(){
        //     for j in 0..g.len(){
        //         graph[i][j] = g[i][j];
        //     }
        // }

        let mut d = Dikstra {
            dist: vec![INF_I64; g.len()],
            start: start,
        };

        d.calc(g);
        return d;
    }

    fn calc(&mut self, g: &Vec<Vec<(i64, usize)>>) {
        let mut used = vec![false; g.len()];
        let mut kouho = std::collections::BinaryHeap::new();
        kouho.push((0, self.start));

        while !kouho.is_empty() {
            let (_, k) = kouho.pop().unwrap();
            used[self.start] = true;
            self.dist[self.start] = 0;
            for i in 0..g[k].len() {
                let (d, p) = g[k][i];
                if used[p] {
                    continue;
                }
                if self.dist[p] > self.dist[k] + d as i64 {
                    self.dist[p] = self.dist[k] + d as i64;
                    kouho.push((self.dist[p] * -1, p));
                }
            }
        }
    }

    pub fn dist_to(&mut self, x: usize) -> i64 {
        mydbg!(self.dist);
        return self.dist[x];
    }
}

fn main() {
    input! {
       n: usize,
       a: [(usize,usize,usize);n-1],
       q:usize,
       k:usize,
       x:[(usize,usize);q],
    }

    let mut ans = 0;
    let mut graph = vec![vec![]; n];
    for i in 0..n - 1 {
        let (e, d, f) = a[i];
        graph[e - 1].push((f as i64, d - 1));
        graph[d - 1].push((f as i64, e - 1));
    }

    let mut dik = Dikstra::new(&graph, k - 1);

    for &(s, e) in &x {
        let t1 = dik.dist_to(s - 1);
        let t2 = dik.dist_to(e - 1);
        println!("{}", t1 + t2);
    }
}


Measured memory_bytes: 30968