Problem:
Score : 500 points

Problem Statement

There are N towns numbered 1 to N and M roads. The i-th road connects Town A_i and Town B_i bidirectionally and has a length of C_i.

Takahashi will travel between these towns by car, passing through these roads. The fuel tank of his car can contain at most L liters of fuel, and one liter of fuel is consumed for each unit distance traveled. When visiting a town while traveling, he can full the tank (or choose not to do so). Travel that results in the tank becoming empty halfway on the road cannot be done.

Process the following Q queries:

The tank is now full. Find the minimum number of times he needs to full his tank while traveling from Town s_i to Town t_i. If Town t_i is unreachable, print -1.

Constraints

All values in input are integers.

2 \leq N \leq 300

0 \leq M \leq \frac{N(N-1)}{2}

1 \leq L \leq 10^9

1 \leq A_i, B_i \leq N

A_i \neq B_i

\left(A_i, B_i\right) \neq \left(A_j, B_j\right) (if i \neq j)

\left(A_i, B_i\right) \neq \left(B_j, A_j\right) (if i \neq j)

1 \leq C_i \leq 10^9

1 \leq Q \leq N\left(N-1\right)

1 \leq s_i, t_i \leq N

s_i \neq t_i

\left(s_i, t_i\right) \neq \left(s_j, t_j\right) (if i \neq j)

Input

Input is given from Standard Input in the following format:

N M L
A_1 B_1 C_1
:
A_M B_M C_M
Q
s_1 t_1
:
s_Q t_Q

Output

Print Q lines.

The i-th line should contain the minimum number of times the tank needs to be fulled while traveling from Town s_i to Town t_i. If Town t_i is unreachable, the line should contain -1 instead.

Sample Input 1

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

Sample Output 1

0
1

To travel from Town 3 to Town 2, we can use the second road to reach Town 2 without fueling the tank on the way.

To travel from Town 1 to Town 3, we can first use the first road to get to Town 2, full the tank, and use the second road to reach Town 3.

Sample Input 2

4 0 1
1
2 1

Sample Output 2

-1

There may be no road at all.

Sample Input 3

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

Sample Output 3

0
0
1
2
0
0
1
2
0
0
0
1
1
1
0
0
2
2
1
0

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

#![allow(unused_imports)]
#![allow(unused_variables)]
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(deprecated)]

use std::cell::{Cell, Ref, RefCell, RefMut};
use std::cmp::{max, min, Ordering};
use std::collections::*;
use std::fmt::{Debug, Formatter, Write as FmtWrite};
use std::io::{stderr, stdin, BufRead, Write};
use std::mem::{replace, swap};
use std::ops::*;
use std::rc::Rc;
use std::usize;

const MOD_10_9_7: u64 = 1_000_000_007;
const INF: i64 = 1_000_000_000_000;
const MIN_INF: i64 = -1_000_000_000_000;

/// FYI: https://github.com/vain0x/scan-bench
#[allow(unused_macros)]
macro_rules! read {
    ([$t:ty] ; $n:expr) =>
        ((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
    ($($t:ty),+ ; $n:expr) =>
        ((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
    ([$t:ty]) =>
        (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
    ($t:ty) =>
        (rl().parse::<$t>().unwrap());
    ($($t:ty),*) => {{
        let buf = rl();
        let mut w = buf.split_whitespace();
        ($(w.next().unwrap().parse::<$t>().unwrap()),*)
    }};
}

fn rl() -> String {
    let mut buf = String::new();
    stdin().read_line(&mut buf).unwrap();
    buf.trim_right().to_owned()
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}

#[allow(unused_macros)]
macro_rules! debugln {
    ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}

fn a() {
    let (a, b) = read!(i32, i32);
    println!("{}", max(0, a - 2 * b));
}

fn b() {
    let n = read!(usize);
    let d: Vec<usize> = read![[usize]];

    let mut ans = 0;
    for i in 0..(n - 1) {
        for j in (i + 1)..n {
            ans += d[i] * d[j];
        }
    }
    println!("{}", ans);
}

fn c() {
    let n = read!(usize);
    let s: String = read!(String);

    let c: Vec<char> = s.chars().collect();

    let mut prev = c[0];
    let mut ans = 0;
    for i in 0..n {
        if prev == c[i] {
            continue;
        } else {
            ans += 1;
            prev = c[i];
        }
    }
    println!("{}", ans + 1);
}

fn d() {
    let n = read!(usize);
    let mut l: Vec<usize> = read![[usize]];
    l.sort();
    l.reverse();

    let mut dp = vec![0; 3000]; // lから2つ選んで和がxになるような組合せの数

    for i in 0..(n - 1) {
        for j in (i + 1)..n {
            let s = l[i] + l[j];
            dp[s] += 1;
        }
    }

    let mut under = vec![0; 3000];
    under[0] = 0;
    under[1] = 0;
    for i in 2..dp.len() {
        let t = under[i - 1];
        under[i] = t + dp[i];
    }

    let mut ans = n * (n - 1) * (n - 2) / 6;
    for i in 0..n {
        let max_v = l[i];
        ans -= under[max_v];
    }
    println!("{}", ans);
}

fn e() {
    let (n, m, l) = read!(usize, usize, i64);
    let mut abc = Vec::new();
    for i in 0..m {
        let t: Vec<usize> = read![[usize]];
        abc.push(t);
    }
    let q = read!(usize);
    let mut st = Vec::new();
    for i in 0..q {
        let t: Vec<usize> = read![[usize]];
        st.push(t);
    }

    let mut cost = vec![vec![1 << 60; n]; n];
    let mut next = vec![vec![1 << 60; n]; n];
    for i in 0..n {
        cost[i][i] = 0;
        for j in 0..n {
            next[i][j] = j;
        }
    }

    for row in abc {
        let from = row[0] - 1;
        let to = row[1] - 1;
        let cc = row[2];
        cost[from][to] = cc;
        cost[to][from] = cc;
    }

    for k in 0..n {
        for i in 0..n {
            for j in 0..n {
                let tmp = cost[i][k] + cost[k][j];
                if cost[i][j] > tmp {
                    cost[i][j] = tmp;
                    next[i][j] = next[i][k];
                }
            }
        }
    }

    'f: for query in st {
        let start = query[0] - 1;
        let end = query[1] - 1;
        if cost[start][end] == 1 << 60 {
            println!("-1");
            continue;
        }
        let mut ans = 0; // 補給回数
        let mut now = start;
        let mut tank: i64 = l;
        while now != end {
            let nexttown = next[now][end];
            // 次の街までの必要燃料がタンク最大容量を超えてたら無理
            if cost[now][nexttown] > l as usize {
                println!("-1");
                continue 'f;
            }

            // 燃料が足りない場合、満タンまで補充
            if cost[now][nexttown] > tank as usize {
                tank = l;
                ans += 1;
            }

            // 次の街まで移動
            tank -= cost[now][nexttown] as i64;
            now = nexttown;
        }
        println!("{}", ans);
    }
}

fn f() {
    unimplemented!();
}

fn main() {
    e();
}