def solve():

    INF = float('inf')



    def Dijkstra(adjList, vSt):

        numV = len(adjList)

        useds = [False] * numV

        fuels = [-1] * numV

        fuels[vSt] = L

        nums = [INF] * numV

        num = 0

        vs = set([vSt])

        while vs:

            vNexts = set()

            fuelNexts = [-1] * numV

            while vs:

                fNow, vNow = -1, -1

                for v in vs:

                    if fuels[v] > fNow:

                        fNow, vNow = fuels[v], v

                nums[vNow] = num

                useds[vNow] = True

                vs.remove(vNow)



                fuel = fuels[vNow]

                for v2, wt in adjList[vNow]:

                    if useds[v2]: continue

                    if fuel >= wt:

                        if fuel-wt > fuels[v2]:

                            fuels[v2] = fuel-wt

                        vs.add(v2)

                    elif L >= wt:

                        if L-wt > fuelNexts[v2]:

                            fuelNexts[v2] = L-wt

                        vNexts.add(v2)



            vs = set([v for v in vNexts if not useds[v]])

            fuels = fuelNexts

            num += 1

        return nums





    N, M, L = list(map(int, input().split()))

    adjL = [[] for _ in range(N)]

    for _ in range(M):

        A, B, C = list(map(int, input().split()))

        A, B = A-1, B-1

        adjL[A].append((B, C))

        adjL[B].append((A, C))



    numss = []

    for v in range(N):

        nums = Dijkstra(adjL, v)

        numss.append(nums)



    Q = int(eval(input()))

    anss = []

    for _ in range(Q):

        s, t = list(map(int, input().split()))

        s, t = s-1, t-1

        if numss[s][t] == INF:

            anss.append(-1)

        else:

            anss.append(numss[s][t])



    print(('\n'.join(map(str, anss))))





solve()

import sys

input = sys.stdin.readline



def solve():

    INF = float('inf')



    def WarshallFloyd(adjList, INF):

        numV = len(adjList)

        D = [[INF]*numV for _ in range(numV)]

        for u, adj in enumerate(adjList):

            for v, wt in adj:

                D[u][v] = wt

            D[u][u] = 0



        for k in range(numV):

            Dk = D[k]

            for i in range(numV):

                Di = D[i]

                Dik = Di[k]

                for j in range(numV):

                    D2 = Dik + Dk[j]

                    if D2 < Di[j]:

                        D[i][j] = D2



        return D



    N, M, L = list(map(int, input().split()))

    adjL = [[] for _ in range(N)]

    for _ in range(M):

        a, b, c = list(map(int, input().split()))

        a, b = a-1, b-1

        adjL[a].append((b, c))

        adjL[b].append((a, c))

    Q = int(eval(input()))

    querys = [tuple(map(int, input().split())) for _ in range(Q)]



    cost1ss = WarshallFloyd(adjL, INF)



    adjL2 = [[] for _ in range(N)]

    for a in range(N):

        for b in range(a+1, N):

            if cost1ss[a][b] <= L:

                adjL2[a].append((b, 1))

                adjL2[b].append((a, 1))



    cost2ss = WarshallFloyd(adjL2, INF)



    anss = []

    for s, t in querys:

        s, t = s-1, t-1

        cost = cost2ss[s][t]

        if cost == INF:

            anss.append(-1)

        else:

            anss.append(cost-1)



    print(('\n'.join(map(str, anss))))





solve()

def solve():
    INF = float("inf")

    def Dijkstra(adjList, vSt):
        numV = len(adjList)
        useds = [False] * numV
        fuels = [-1] * numV
        fuels[vSt] = L
        nums = [INF] * numV
        num = 0
        vs = set([vSt])
        while vs:
            vNexts = set()
            fuelNexts = [-1] * numV
            while vs:
                fNow, vNow = -1, -1
                for v in vs:
                    if fuels[v] > fNow:
                        fNow, vNow = fuels[v], v
                nums[vNow] = num
                useds[vNow] = True
                vs.remove(vNow)
                fuel = fuels[vNow]
                for v2, wt in adjList[vNow]:
                    if useds[v2]:
                        continue
                    if fuel >= wt:
                        if fuel - wt > fuels[v2]:
                            fuels[v2] = fuel - wt
                        vs.add(v2)
                    elif L >= wt:
                        if L - wt > fuelNexts[v2]:
                            fuelNexts[v2] = L - wt
                        vNexts.add(v2)
            vs = set([v for v in vNexts if not useds[v]])
            fuels = fuelNexts
            num += 1
        return nums

    N, M, L = list(map(int, input().split()))
    adjL = [[] for _ in range(N)]
    for _ in range(M):
        A, B, C = list(map(int, input().split()))
        A, B = A - 1, B - 1
        adjL[A].append((B, C))
        adjL[B].append((A, C))
    numss = []
    for v in range(N):
        nums = Dijkstra(adjL, v)
        numss.append(nums)
    Q = int(eval(input()))
    anss = []
    for _ in range(Q):
        s, t = list(map(int, input().split()))
        s, t = s - 1, t - 1
        if numss[s][t] == INF:
            anss.append(-1)
        else:
            anss.append(numss[s][t])
    print(("\n".join(map(str, anss))))


