import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
i8 = np.int64
i4 = np.int32


def main():
    stdin = open('/dev/stdin')
    pin = np.fromstring(stdin.read(), i8, sep=' ')
    N = pin[0]
    M = pin[1]
    L = pin[2]
    ABC = pin[3: M * 3 + 3].reshape((-1, 3))
    A = ABC[:, 0] - 1
    B = ABC[:, 1] - 1
    C = ABC[:, 2]
    Q = pin[M * 3 + 3]
    st = (pin[M * 3 + 4: 2 * Q + M * 3 + 4] - 1).reshape((-1, 2))

    graph = csr_matrix((C, (A, B)), shape=(N, N))
    dist = np.array(floyd_warshall(graph, directed=False))
    dist[dist > L + 0.5] = 0
    dist[dist > 0] = 1
    min_dist = np.array(floyd_warshall(dist))
    min_dist[min_dist == np.inf] = 0
    for i in range(st.shape[0]):
        print(int(min_dist[st[i, 0], st[i, 1]] - 1.0))


if __name__ == '__main__':
    main() import numpy as np numpy from scipy.sparse import csr_matrix from scipy.sparse.csgraph import floyd_warshall i8 = np.int64 i8 np.int64 np int64 i4 = np.int32 i4 np.int32 np int32 def main():
    stdin = open('/dev/stdin')
    pin = np.fromstring(stdin.read(), i8, sep=' ')
    N = pin[0]
    M = pin[1]
    L = pin[2]
    ABC = pin[3: M * 3 + 3].reshape((-1, 3))
    A = ABC[:, 0] - 1
    B = ABC[:, 1] - 1
    C = ABC[:, 2]
    Q = pin[M * 3 + 3]
    st = (pin[M * 3 + 4: 2 * Q + M * 3 + 4] - 1).reshape((-1, 2))

    graph = csr_matrix((C, (A, B)), shape=(N, N))
    dist = np.array(floyd_warshall(graph, directed=False))
    dist[dist > L + 0.5] = 0
    dist[dist > 0] = 1
    min_dist = np.array(floyd_warshall(dist))
    min_dist[min_dist == np.inf] = 0
    for i in range(st.shape[0]):
        print(int(min_dist[st[i, 0], st[i, 1]] - 1.0)) main stdin = open('/dev/stdin') stdin open('/dev/stdin') open '/dev/stdin' pin = np.fromstring(stdin.read(), i8, sep=' ') pin np.fromstring(stdin.read(), i8, sep=' ') np.fromstring np fromstring stdin.read() stdin.read stdin read i8 sep=' ' sep ' ' N = pin[0] N pin[0] pin 0 M = pin[1] M pin[1] pin 1 L = pin[2] L pin[2] pin 2 ABC = pin[3: M * 3 + 3].reshape((-1, 3)) ABC pin[3: M * 3 + 3].reshape((-1, 3)) [3: M * 3 + 3].reshape [3: M * 3 + 3] pin 3: M * 3 + 3 3 M * 3 + 3 M * 3 M 3 3 reshape (-1, 3) -1 3 A = ABC[:, 0] - 1 A ABC[:, 0] - 1 ABC[:, 0] ABC : 1 B = ABC[:, 1] - 1 B ABC[:, 1] - 1 ABC[:, 1] ABC : 1 C = ABC[:, 2] C ABC[:, 2] ABC : Q = pin[M * 3 + 3] Q pin[M * 3 + 3] pin M * 3 + 3 M * 3 M 3 3 st = (pin[M * 3 + 4: 2 * Q + M * 3 + 4] - 1).reshape((-1, 2)) st (pin[M * 3 + 4: 2 * Q + M * 3 + 4] - 1).reshape((-1, 2)) pin[M * 3 + 4: 2 * Q + M * 3 + 4] - 1).reshape pin[M * 3 + 4: 2 * Q + M * 3 + 4] - 1 pin[M * 3 + 4: 2 * Q + M * 3 + 4] pin M * 3 + 4: 2 * Q + M * 3 + 4 M * 3 + 4 M * 3 M 3 4 2 * Q + M * 3 + 4 2 * Q + M * 3 2 * Q 2 Q M * 3 M 3 4 1 reshape (-1, 2) -1 2 graph = csr_matrix((C, (A, B)), shape=(N, N)) graph csr_matrix((C, (A, B)), shape=(N, N)) csr_matrix (C, (A, B)) C (A, B) A B shape=(N, N) shape (N, N) N N dist = np.array(floyd_warshall(graph, directed=False)) dist np.array(floyd_warshall(graph, directed=False)) np.array np array floyd_warshall(graph, directed=False) floyd_warshall graph directed=False directed False dist[dist > L + 0.5] = 0 dist[dist > L + 0.5] dist dist > L + 0.5 dist L + 0.5 L 0.5 0 dist[dist > 0] = 1 dist[dist > 0] dist dist > 0 dist 0 1 min_dist = np.array(floyd_warshall(dist)) min_dist np.array(floyd_warshall(dist)) np.array np array floyd_warshall(dist) floyd_warshall dist min_dist[min_dist == np.inf] = 0 min_dist[min_dist == np.inf] min_dist min_dist == np.inf min_dist np.inf np inf 0 for i in range(st.shape[0]):
        print(int(min_dist[st[i, 0], st[i, 1]] - 1.0)) i range(st.shape[0]) range st.shape[0] st.shape st shape 0 print(int(min_dist[st[i, 0], st[i, 1]] - 1.0)) print int(min_dist[st[i, 0], st[i, 1]] - 1.0) int min_dist[st[i, 0], st[i, 1]] - 1.0 min_dist[st[i, 0], st[i, 1]] min_dist st[i, 0] st i 1.0 if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    stdin = open('/dev/stdin')
    pin = np.fromstring(stdin.read(), i8, sep=' ')
    N = pin[0]
    M = pin[1]
    L = pin[2]
    ABC = pin[3: M * 3 + 3].reshape((-1, 3))
    A = ABC[:, 0] - 1
    B = ABC[:, 1] - 1
    C = ABC[:, 2]
    Q = pin[M * 3 + 3]
    st = (pin[M * 3 + 4: 2 * Q + M * 3 + 4] - 1).reshape((-1, 2))

    graph = csr_matrix((C, (A, B)), shape=(N, N))
    dist = np.array(floyd_warshall(graph, directed=False))
    dist[dist > L + 0.5] = 0
    dist[dist > 0] = 1
    min_dist = np.array(floyd_warshall(dist))
    min_dist[min_dist == np.inf] = 0
    for i in range(st.shape[0]):
        print(int(min_dist[st[i, 0], st[i, 1]] - 1.0)) def main():
    stdin = open('/dev/stdin')
    pin = np.fromstring(stdin.read(), i8, sep=' ')
    N = pin[0]
    M = pin[1]
    L = pin[2]
    ABC = pin[3: M * 3 + 3].reshape((-1, 3))
    A = ABC[:, 0] - 1
    B = ABC[:, 1] - 1
    C = ABC[:, 2]
    Q = pin[M * 3 + 3]
    st = (pin[M * 3 + 4: 2 * Q + M * 3 + 4] - 1).reshape((-1, 2))

    graph = csr_matrix((C, (A, B)), shape=(N, N))
    dist = np.array(floyd_warshall(graph, directed=False))
    dist[dist > L + 0.5] = 0
    dist[dist > 0] = 1
    min_dist = np.array(floyd_warshall(dist))
    min_dist[min_dist == np.inf] = 0
    for i in range(st.shape[0]):
        print(int(min_dist[st[i, 0], st[i, 1]] - 1.0)) main i4 = np.int32 np.int32 i4 i8 = np.int64 np.int64 i8
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
i8 = np.int64
i4 = np.int32


def main():
    stdin = open('/dev/stdin')
    pin = np.fromstring(stdin.read(), i8, sep=' ')
    N = pin[0]
    M = pin[1]
    L = pin[2]
    ABC = pin[3: M * 3 + 3].reshape((-1, 3))
    A = ABC[:, 0] - 1
    B = ABC[:, 1] - 1
    C = ABC[:, 2]
    Q = pin[M * 3 + 3]
    st = (pin[M * 3 + 4: 2 * Q + M * 3 + 4] - 1).reshape((-1, 2))

    graph = csr_matrix((C, (A, B)), shape=(N, N))
    dist = np.array(floyd_warshall(graph, directed=False))
    dist[dist > L + 0.5] = 0
    dist[dist > 0] = 1
    min_dist = np.array(floyd_warshall(dist))
    min_dist[min_dist == np.inf] = 0
    for i in range(st.shape[0]):
        print(int(min_dist[st[i, 0], st[i, 1]] - 1.0))


if __name__ == '__main__':
    main()
