import sys
input = [int(x) for x in sys.stdin.read().split()][::-1].pop


def solve():
    n = input()
    s = input()
    graph = [{} for _ in range(n)]
    for _ in range(n-1):
        x = input()
        y = input()
        z = input()
        graph[x-1][y-1] = z
        graph[y-1][x-1] = z
       
    # Do DFS to find the farthest node from 0
    stack: list[tuple[int, int]] = [(0, 0)]
    visited: list[bool] = [False] * n
    visited[0] = True
    max_dist: int = 0
    max_node: int = 0
    while stack:
        node, dist = stack.pop()
        if dist > max_dist:
            max_dist = dist
            max_node = node
        for neighbor, weight in graph[node].items():
            if not visited[neighbor]:
                visited[neighbor] = True
                stack.append((neighbor, dist + weight))

    # Do DFS to find the farthest node from max_node and get the path between them
    stack = [(max_node, 0)]
    visited = [False] * n
    visited[max_node] = True
    parent: list[int] = [-1] * n
    max_dist = 0
    second_max_node = 0
    while stack:
        node, dist = stack.pop()
        if dist > max_dist:
            max_dist = dist
            second_max_node = node
        for neighbor, weight in graph[node].items():
            if not visited[neighbor]:
                visited[neighbor] = True
                stack.append((neighbor, dist + weight))
                parent[neighbor] = node

    longest_path: list[int] = []
    curr = second_max_node
    while curr != -1:
        longest_path.append(curr)
        curr = parent[curr]

    l: int = 0
    r: int = len(longest_path) - 1
    sum_left: int = 0
    sum_right: int = 0
    while l < r:
        if sum_left < sum_right:
            sum_left += graph[longest_path[l]][longest_path[l+1]]
            l += 1
        else:
            sum_right += graph[longest_path[r]][longest_path[r-1]]
            r -= 1

    while l > 0 or r < len(longest_path) - 1:
        if sum_left == 0 and sum_right == 0:
            break
        if sum_left < sum_right:
            # nullify the right side
            if graph[longest_path[r]][longest_path[r+1]] <= s:
                s -= graph[longest_path[r]][longest_path[r+1]]
                sum_right -= graph[longest_path[r]][longest_path[r+1]]
                r += 1
            else:
                break
        else:
            # nullify the left side
            if graph[longest_path[l]][longest_path[l-1]] <= s:
                s -= graph[longest_path[l]][longest_path[l-1]]
                sum_left -= graph[longest_path[l]][longest_path[l-1]]
                l -= 1
            else:
                break

    ans: int = max(sum_left, sum_right)

    # Do DFS from path to get longest distance
    visited = [False] * n
    for node in longest_path:
        visited[node] = True
    stack = [(node, 0) for node in longest_path]
    while stack:
        node, dist = stack.pop()
        if dist > ans:
            ans = dist
        for neighbor, weight in graph[node].items():
            if not visited[neighbor]:
                visited[neighbor] = True
                stack.append((neighbor, dist + weight))

    print(ans)


if __name__ == "__main__":
    n_tests: int = input()
    for test_nb in range(n_tests):
        solve()