solve()

import sys

input = sys.stdin.readline


def solve():
    INF = float("inf")

    def WarshallFloyd(adjList, INF):
        numV = len(adjList)
        D = [[INF] * numV for _ in range(numV)]
        for u, adj in enumerate(adjList):
            for v, wt in adj:
                D[u][v] = wt
            D[u][u] = 0
        for k in range(numV):
            Dk = D[k]
            for i in range(numV):
                Di = D[i]
                Dik = Di[k]
                for j in range(numV):
                    D2 = Dik + Dk[j]
                    if D2 < Di[j]:
                        D[i][j] = D2
        return D

    N, M, L = list(map(int, input().split()))
    adjL = [[] for _ in range(N)]
    for _ in range(M):
        a, b, c = list(map(int, input().split()))
        a, b = a - 1, b - 1
        adjL[a].append((b, c))
        adjL[b].append((a, c))
    Q = int(eval(input()))
    querys = [tuple(map(int, input().split())) for _ in range(Q)]
    cost1ss = WarshallFloyd(adjL, INF)
    adjL2 = [[] for _ in range(N)]
    for a in range(N):
        for b in range(a + 1, N):
            if cost1ss[a][b] <= L:
                adjL2[a].append((b, 1))
                adjL2[b].append((a, 1))
    cost2ss = WarshallFloyd(adjL2, INF)
    anss = []
    for s, t in querys:
        s, t = s - 1, t - 1
        cost = cost2ss[s][t]
        if cost == INF:
            anss.append(-1)
        else:
            anss.append(cost - 1)
    print(("\n".join(map(str, anss))))


solve()

+import sys + +input = sys.stdin.readline + + -    def Dijkstra(adjList, vSt): +    def WarshallFloyd(adjList, INF): -        useds = [False] * numV -        fuels = [-1] * numV -        fuels[vSt] = L -        nums = [INF] * numV -        num = 0 -        vs = set([vSt]) -        while vs: -            vNexts = set() -            fuelNexts = [-1] * numV -            while vs: -                fNow, vNow = -1, -1 -                for v in vs: -                    if fuels[v] > fNow: -                        fNow, vNow = fuels[v], v -                nums[vNow] = num -                useds[vNow] = True -                vs.remove(vNow) -                fuel = fuels[vNow] -                for v2, wt in adjList[vNow]: -                    if useds[v2]: -                        continue -                    if fuel >= wt: -                        if fuel - wt > fuels[v2]: -                            fuels[v2] = fuel - wt -                        vs.add(v2) -                    elif L >= wt: -                        if L - wt > fuelNexts[v2]: -                            fuelNexts[v2] = L - wt -                        vNexts.add(v2) -            vs = set([v for v in vNexts if not useds[v]]) -            fuels = fuelNexts -            num += 1 -        return nums +        D = [[INF] * numV for _ in range(numV)] +        for u, adj in enumerate(adjList): +            for v, wt in adj: +                D[u][v] = wt +            D[u][u] = 0 +        for k in range(numV): +            Dk = D[k] +            for i in range(numV): +                Di = D[i] +                Dik = Di[k] +                for j in range(numV): +                    D2 = Dik + Dk[j] +                    if D2 < Di[j]: +                        D[i][j] = D2 +        return D -        A, B, C = list(map(int, input().split())) -        A, B = A - 1, B - 1 -        adjL[A].append((B, C)) -        adjL[B].append((A, C)) -    numss = [] -    for v in range(N): -        nums = Dijkstra(adjL, v) -        numss.append(nums) +        a, b, c = list(map(int, input().split())) +        a, b = a - 1, b - 1 +        adjL[a].append((b, c)) +        adjL[b].append((a, c)) +    querys = [tuple(map(int, input().split())) for _ in range(Q)] +    cost1ss = WarshallFloyd(adjL, INF) +    adjL2 = [[] for _ in range(N)] +    for a in range(N): +        for b in range(a + 1, N): +            if cost1ss[a][b] <= L: +                adjL2[a].append((b, 1)) +                adjL2[b].append((a, 1)) +    cost2ss = WarshallFloyd(adjL2, INF) -    for _ in range(Q): -        s, t = list(map(int, input().split())) +    for s, t in querys: -        if numss[s][t] == INF: +        cost = cost2ss[s][t] +        if cost == INF: -            anss.append(numss[s][t]) +            anss.append(cost - 1)