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: Accepted)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static System.Math;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Runtime.CompilerServices;
using Library;

namespace Program
{
    public static class ABC143E
    {
        static public int numberOfRandomCases = 0;
        static public void MakeTestCase(List<string> _input, List<string> _output, ref Func<string[], bool> _outputChecker)
        {
        }
        static public void Solve()
        {
            var N = NN;
            var M = NN;
            var L = NN;
            var abcList = Repeat(0, M).Select(_ => new { A = NN - 1, B = NN - 1, C = NN }).ToArray();
            var Q = NN;
            var queryList = Repeat(0, Q).Select(_ => new { s = NN - 1, t = NN - 1 }).ToArray();
            var path = Repeat(0, N).Select(_ => new List<Tuple<long, long>>()).ToArray();
            foreach (var abc in abcList)
            {
                path[abc.A].Add(Tuple.Create(abc.B, abc.C));
                path[abc.B].Add(Tuple.Create(abc.A, abc.C));
            }
            var ans = new long[N, N];
            for (var i = 0; i < N; i++)
            {
                {
                    var dist = Repeat(1000000000000000000, N).ToArray();
                    var enery = Repeat(0L, N).ToArray();
                    var q = new LIB_PriorityQueue<long, long>();
                    for (var j = 0; j < N; j++)
                    {
                        q.Push(1000000000000000000, j);
                    }
                    dist[i] = 0;
                    enery[i] = L;
                    q.ChangeKey(i, 0);
                    var status = new bool[N];
                    status[i] = true;
                    while (q.Peek.Key < 1000000000000000000)
                    {
                        var t = q.Peek;
                        q.ChangeKey(t.Value, 1000000000000000000);
                        var v = t.Value;
                        if (!status[v]) continue;
                        status[v] = false;
                        foreach (var item in path[v])
                        {
                            var eneTemp = 0L;
                            var distTemp = 0L;
                            if (enery[v] < item.Item2)
                            {
                                if (L < item.Item2)
                                {
                                    continue;
                                }
                                else
                                {
                                    eneTemp = L - item.Item2;
                                    distTemp = dist[v] + 1;
                                }
                            }
                            else
                            {
                                eneTemp = enery[v] - item.Item2;
                                distTemp = dist[v];
                            }
                            if (dist[item.Item1] > distTemp ||
                               (dist[item.Item1] == distTemp && enery[item.Item1] < eneTemp))
                            {
                                enery[item.Item1] = eneTemp;
                                dist[item.Item1] = distTemp;
                                q.ChangeKey(item.Item1, dist[item.Item1] * L + (L - enery[item.Item1]));
                                status[item.Item1] = true;
                            }
                        }
                    }
                    for (var j = 0; j < N; j++)
                    {
                        if (dist[j] > 1000000000000000)
                        {
                            ans[i, j] = -1;
                        }
                        else
                        {
                            ans[i, j] = dist[j];
                        }
                    }
                }
            }
            for (var i = 0; i < Q; i++)
            {
                Console.WriteLine(ans[queryList[i].s, queryList[i].t]);
            }
        }
        static class Console_
        {
            static Queue<string> param = new Queue<string>();
            public static string NextString() { if (param.Count == 0) foreach (var item in Console.ReadLine().Split(' ')) param.Enqueue(item); return param.Dequeue(); }
        }
        class Printer : StreamWriter
        {
            public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
            public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) { base.AutoFlush = false; }
            public Printer(Stream stream, Encoding encoding) : base(stream, encoding) { base.AutoFlush = false; }
        }
        static public void Main(string[] args) { if (args.Length == 0) { Console.SetOut(new Printer(Console.OpenStandardOutput())); } var t = new Thread(Solve, 134217728); t.Start(); t.Join(); Console.Out.Flush(); }
        static long NN => long.Parse(Console_.NextString());
        static double ND => double.Parse(Console_.NextString());
        static string NS => Console_.NextString();
        static long[] NNList(long N) => Repeat(0, N).Select(_ => NN).ToArray();
        static double[] NDList(long N) => Repeat(0, N).Select(_ => ND).ToArray();
        static string[] NSList(long N) => Repeat(0, N).Select(_ => NS).ToArray();
        static IEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => x.OrderBy(_ => xorshift);
        static long Count<T>(this IEnumerable<T> x, Func<T, bool> pred) => Enumerable.Count(x, pred);
        static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n);
        static IEnumerable<int> Range(long s, long c) => Enumerable.Range((int)s, (int)c);
        static uint xorshift { get { _xsi.MoveNext(); return _xsi.Current; } }
        static IEnumerator<uint> _xsi = _xsc();
        static IEnumerator<uint> _xsc() { uint x = 123456789, y = 362436069, z = 521288629, w = (uint)(DateTime.Now.Ticks & 0xffffffff); while (true) { var t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); yield return w; } }
    }
}
namespace Library {
    class LIB_PriorityQueue<TK, TV>
    {
        public class KeyValuePair
        {
            public TK Key;
            public TV Value;
        }
        List<KeyValuePair> heap;
        Dictionary<TV, int> map;
        Comparison<TK> comp;
        public KeyValuePair Peek => heap[0];
        public int Count => heap.Count;
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public LIB_PriorityQueue(int cap, Comparison<TK> cmp, bool asc = true)
        {
            heap = new List<KeyValuePair>(cap);
            map = new Dictionary<TV, int>(cap);
            comp = asc ? cmp : (x, y) => cmp(y, x);
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public LIB_PriorityQueue(Comparison<TK> cmp, bool asc = true)
        {
            heap = new List<KeyValuePair>();
            map = new Dictionary<TV, int>();
            comp = asc ? cmp : (x, y) => cmp(y, x);
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public LIB_PriorityQueue(int cap, bool asc = true) : this(cap, Comparer<TK>.Default.Compare, asc) { }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public LIB_PriorityQueue(bool asc = true) : this(Comparer<TK>.Default.Compare, asc) { }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Push(TK key, TV value)
        {
            var i = heap.Count;
            var kv = new KeyValuePair() { Key = key, Value = value };
            heap.Add(kv);
            while (i > 0)
            {
                var ni = (i - 1) / 2;
                if (comp(key, heap[ni].Key) >= 0) break;
                heap[i] = heap[ni];
                map[heap[ni].Value] = i;
                i = ni;
            }
            heap[i] = kv;
            map[value] = i;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public KeyValuePair Pop()
        {
            var ret = heap[0];
            var kv = heap.Last();
            heap.RemoveAt(heap.Count - 1);
            if (heap.Count == 0) return ret;
            var i = 0;
            while ((i << 1) + 1 < heap.Count)
            {
                var i1 = (i << 1) + 1;
                var i2 = (i << 1) + 2;
                if (i2 < heap.Count && comp(heap[i1].Key, heap[i2].Key) > 0) i1 = i2;
                if (comp(kv.Key, heap[i1].Key) <= 0) break;
                heap[i] = heap[i1];
                map[heap[i1].Value] = i;
                i = i1;
            }
            heap[i] = kv;
            map[kv.Value] = i;
            return ret;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void ChangeKey(TV value, TK newKey)
        {
            var i = map[value];
            var kv = heap[i];
            var dir = comp(kv.Key, newKey) < 0;
            kv.Key = newKey;
            if (dir) // decrease
            {
                while ((i << 1) + 1 < heap.Count)
                {
                    var i1 = (i << 1) + 1;
                    var i2 = (i << 1) + 2;
                    if (i2 < heap.Count && comp(heap[i1].Key, heap[i2].Key) > 0) i1 = i2;
                    if (comp(kv.Key, heap[i1].Key) <= 0) break;
                    heap[i] = heap[i1];
                    map[heap[i1].Value] = i;
                    i = i1;
                }
                heap[i] = kv;
                map[kv.Value] = i;
            }
            else // increase
            {
                while (i > 0)
                {
                    var ni = (i - 1) / 2;
                    if (comp(kv.Key, heap[ni].Key) >= 0) break;
                    heap[i] = heap[ni];
                    map[heap[ni].Value] = i;
                    i = ni;
                }
                heap[i] = kv;
                map[value] = i;
            }
        }
    }
    class LIB_PriorityQueue<T>
    {
        LIB_PriorityQueue<T, int> q;
        public T Peek => q.Peek.Key;
        public int Count => q.Count;
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public LIB_PriorityQueue(int cap, Comparison<T> cmp, bool asc = true)
        {
            q = new LIB_PriorityQueue<T, int>(cap, cmp, asc);
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public LIB_PriorityQueue(Comparison<T> cmp, bool asc = true)
        {
            q = new LIB_PriorityQueue<T, int>(cmp, asc);
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public LIB_PriorityQueue(int cap, bool asc = true) : this(cap, Comparer<T>.Default.Compare, asc) { }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public LIB_PriorityQueue(bool asc = true) : this(Comparer<T>.Default.Compare, asc) { }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Push(T val) => q.Push(val, 0);
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public T Pop() => q.Pop().Key;
    }
}