@staticmethod
def d():
    (n, m) = map(int, sys.stdin.readline().split())
    uf = GeometryTopology.UnionFind(n=n)
    queue = []
    for _ in range(m):
        (a, b, y) = map(int, sys.stdin.readline().split())
        heappush(queue, ((- (2 * y)), (a - 1), (b - 1)))
    q = int(sys.stdin.readline().rstrip())
    for i in range(q):
        (v, y) = map(int, sys.stdin.readline().split())
        heappush(queue, ((- ((2 * y) + 1)), (v - 1), i))
    res = ([None] * q)
    while queue:
        (y, i, j) = heappop(queue)
        if (y & 1):
            res[j] = uf.size[uf.find_root(i)]
        else:
            uf.unite(i, j)
    print(*res, sep='\n')
@staticmethod
def d():
    (n, m) = map(int, sys.stdin.readline().split())
    uf = GeometryTopology.UnionFind(n=n)
    queue = []
    for _ in range(m):
        (a, b, y) = map(int, sys.stdin.readline().split())
        heappush(queue, ((- (2 * y)), (a - 1), (b - 1)))
    q = int(sys.stdin.readline().rstrip())
    for i in range(q):
        (v, y) = map(int, sys.stdin.readline().split())
        heappush(queue, ((- ((2 * y) + 1)), (v - 1), i))
    res = ([None] * q)
    while queue:
        (y, i, j) = heappop(queue)
        if (y & 1):
            res[j] = uf.size[uf.find_root(i)]
        else:
            uf.unite(i, j)
    print(*res, sep='\n')