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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

public class Program
{
    private int N, M, L;
    private List<Edge>[] Edges;

    private int Q;
    private int[] S, T;

    private int[,] Ans;

    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        M = sc.NextInt();
        L = sc.NextInt();
        Edges = new List<Edge>[N];
        for (int i = 0; i < N; i++)
        {
            Edges[i] = new List<Edge>();
        }

        for (int i = 0; i < M; i++)
        {
            int a = sc.NextInt() - 1;
            int b = sc.NextInt() - 1;
            int c = sc.NextInt();
            Edges[a].Add(new Edge(b, c));
            Edges[b].Add(new Edge(a, c));
        }

        Q = sc.NextInt();
        S = new int[Q];
        T = new int[Q];
        for (int i = 0; i < Q; i++)
        {
            S[i] = sc.NextInt() - 1;
            T[i] = sc.NextInt() - 1;
        }

        Ans = new int[N, N];
        for (int i = 0; i < N; i++)
        {
            Calc(i);
        }

        var sb = new StringBuilder();
        for (int i = 0; i < Q; i++)
        {
            sb.AppendLine(Ans[S[i], T[i]].ToString());
        }

        Console.Write(sb.ToString());
    }

    private void Calc(int start)
    {
        int[] ans = new int[N];
        for (int i = 0; i < N; i++)
        {
            if (i == start)
            {
                ans[i] = 0;
            }
            else
            {
                ans[i] = 99999;
            }
        }


        var deq = new LinkedList<S>();
        deq.AddLast(new S(start, L));
        while (deq.Count > 0)
        {
            var pop = deq.First.Value;
            deq.RemoveFirst();
            foreach (Edge edge in Edges[pop.Pos])
            {
                // しない
                if (edge.C <= pop.Oil)
                {
                    if (ans[edge.B] >= ans[pop.Pos])
                    {
                        ans[edge.B] = ans[pop.Pos];
                        deq.AddFirst(new S(edge.B, pop.Oil - edge.C));
                    }
                }

                // する
                if (edge.C <= L)
                {
                    if (ans[edge.B] >= ans[pop.Pos] + 1)
                    {
                        ans[edge.B] = ans[pop.Pos] + 1;
                        deq.AddLast(new S(edge.B, L - edge.C));
                    }
                }
            }
        }

        for (int i = 0; i < N; i++)
        {
            Ans[start, i] = ans[i] == 99999 ? -1 : ans[i];
        }
    }

    public static void Main(string[] args)
    {
        new Program().Solve();
    }
}

#region Deque<T>

/// <summary>
/// 指定した同じ型のインスタンスの，先頭または末尾への要素の追加，削除が可能な可変サイズのコレクションを表します．
/// </summary>
/// <typeparam name="T"><see cref="Deque{T}"/> 内の要素の型を指定します．</typeparam>
public class Deque<T>
{
    int dx;
    T[] buf;
    int mask;

    /// <summary>
    ///<see cref="Deque{T}"/> に格納されている要素の数を取得します．
    /// </summary>
    /// <remarks>この操作は計算量 O(1) で実行されます．</remarks>
    public int Count { get; private set; }

    /// <summary>
    /// 空であって，既定の初期容量を備えた <see cref="Deque{T}"/> クラスの新しいインスタンスを初期化します．
    /// </summary>
    public Deque() : this(8)
    {
    }

    /// <summary>
    /// 空であって，指定の初期容量を備えた <see cref="Deque{T}"/> クラスの新しいインスタンスを初期化します．
    /// </summary>
    /// <param name="capacity">作成したインスタンスが格納できる要素数の初期値．2 べきの値にしてください．</param>
    public Deque(int capacity)
    {
        Debug.Assert(capacity == (capacity & -capacity));
        mask = capacity - 1;
        buf = new T[capacity];
    }

    /// <summary>
    /// 指定したインデックスにある要素を取得または設定します．
    /// </summary>
    /// <param name="index">取得または設定する要素の，0-inexed での番号．</param>
    /// <returns>指定したインデックス位置にある要素</returns>
    /// <remarks>この操作は計算量 O(1) で実行されます．</remarks>
    public T this[int index]
    {
        get
        {
            Debug.Assert(0 <= index && index < Count);
            return buf[(dx + index) & mask];
        }
        set
        {
            Debug.Assert(0 <= index && index < Count);
            buf[(dx + index) & mask] = value;
        }
    }

    /// <summary>
    /// <see cref="Deque{T}"/> の先頭に要素を追加します．
    /// </summary>
    /// <param name="item">追加する要素</param>
    /// <remarks>この操作はならし計算量 O(1) で実行されます．</remarks>
    public void PushFront(T item)
    {
        if (Count == buf.Length) extend();
        dx = (dx + buf.Length - 1) & mask;
        buf[dx] = item;
        Count++;
    }

    /// <summary>
    /// <see cref="Deque{T}"/> の先頭から要素を削除し，返します．
    /// </summary>
    /// <remarks>この操作は計算量 O(1) で実行されます．</remarks>
    public T PopFront()
    {
        Debug.Assert(Count > 0);
        var ret = buf[dx = (dx + 1) & mask];
        Count--;
        return ret;
    }

