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;
    }
}


Measured memory_bytes: 36008