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 = map(int, input().split())
    adjL = [[] for _ in range(N)]
    for _ in range(M):
        A, B, C = 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(input())
    anss = []
    for _ in range(Q):
        s, t = 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
        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 = map(int, input().split())
    adjL = [[] for _ in range(N)]
    for _ in range(M):
        A, B, C = 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(input())
    anss = []
    for _ in range(Q):
        s, t = 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 INF = float('inf') INF float('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

     Dijkstra numV = len(adjList) numV len(adjList) len adjList useds = [False] * numV useds [False] * numV [False] False numV numUsed = 0 numUsed 0 costs = [INF] * numV costs [INF] * numV [INF] INF numV costs[vSt] = 0 costs[vSt] 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
         True cNow, vNow = INF, -1 cNow INF vNow -1 for v in range(numV):
                if useds[v]: continue
                if costs[v] < cNow:
                    cNow, vNow = costs[v], v
             v range(numV) range numV if useds[v]: continue
                 useds[v] useds v if costs[v] < cNow:
                    cNow, vNow = costs[v], v
             costs[v] < cNow costs[v] costs v cNow cNow, vNow = costs[v], v cNow costs[v] costs v vNow v useds[vNow] = True useds[vNow] useds vNow True numUsed += 1 numUsed 1 if numUsed == numV: break
             numUsed == numV numUsed numV for v2, wt in adjList[vNow]:
                c2 = cNow + wt
                if c2 < costs[v2]:
                    costs[v2] = c2
         v2 wt adjList[vNow] adjList vNow c2 = cNow + wt c2 cNow + wt cNow wt if c2 < costs[v2]:
                    costs[v2] = c2
         c2 < costs[v2] c2 costs[v2] costs v2 costs[v2] = c2 costs[v2] costs v2 c2 return costs costs adjList adjList vSt vSt N, M, L = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M L [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] adjL = [[] for _ in range(N)] adjL [[] for _ in range(N)] for _ in range(M):
        A, B, C = map(int, input().split())
        A, B = A-1, B-1
        adjL[A].append((B, C))
        adjL[B].append((A, C))

     _ range(M) range M A, B, C = map(int, input().split()) A map(int, input().split()) map int input().split() ().split () input split B C A, B = A-1, B-1 A A-1 A 1 B B-1 B 1 adjL[A].append((B, C)) [A].append [A] adjL A append (B, C) B C adjL[B].append((A, C)) [B].append [B] adjL B append (A, C) A C Dijkstra(adjL, vSt) for vSt in range(N) for vSt in range(N) vSt range(N) range N for vSt in range(N) Dijkstra(adjL, vSt) Dijkstra adjL vSt costss = [Dijkstra(adjL, vSt) for vSt in range(N)] costss [Dijkstra(adjL, vSt) for vSt in range(N)] [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] adjL2 = [[] for _ 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))

     i costs enumerate(costss) 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))

     j cost enumerate(costs[i+1:], i+1) enumerate costs[i+1:] costs i+1: i+1 i 1 i+1 i 1 if cost <= L:
                adjL2[i].append((j, 1))
                adjL2[j].append((i, 1))

     cost <= L cost L adjL2[i].append((j, 1)) [i].append [i] adjL2 i append (j, 1) j 1 adjL2[j].append((i, 1)) [j].append [j] adjL2 j append (i, 1) i 1 Dijkstra(adjL2, vSt) for vSt in range(N) for vSt in range(N) vSt range(N) range N for vSt in range(N) Dijkstra(adjL2, vSt) Dijkstra adjL2 vSt costss = [Dijkstra(adjL2, vSt) for vSt in range(N)] costss [Dijkstra(adjL2, vSt) for vSt in range(N)] Q = int(input()) Q int(input()) int input() input anss = [] anss [] for _ in range(Q):
        s, t = 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)

     _ range(Q) range Q s, t = map(int, input().split()) s map(int, input().split()) map int input().split() ().split () input split t s, t = s-1, t-1 s s-1 s 1 t t-1 t 1 if costss[s][t] == INF:
            anss.append(-1)
        else:
            anss.append(costss[s][t]-1)

     costss[s][t] == INF costss[s][t] [s] costss s t INF anss.append(-1) anss.append anss append -1 anss.append(costss[s][t]-1) anss.append anss append costss[s][t]-1 costss[s][t] [s] costss s t 1 print('\n'.join(map(str, anss))) print '\n'.join(map(str, anss)) '\n'.join '\n' join map(str, anss) map str anss solve() 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 = map(int, input().split())
    adjL = [[] for _ in range(N)]
    for _ in range(M):
        A, B, C = 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(input())
    anss = []
    for _ in range(Q):
        s, t = 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))) 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 = map(int, input().split())
    adjL = [[] for _ in range(N)]
    for _ in range(M):
        A, B, C = 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(input())
    anss = []
    for _ in range(Q):
        s, t = 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
        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 = map(int, input().split())
    adjL = [[] for _ in range(N)]
    for _ in range(M):
        A, B, C = 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(input())
    anss = []
    for _ in range(Q):
        s, t = 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()