    /// <summary>
    /// <see cref="Deque{T}"/> の末尾に要素を追加します．
    /// </summary>
    /// <param name="item">追加する要素</param>
    /// <remarks>この操作はならし計算量 O(1) で実行されます．</remarks>
    public void PushBack(T item)
    {
        if (Count == buf.Length) extend();
        buf[(dx + Count++) & mask] = item;
    }

    /// <summary>
    /// <see cref="Deque{T}"/> の末尾から要素を削除し，返します．
    /// </summary>
    /// <remarks>この操作は計算量 O(1) で実行されます．</remarks>
    public T PopBack()
    {
        Debug.Assert(Count > 0);
        var ret = buf[(dx + --Count) & mask];
        return ret;
    }

    /// <summary>
    /// <see cref="Deque{T}"/> の末尾の要素を返します．
    /// </summary>
    /// <remarks>この操作は計算量 O(1) で実行されます．</remarks>
    public T Last()
    {
        Debug.Assert(Count > 0);
        return buf[(dx + Count - 1) & mask];
    }

    /// <summary>
    /// <see cref="Deque{T}"/> の先頭の要素を返します．
    /// </summary>
    /// <remarks>この操作は計算量 O(1) で実行されます．</remarks>
    public T First()
    {
        Debug.Assert(Count > 0);
        return buf[dx];
    }

    void extend()
    {
        var nbuf = new T[buf.Length * 2];
        for (int i = 0; i < buf.Length; i++)
            nbuf[i] = buf[(dx + i) & mask];
        mask = mask * 2 + 1;
        dx = 0;
        buf = nbuf;
    }

    /// <summary>
    /// デックを空にします．
    /// </summary>
    /// <remarks>この操作は O(1) で実行されます．</remarks>
    public void Clear()
    {
        Count = 0;
    }

    /// <summary>
    /// <see cref="Deque{T}"/> 内に要素が存在するかどうかを調べます．
    /// </summary>
    /// <returns>要素が存在するならば true，そうでなければ false</returns>
    /// <remarks>この操作は O(1) で実行されます．</remarks>
    public bool Any()
    {
        return Count != 0;
    }

    /// <summary>
    /// 指定した位置に要素を挿入します．
    /// </summary>
    /// <param name="item">追加したい要素</param>
    /// <param name="index">追加したい位置の 0-indexed での番号</param>
    /// <remarks>この操作は最悪計算量 O(N) で実行されます．</remarks>
    public void Insert(int index, T item)
    {
        Debug.Assert(0 <= index && index <= Count);
        PushFront(item);
        for (int i = 0; i < index; i++)
            this[i] = this[i + 1];
        this[index] = item;
    }

    /// <summary>
    /// 指定した位置にある要素を削除し，返します．
    /// </summary>
    /// <param name="index">削除したい位置の 0-indexed での番号</param>
    /// <remarks>この操作は最悪計算量 O(N) で実行されます．</remarks>
    public T RemoveAt(int index)
    {
        Debug.Assert(0 <= index && index < Count);
        var ret = this[index];
        for (int i = index; i > 0; i--)
            this[i] = this[i - 1];
        PopFront();
        return ret;
    }

    /// <summary>
    /// <see cref="Deque{T}"/> 内にある要素の一覧を返します．
    /// </summary>
    /// <returns><see cref="Deque{T}"/> 内にある要素の一覧</returns>
    /// <remarks>この操作は O(N) で実行されます．</remarks>
    public T[] Items
    {
        get
        {
            var ret = new T[Count];
            for (int i = 0; i < Count; i++)
                ret[i] = this[i];
            return ret;
        }
    }
}

#endregion

struct S
{
    public int Pos, Oil;

    public S(int p, int o)
    {
        Pos = p;
        Oil = o;
    }
}

struct Edge
{
    public int B, C;

    public Edge(int b, int c)
    {
        B = b;
        C = c;
    }
}

class Scanner
{
    public Scanner()
    {
        _pos = 0;
        _line = new string[0];
    }

    const char Separator = ' ';
    private int _pos;
    private string[] _line;

    #region スペース区切りで取得

    public string Next()
    {
        if (_pos >= _line.Length)
        {
            _line = Console.ReadLine().Split(Separator);
            _pos = 0;
        }

        return _line[_pos++];
    }

    public int NextInt()
    {
        return int.Parse(Next());
    }

    public long NextLong()
    {
        return long.Parse(Next());
    }

    public double NextDouble()
    {
        return double.Parse(Next());
    }

    #endregion

    #region 型変換

    private int[] ToIntArray(string[] array)
    {
        var result = new int[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = int.Parse(array[i]);
        }

        return result;
    }

    private long[] ToLongArray(string[] array)
    {
        var result = new long[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = long.Parse(array[i]);
        }

        return result;
    }

    private double[] ToDoubleArray(string[] array)
    {
        var result = new double[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = double.Parse(array[i]);
        }

        return result;
    }

    #endregion

    #region 配列取得

    public string[] Array()
    {
        if (_pos >= _line.Length)
            _line = Console.ReadLine().Split(Separator);

        _pos = _line.Length;
        return _line;
    }

    public int[] IntArray()
    {
        return ToIntArray(Array());
    }

    public long[] LongArray()
    {
        return ToLongArray(Array());
    }

    public double[] DoubleArray()
    {
        return ToDoubleArray(Array());
    }

    #endregion
}