def solve():

    INF = float('inf')



    def Dijkstra(adjList, vSt):

        numV = len(adjList)

        useds = [False] * numV

        numUsed = 0

        costs = [INF] * numV

        costs[vSt] = 0

        while True:

            cNow, vNow = INF, -1

            for v in range(numV):

                if useds[v]: continue

                if costs[v] < cNow:

                    cNow, vNow = costs[v], v

            useds[vNow] = True

            numUsed += 1

            if numUsed == numV: break

            for v2, wt in adjList[vNow]:

                c2 = cNow + wt

                if c2 < costs[v2]:

                    costs[v2] = c2

        return costs



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



    costss = [Dijkstra(adjL, vSt) for vSt in range(N)]



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

    for i, costs in enumerate(costss):

        for j, cost in enumerate(costs[i+1:], i+1):

            if cost <= L:

                adjL2[i].append((j, 1))

                adjL2[j].append((i, 1))



    costss = [Dijkstra(adjL2, vSt) for vSt in range(N)]



    Q = int(eval(input()))

    anss = []

    for _ in range(Q):

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

        s, t = s-1, t-1

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

            anss.append(-1)

        else:

            anss.append(costss[s][t]-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()

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

    def Dijkstra(adjList, vSt):
        numV = len(adjList)
        useds = [False] * numV
        numUsed = 0
        costs = [INF] * numV
        costs[vSt] = 0
        while True:
            cNow, vNow = INF, -1
            for v in range(numV):
                if useds[v]:
                    continue
                if costs[v] < cNow:
                    cNow, vNow = costs[v], v
            useds[vNow] = True
            numUsed += 1
            if numUsed == numV:
                break
            for v2, wt in adjList[vNow]:
                c2 = cNow + wt
                if c2 < costs[v2]:
                    costs[v2] = c2
        return costs

    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))
    costss = [Dijkstra(adjL, vSt) for vSt in range(N)]
    adjL2 = [[] for _ in range(N)]
    for i, costs in enumerate(costss):
        for j, cost in enumerate(costs[i + 1 :], i + 1):
            if cost <= L:
                adjL2[i].append((j, 1))
                adjL2[j].append((i, 1))
    costss = [Dijkstra(adjL2, vSt) for vSt in range(N)]
    Q = int(eval(input()))
    anss = []
    for _ in range(Q):
        s, t = list(map(int, input().split()))
        s, t = s - 1, t - 1
        if costss[s][t] == INF:
            anss.append(-1)
        else:
            anss.append(costss[s][t] - 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()

-        numUsed = 0 -        costs = [INF] * numV -        costs[vSt] = 0 -        while True: -            cNow, vNow = INF, -1 -            for v in range(numV): -                if useds[v]: -                    continue -                if costs[v] < cNow: -                    cNow, vNow = costs[v], v -            useds[vNow] = True -            numUsed += 1 -            if numUsed == numV: -                break -            for v2, wt in adjList[vNow]: -                c2 = cNow + wt -                if c2 < costs[v2]: -                    costs[v2] = c2 -        return costs +        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 -    costss = [Dijkstra(adjL, vSt) for vSt in range(N)] -    adjL2 = [[] for _ in range(N)] -    for i, costs in enumerate(costss): -        for j, cost in enumerate(costs[i + 1 :], i + 1): -            if cost <= L: -                adjL2[i].append((j, 1)) -                adjL2[j].append((i, 1)) -    costss = [Dijkstra(adjL2, vSt) for vSt in range(N)] +    numss = [] +    for v in range(N): +        nums = Dijkstra(adjL, v) +        numss.append(nums) -        if costss[s][t] == INF: +        if numss[s][t] == INF: -            anss.append(costss[s][t] - 1) +            anss.append(numss[s][t])