import java.util.Scanner;

public class JavaApplication31 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] t = new int[n];
        int[] a = new int[n];

        for (int i = 0; i < n; i++) {
            t[i] = scan.nextInt();
        }
        int max = t[n - 1];
        a[n - 1] = 0;
        for (int i = n - 2; i >= 0; i--) {

            if (t[i] > max) {
                max = t[i];
                t[i] = 0;
            } else if (t[i] == max) {
                a[i] = 1;
            } else {
                a[i] = max - t[i] + 1;
            }
        }
        for (int i = 0; i < n; i++) {
            System.out.print(a[i] + " ");
        }

    }

}


import java.util.Scanner;

public class JavaApplication31 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] t = new int[n];
        int[] a = new int[n];
        int imax = 0;
        for (int i = 0; i < n; i++) {
            t[i] = scan.nextInt();
            if (t[i] > t[imax]) {
                imax = i;
            }
        }
        int p = 0;
        while (imax <= n - 1) {
            remplir(a, t, p, imax);
            p = imax;
            imax = maximum(t, imax + 1);

        }

        int j;
        for (j = 0; j < n; j++) {
            System.out.print(a[j] + " ");
        }

    }

    public static void remplir(int a[], int t[], int posInit, int posFinal) {
        for (int i = posInit; i < posFinal; i++) {
            a[i] = t[posFinal] - t[i] + 1;
        }
    }

    public static int maximum(int t[], int posInit) {
        int imax = posInit;
        for (int i = posInit + 1; i < t.length; i++) {
            if (t[imax] < t[i]) {
                imax = i;
            }
        }
        return imax;
    }
}


import java.util.Scanner;

public class JavaApplication31 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] t = new int[n];
        int[] a = new int[n];
        int imax = 0;
        for (int i = 0; i < n; i++) {
            t[i] = scan.nextInt();
            if (t[i] > t[imax]) {
                imax = i;
            }
        }
        int p = 0;
        while (imax <= n - 1) {
            remplir(a, t, p, imax);
            p = imax + 1;
            imax = maximum(t, imax + 1);

        }

        int j;
        for (j = 0; j < n; j++) {
            System.out.print(a[j] + " ");
        }

    }

    public static void remplir(int a[], int t[], int posInit, int posFinal) {
        for (int i = posInit; i < posFinal; i++) {
            a[i] = t[posFinal] - t[i] + 1;
        }
    }

    public static int maximum(int t[], int posInit) {
        int imax = posInit;
        for (int i = posInit + 1; i < t.length; i++) {
            if (t[imax] < t[i]) {
                imax = i;
            }
        }
        return imax;
    }
}


import java.util.Scanner;

public class exp
{
    public static void main(String args[])
    {long timei = System.currentTimeMillis();
    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    long [] h = new long[n];

    for(int i=0;i<n;i++)
        {
        h[i]  = s.nextLong();
        }
    long[] output= new long[n];
    long maxh=h[n-1];
    output[n-1]=0L;

    for(int i=n-2;i>=0;i--)
        {
        if(h[i]>maxh)
            {
            output[i]=0L;
            maxh =h[i];
            }
        else
            {
            output[i]=maxh-h[i]+1L;
            }

        }

    for(int i=0;i<n;i++)
        System.out.print(output[i]+" ");

    //time analysis
    long timef = System.currentTimeMillis();
    //  System.out.println("total execution time:"+(timef-timei));
    }

}


import java.util.Scanner;

public class luxury
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);

int n = s.nextInt();

long[] h =new long[n];

for(int i=0;i<n;i++)
    {
        h[i]= s.nextInt();
    }
//input ready
long[] output = new long[n];
long maxh = max(0,h);

for(int i=0;i<n;i++)
    {
    if(h[i]==maxh)
        {
        output[i]=0;
        maxh= max(i+1,h);
        }
    else
        output[i]= maxh-h[i]+1;
    }

//output stream
for(int i=0;i<n;i++)
    System.out.print(output[i]+" ");

}

    private static long max(int i,long[] ary)
    {
    long temp = ary[i];

    for(int u = i;i<ary.length;i++)
        {
        if(temp < ary[u]) temp = ary[u];
        }
        return temp;
    }

}


import java.util.Scanner;

public class exp
{
    public static void main(String args[])
    {long timei = System.currentTimeMillis();
    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    long [] h = new long[n];

    for(int i=0;i<n;i++)
        {
        h[i]  = s.nextLong();
        }
    String output = "";
    long maxh=h[n-1];

    for(int i=n-1;i>=0;i--)
        {
        if(h[i]>maxh)
            {
            output= "0 ".concat(output);
            maxh =h[i];
            }
        else
            {
            output= ((maxh-h[i]+1)+" ").concat(output);
            }

        }

    System.out.println(output);

    //time analysis
    long timef = System.currentTimeMillis();
    System.out.println("total execution time:"+(timef-timei));
    }

}


import java.util.Scanner;

public class luxury
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);

int n = s.nextInt();

long[] h =new long[n];

for(int i=0;i<n;i++)
    {
        h[i]= s.nextInt();
    }
//input ready
long maxh = max(0,h);

for(int i=0;i<n;i++)
    {
    if(h[i]==maxh)
        {
        System.out.print(0+" ");
        maxh= max(i+1,h);
        }
    else
        System.out.print((maxh-h[i]+1)+" ");
    }

//output stream
}

    private static long max(int i,long[] ary)
    {
    long temp;
    if(i<ary.length)
        temp = ary[i];

    else return ary[i-1];
    for(int u = i;u<ary.length;u++)
        {
        if(temp < ary[u]) temp = ary[u];
        }
        return temp;
    }

}


import sys
import bisect
from itertools import accumulate
from collections import defaultdict
input = sys.stdin.buffer.readline

def tup(u, v):
    if u < v:
        u, v = v, u
    return u * 10 ** 6 + v

def untup(n):
    return divmod(n, 10 ** 6)

def tup3(w, u, v):
    return - (w * 10 ** 12 + u * 10 ** 6 + v)

def untup3(n):
    n = -n
    w, t = divmod(n, 10 ** 12)
    u, v = divmod(t, 10 ** 6)
    return w, u, v

t = int(input())
for _ in range(t):
    n, S = map(int, input().split())
    adj = [dict() for _ in range(n+1)]
    for _ in range(n-1):
        u, v, w = map(int, input().split())
        adj[u][v] = w
        adj[v][u] = w
    path_count = defaultdict(int)
    stack = [1]
    parent = [0] * (n+1)
    parent[1] = 1
    last_node = 0
    while stack:
        node = stack.pop()
        leaf = True
        for next_node in adj[node].keys():
            if parent[next_node] == 0:
                parent[next_node] = node
                stack.append(next_node)
                leaf = False

        if leaf:
            pos = node
            while pos != 1:
                path_count[tup(pos, parent[pos])] += 1
                pos = parent[pos]

        last_node = node

    moves = []
    s = 0
    for t, c in path_count.items():
        u, v = untup(t)
        w = adj[u][v]
        s += w * c
        while w > 0:
            moves.append((w - w // 2) * c)
            w //= 2

    moves.sort(reverse = True)
    csum = list(accumulate([0] + moves))

    print(bisect.bisect_left(csum, s - S))


import sys
from heapq import heappop, heapify
from collections import defaultdict
input = sys.stdin.buffer.readline

t = int(input())
for _ in range(t):
    n, S = map(int, input().split())
    adj = [dict() for _ in range(n+1)]
    for _ in range(n-1):
        u, v, w = map(int, input().split())
        adj[u][v] = w
        adj[v][u] = w

    stack = [1]
    dp = [0] * (n + 1)
    dfs_order = []
    parent = [0] * (n+1)
    parent[1] = 1
    while stack:
        node = stack.pop()
        dfs_order.append(node)
        leaf = True
        for next_node in adj[node].keys():
            if parent[next_node] == 0:
                parent[next_node] = node
                stack.append(next_node)
                leaf = False
        if leaf:
            dp[node] = 1

    for node in reversed(dfs_order):
        for next_node in adj[node].keys():
            if next_node != parent[node]:
                dp[node] += dp[next_node]

    moves = []
    s = 0
    for v, c in enumerate(dp):
        if v == 0 or v == 1:
            continue
        u = parent[v]
        w = adj[u][v]
        s += w * c
        while w > 0:
            moves.append((w//2 - w) * c)
            w //= 2

    heapify(moves)
    ans = 0
    while s - S > 0:
        s += heappop(moves)
        ans += 1

    print(ans)


import sys
import heapq
from collections import defaultdict
input = sys.stdin.buffer.readline

def tup(u, v):
    if u < v:
        u, v = v, u
    return u * 10 ** 6 + v

def untup(n):
    return divmod(n, 10 ** 6)

t = int(input())
for _ in range(t):
    n, S = map(int, input().split())
    adj = [dict() for _ in range(n+1)]
    for _ in range(n-1):
        u, v, w = map(int, input().split())
        adj[u][v] = w
        adj[v][u] = w
    path_count = defaultdict(int)
    stack = [1]
    parent = [0] * (n+1)
    parent[1] = 1
    last_node = 0
    while stack:
        node = stack.pop()
        leaf = True
        for next_node in adj[node].keys():
            if parent[next_node] == 0:
                parent[next_node] = node
                stack.append(next_node)
                leaf = False

        if leaf:
            pos = node
            while pos != 1:
                path_count[tup(pos, parent[pos])] += 1
                pos = parent[pos]

        last_node = node

    pq = []
    for t, c in path_count.items():
        u, v = untup(t)
        pq.append(-(adj[u][v] * c * 10 ** 6 + adj[u][v]))
    heapq.heapify(pq)
    s = sum([-elem // 10 ** 6 for elem in pq])
    count = 0
    while s > S:
        n = -heapq.heappop(pq)
        wsum, w = divmod(n, 10 ** 6)
        c = wsum // w
        s -= w * c
        w //= 2
        s += w * c
        heapq.heappush(pq, -((w * c) * 10 ** 6 + w))
        count += 1
    print(count)


import sys
import bisect
from itertools import accumulate
from collections import defaultdict
input = sys.stdin.buffer.readline

def tup(u, v):
    if u < v:
        u, v = v, u
    return u * 10 ** 6 + v

def untup(n):
    return divmod(n, 10 ** 6)

t = int(input())
for _ in range(t):
    n, S = map(int, input().split())
    adj = [dict() for _ in range(n+1)]
    for _ in range(n-1):
        u, v, w = map(int, input().split())
        adj[u][v] = w
        adj[v][u] = w
    path_count = defaultdict(int)
    stack = [1]
    parent = [0] * (n+1)
    parent[1] = 1
    last_node = 0
    while stack:
        node = stack.pop()
        leaf = True
        for next_node in adj[node].keys():
            if parent[next_node] == 0:
                parent[next_node] = node
                stack.append(next_node)
                leaf = False

        if leaf:
            pos = node
            while pos != 1:
                path_count[tup(pos, parent[pos])] += 1
                pos = parent[pos]

        last_node = node

    moves = defaultdict(int)
    s = 0
    for t, c in path_count.items():
        u, v = untup(t)
        w = adj[u][v]
        s += w * c
        while w > 0:
            moves[(w - w // 2) * c] += 1
            w //= 2

    moves_list = list(moves.items())
    moves_list.sort(reverse = True)
    csum = list(accumulate([0] + [w * c for w, c in moves_list]))
    index = bisect.bisect_left(csum, s - S)
    if index == 0:
        print(0)
    else:
        count = sum(map(lambda t: t[1], moves_list[:index-1]))
        rest = s - S - csum[index-1]
        count += -((-rest) // moves_list[index-1][0])
        print(count)


import sys
import heapq
from collections import defaultdict
input = sys.stdin.buffer.readline

def tup(u, v):
    if u < v:
        u, v = v, u
    return u * 10 ** 6 + v

def untup(n):
    return divmod(n, 10 ** 6)

t = int(input())
for _ in range(t):
    n, S = map(int, input().split())
    adj = [dict() for _ in range(n+1)]
    for _ in range(n-1):
        u, v, w = map(int, input().split())
        adj[u][v] = w
        adj[v][u] = w
    path_count = defaultdict(int)
    stack = [1]
    parent = [0] * (n+1)
    parent[1] = 1
    last_node = 0
    while stack:
        node = stack.pop()
        leaf = True
        for next_node in adj[node].keys():
            if parent[next_node] == 0:
                parent[next_node] = node
                stack.append(next_node)
                leaf = False

        if leaf:
            pos = node
            while pos != 1:
                path_count[tup(pos, parent[pos])] += 1
                pos = parent[pos]

        last_node = node

    pq = []
    for t, c in path_count.items():
        u, v = untup(t)
        pq.append(-adj[u][v] * c)
    heapq.heapify(pq)
    s = -sum(pq)
    count = 0
    while s > S:
        w = -heapq.heappop(pq)
        s -= w
        w //= 2
        s += w
        heapq.heappush(pq, -w)
        count += 1
    print(count)


import sys
import heapq
import random
from collections import defaultdict
input = sys.stdin.buffer.readline

t = int(input())
for _ in range(t):
    #"""
    n, S = map(int, input().split())
    adj = [dict() for _ in range(n+1)]
    for _ in range(n-1):
        u, v, w = map(int, input().split())
        adj[u][v] = w
        adj[v][u] = w
    """
    n = 10 ** 5
    S = 1
    adj = [dict() for _ in range(n+1)]
    connect = list(range(1, n+1))
    random.shuffle(connect)
    for i in range(1, n):
        j = random.randint(0, i-1)
        w = random.randint(1, 10 ** 6)
        u, v = connect[i], connect[j]
        adj[u][v] = w
        adj[v][u] = w
    #"""
    stack = [1]
    dp = [0] * (n + 1)
    dfs_order = []
    parent = [0] * (n+1)
    parent[1] = 1
    while stack:
        node = stack.pop()
        dfs_order.append(node)
        leaf = True
        for next_node in adj[node].keys():
            if parent[next_node] == 0:
                parent[next_node] = node
                stack.append(next_node)
                leaf = False
        if leaf:
            dp[node] = 1

    for node in reversed(dfs_order):
        for next_node in adj[node].keys():
            if next_node != parent[node]:
                dp[node] += dp[next_node]

    pq = []
    s = 0
    for v, c in enumerate(dp):
        if v == 0 or v == 1:
            continue
        u = parent[v]
        w = adj[u][v]
        s += w * c
        pq.append(-((w - w//2) * c * 10 ** 12 +  w * 10 ** 6 + c))
    heapq.heapify(pq)
    count = 0
    while s > S:
        n = -heapq.heappop(pq)
        t, c = divmod(n, 10 ** 6)
        dec, w = divmod(t, 10 ** 6)
        s -= dec
        w //= 2
        new_dec = (w - w//2) * c
        heapq.heappush(pq, -(new_dec * 10 ** 12 + w * 10 ** 6 + c))
        count += 1
    print(count)


import sys
import heapq
import random
from collections import defaultdict
input = sys.stdin.buffer.readline

t = int(input())
for _ in range(t):
    #"""
    n, S = map(int, input().split())
    adj = [dict() for _ in range(n+1)]
    for _ in range(n-1):
        u, v, w = map(int, input().split())
        adj[u][v] = w
        adj[v][u] = w
    """
    n = 10 ** 5
    S = 1
    adj = [dict() for _ in range(n+1)]
    connect = list(range(1, n+1))
    random.shuffle(connect)
    for i in range(1, n):
        j = random.randint(0, i-1)
        w = random.randint(1, 10 ** 6)
        u, v = connect[i], connect[j]
        adj[u][v] = w
        adj[v][u] = w
    #"""
    path_count = defaultdict(int)
    stack = [1]
    parent = [0] * (n+1)
    parent[1] = 1
    while stack:
        node = stack.pop()
        leaf = True
        for next_node in adj[node].keys():
            if parent[next_node] == 0:
                parent[next_node] = node
                stack.append(next_node)
                leaf = False

        if leaf:
            pos = node
            while pos != 1:
                path_count[pos * 10 ** 6 + parent[pos]] += 1
                pos = parent[pos]
    pq = []
    s = 0
    for t, c in path_count.items():
        u, v = divmod(t, 10 ** 6)
        w = adj[u][v]
        s += w * c
        pq.append(-((w - w//2) * c * 10 ** 12 +  w * 10 ** 6 + c))
    heapq.heapify(pq)
    count = 0
    while s > S:
        n = -heapq.heappop(pq)
        t, c = divmod(n, 10 ** 6)
        dec, w = divmod(t, 10 ** 6)
        s -= dec
        w //= 2
        new_dec = (w - w//2) * c
        heapq.heappush(pq, -(new_dec * 10 ** 12 + w * 10 ** 6 + c))
        count += 1
    print(count)


"""
NTC here
"""

#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
# from functools import lru_cache
# sys.setrecursionlimit(10**6)

# recusive with optimization
# from types import GeneratorType

# def bootstrap(f, stack=[]):
#     def wrappedfunc(*args, **kwargs):
#         if stack:
#             return f(*args, **kwargs)
#         else:
#             to = f(*args, **kwargs)
#             while True:
#                 if type(to) is GeneratorType:
#                     stack.append(to)
#                     to = next(to)
#                 else:
#                     stack.pop()
#                     if not stack:
#                         break
#                     to = stack[-1].send(to)
#             return to

#     return wrappedfunc

# def dfs(adj, S=1):
#     N = len(adj)
#     parent = [False]*N
#     dp = [0]*N
#     # dp[i] = number of leaves in i th vertex
#     edge_value = []
#     # store [w, number of leaves]
#     @bootstrap
#     def dfs_run(S):
#         # print(S, end='->')
#         ch = 0
#         leaves = 0
#         for v, w in adj[S]:
#             if parent[v]==False:
#                 ch = 1
#                 parent[v] = True
#                 lv = yield dfs_run(v)
#                 edge_value.append([w, lv])
#                 leaves += lv
#         # print(S, end='->')
#         if ch==0:
#             # leaf node
#             dp[S] = 1
#         else:
#             dp[S] = leaves
#         yield dp[S]
#     parent[S] = True
#     dfs_run(S)
#     # print(dp, edge_value)
#     return edge_value

def dfs(adj, start=1):
    n = len(adj)
    visited = [False]*n
    srt = [start]
    parent = [-1]*n
    dp = [0]*n
    # dp[i] = number of leaves in i th vertex
    edge_value = {}
    # store [w, number of leaves]
    while srt:

        v = srt.pop()
        # print(dp)
        # print(v, end="->")
        if visited[v]:
            if parent[v] != -1:
                S, w = parent[v]
                dp[S] += dp[v]
                if (v, S) not in edge_value:
                    edge_value[(v, S)] = [w, 0]
                edge_value[(v, S)][1] += dp[v]
                dp[v] = 0
            continue
        visited[v] = True
        if parent[v] != -1:
            srt.append(parent[v][0])
        count_child = 0
        for u, w in adj[v]:
            if not visited[u]:
                count_child = 1
                parent[u] = [v, w]
                srt.append(u)
        if count_child==0:
            # leaf node
            dp[v] = 1

        if parent[v] != -1:
            S, w = parent[v]
            dp[S] += dp[v]
            if (v, S) not in edge_value:
                edge_value[(v, S)] = [w, 0]
            edge_value[(v, S)][1] += dp[v]

        dp[v] = 0
    # print(edge_value)
    return list(edge_value.values())

import heapq as hq

def main():
    T = iin()
    while T:
        T-=1
        n, S = lin()
        adj = [[] for i in range(n+1)]
        for _ in range(n-1):
            i, j, w = lin()
            adj[i].append([j, w])
            adj[j].append([i, w])
        edge_val = dfs(adj)
        # print('\n*******************************')
        sm = sum([w*num for w, num in edge_val])
        edge_val = [ [(w//2)*num-w*num, w, num] for w, num in edge_val]
        hq.heapify(edge_val)
        Q = 0
        while sm>S:
            val, w, num = hq.heappop(edge_val)
            # print(val, w, num)
            sm = sm - (w*num) + ((w//2)*num)
            w //= 2
            if w>0:
                hq.heappush(edge_val, [(w//2)*num-w*num, w, num])
            Q += 1
        print(Q)

# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def iin(): return int(input())
def lin(): return list(map(int, input().split()))
# endregion

if __name__ == "__main__":
    main()


"""
NTC here
"""

#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
# from functools import lru_cache
# sys.setrecursionlimit(10**6)

# recusive with optimization
# from types import GeneratorType

# def bootstrap(f, stack=[]):
#     def wrappedfunc(*args, **kwargs):
#         if stack:
#             return f(*args, **kwargs)
#         else:
#             to = f(*args, **kwargs)
#             while True:
#                 if type(to) is GeneratorType:
#                     stack.append(to)
#                     to = next(to)
#                 else:
#                     stack.pop()
#                     if not stack:
#                         break
#                     to = stack[-1].send(to)
#             return to

#     return wrappedfunc

# def dfs(adj, S=1):
#     N = len(adj)
#     parent = [False]*N
#     dp = [0]*N
#     # dp[i] = number of leaves in i th vertex
#     edge_value = []
#     # store [w, number of leaves]
#     @bootstrap
#     def dfs_run(S):
#         # print(S, end='->')
#         ch = 0
#         leaves = 0
#         for v, w in adj[S]:
#             if parent[v]==False:
#                 ch = 1
#                 parent[v] = True
#                 lv = yield dfs_run(v)
#                 edge_value.append([w, lv])
#                 leaves += lv
#         # print(S, end='->')
#         if ch==0:
#             # leaf node
#             dp[S] = 1
#         else:
#             dp[S] = leaves
#         yield dp[S]
#     parent[S] = True
#     dfs_run(S)
#     # print(dp, edge_value)
#     return edge_value

def dfs(adj, start=1):
    n = len(adj)
    visited = [False]*n
    srt = [start]
    parent = [-1]*n
    dp = [0]*n
    # dp[i] = number of leaves in i th vertex
    edge_value = {}
    # store [w, number of leaves]
    while srt:

        v = srt.pop()
        # print(dp)
        # print(v, end="->")
        if visited[v]:
            if parent[v] != -1:
                S, w = parent[v]
                dp[S] += dp[v]
                if (v, S) not in edge_value:
                    edge_value[(v, S)] = [w, 0]
                edge_value[(v, S)][1] += dp[v]
                dp[v] = 0
            continue
        visited[v] = True
        if parent[v] != -1:
            srt.append(parent[v][0])
        count_child = 0
        for u, w in adj[v]:
            if not visited[u]:
                count_child = 1
                parent[u] = [v, w]
                srt.append(u)
        if count_child==0:
            # leaf node
            dp[v] = 1

        if parent[v] != -1:
            S, w = parent[v]
            dp[S] += dp[v]
            if (v, S) not in edge_value:
                edge_value[(v, S)] = [w, 0]
            edge_value[(v, S)][1] += dp[v]

        dp[v] = 0
    # print(edge_value)
    return list(edge_value.values())

import heapq as hq

def main():
    T = iin()
    while T:
        T-=1
        n, S = lin()
        adj = [[] for i in range(n+1)]
        for _ in range(n-1):
            i, j, w = lin()
            adj[i].append([j, w])
            adj[j].append([i, w])
        edge_val = dfs(adj)
        # print('\n*******************************')
        Q = 0
        ans = []
        for w, num in edge_val:
            x = w
            while x:
                ans.append(x*num - (x//2)*num)
                x//=2
        ans.sort()

        sm = sum([w*num for w, num in edge_val])

        while sm>S and ans:
            val = ans.pop()
            sm -= val
            Q += 1
        print(Q)

        # edge_val = [ [(w//2)*num-w*num, w, num] for w, num in edge_val]
        # hq.heapify(edge_val)
        # Q = 0
        # while sm>S:
        #     val, w, num = hq.heappop(edge_val)
        #     # print(val, w, num)
        #     sm = sm - (w*num) + ((w//2)*num)
        #     w //= 2
        #     if w>0:
        #         hq.heappush(edge_val, [(w//2)*num-w*num, w, num])
        #     Q += 1
        # print(Q)

# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def iin(): return int(input())
def lin(): return list(map(int, input().split()))
# endregion

if __name__ == "__main__":
    main()


"""
NTC here
"""

#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
# from functools import lru_cache
# sys.setrecursionlimit(10**6)

# recusive with optimization
from types import GeneratorType

def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to

    return wrappedfunc

def dfs(adj, S=1):
    N = len(adj)
    parent = [False]*N
    dp = [0]*N
    # dp[i] = number of leaves in i th vertex
    edge_value = []
    # store [w, number of leaves]
    @bootstrap
    def dfs_run(S):
        # print(S, end='->')
        ch = 0
        leaves = 0
        for v, w in adj[S]:
            if parent[v]==False:
                ch = 1
                parent[v] = True
                lv = yield dfs_run(v)
                edge_value.append([w, lv])
                leaves += lv
        # print(S, end='->')
        if ch==0:
            # leaf node
            dp[S] = 1
        else:
            dp[S] = leaves
        yield dp[S]
    parent[S] = True
    dfs_run(S)
    # print(dp, edge_value)
    return edge_value

import heapq as hq

def main():
    T = iin()
    while T:
        T-=1
        n, S = lin()
        adj = [[] for i in range(n+1)]
        for _ in range(n-1):
            i, j, w = lin()
            adj[i].append([j, w])
            adj[j].append([i, w])
        edge_val = dfs(adj)
        # print('\n*******************************')
        sm = sum([w*num for w, num in edge_val])
        edge_val = [ [-1*(w*num), w, num] for w, num in edge_val]
        hq.heapify(edge_val)
        Q = 0
        while sm>S:
            val, w, num = hq.heappop(edge_val)
            # print(val, w, num)
            sm = sm - (w*num) + ((w//2)*num)
            w //= 2
            if w:
                hq.heappush(edge_val, [-1*(w*num), w, num])
            Q += 1
        print(Q)

# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def iin(): return int(input())
def lin(): return list(map(int, input().split()))
# endregion

if __name__ == "__main__":
    main()


"""
NTC here
"""

#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
# from functools import lru_cache
# sys.setrecursionlimit(10**6)

# recusive with optimization
from types import GeneratorType

def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to

    return wrappedfunc

def dfs(adj, S=1):
    N = len(adj)
    parent = [False]*N
    dp = [0]*N
    # dp[i] = number of leaves in i th vertex
    edge_value = []
    # store [w, number of leaves]
    @bootstrap
    def dfs_run(S):
        # print(S, end='->')
        ch = 0
        leaves = 0
        for v, w in adj[S]:
            if parent[v]==False:
                ch = 1
                parent[v] = True
                lv = yield dfs_run(v)
                edge_value.append([w, lv])
                leaves += lv
        # print(S, end='->')
        if ch==0:
            # leaf node
            dp[S] = 1
        else:
            dp[S] = leaves
        yield dp[S]
    parent[S] = True
    dfs_run(S)
    # print(dp, edge_value)
    return edge_value

# def dfs(adj, start=1):
#     n = len(adj)
#     visited = [False]*n
#     srt = [start]
#     parent = [-1]*n
#     dp = [0]*n
#     # dp[i] = number of leaves in i th vertex
#     edge_value = {}
#     # store [w, number of leaves]
#     while srt:

#         v = srt.pop()
#         # print(dp)
#         # print(v, end="->")
#         if visited[v]:
#             if parent[v] != -1:
#                 S, w = parent[v]
#                 dp[S] += dp[v]
#                 if (v, S) not in edge_value:
#                     edge_value[(v, S)] = [w, 0]
#                 edge_value[(v, S)][1] += dp[v]
#                 dp[v] = 0
#             continue
#         visited[v] = True
#         if parent[v] != -1:
#             srt.append(parent[v][0])
#         count_child = 0
#         for u, w in adj[v]:
#             if not visited[u]:
#                 count_child = 1
#                 parent[u] = [v, w]
#                 srt.append(u)
#         if count_child==0:
#             # leaf node
#             dp[v] = 1

#         if parent[v] != -1:
#             S, w = parent[v]
#             dp[S] += dp[v]
#             if (v, S) not in edge_value:
#                 edge_value[(v, S)] = [w, 0]
#             edge_value[(v, S)][1] += dp[v]

#         dp[v] = 0
#     # print(edge_value)
#     return list(edge_value.values())

import heapq as hq

def main():
    T = iin()
    while T:
        T-=1
        n, S = lin()
        adj = [[] for i in range(n+1)]
        for _ in range(n-1):
            i, j, w = lin()
            adj[i].append([j, w])
            adj[j].append([i, w])
        edge_val = dfs(adj)
        # print('\n*******************************')
        Q = 0
        ans = []
        for w, num in edge_val:
            x = w
            while x:
                ans.append(x*num - (x//2)*num)
                x//=2
        ans.sort()

        sm = sum([w*num for w, num in edge_val])

        while sm>S and ans:
            val = ans.pop()
            sm -= val
            Q += 1
        print(Q)

        # edge_val = [ [(w//2)*num-w*num, w, num] for w, num in edge_val]
        # hq.heapify(edge_val)
        # Q = 0
        # while sm>S:
        #     val, w, num = hq.heappop(edge_val)
        #     # print(val, w, num)
        #     sm = sm - (w*num) + ((w//2)*num)
        #     w //= 2
        #     if w>0:
        #         hq.heappush(edge_val, [(w//2)*num-w*num, w, num])
        #     Q += 1
        # print(Q)

# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def iin(): return int(input())
def lin(): return list(map(int, input().split()))
# endregion

if __name__ == "__main__":
    main()


"""
NTC here
"""

#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
# from functools import lru_cache
# sys.setrecursionlimit(10**6)

# recusive with optimization
from types import GeneratorType

def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to

    return wrappedfunc

def dfs(adj, S=1):
    N = len(adj)
    parent = [False]*N
    dp = [0]*N
    # dp[i] = number of leaves in i th vertex
    edge_value = []
    # store [w, number of leaves]
    @bootstrap
    def dfs_run(S):
        # print(S, end='->')
        ch = 0
        leaves = 0
        for v, w in adj[S]:
            if parent[v]==False:
                ch = 1
                parent[v] = True
                lv = yield dfs_run(v)
                edge_value.append([w, lv])
                leaves += lv
        # print(S, end='->')
        if ch==0:
            # leaf node
            dp[S] = 1
        else:
            dp[S] = leaves
        yield dp[S]
    parent[S] = True
    dfs_run(S)
    # print(dp, edge_value)
    return edge_value

import heapq as hq

def main():
    T = iin()
    while T:
        T-=1
        n, S = lin()
        adj = [[] for i in range(n+1)]
        for _ in range(n-1):
            i, j, w = lin()
            adj[i].append([j, w])
            adj[j].append([i, w])
        edge_val = dfs(adj)
        # print('\n*******************************')
        sm = sum([w*num for w, num in edge_val])
        edge_val = [ [(w//2)*num-w*num, w, num] for w, num in edge_val]
        hq.heapify(edge_val)
        Q = 0
        while sm>S:
            val, w, num = hq.heappop(edge_val)
            # print(val, w, num)
            sm = sm - (w*num) + ((w//2)*num)
            w //= 2
            if w>0:
                hq.heappush(edge_val, [(w//2)*num-w*num, w, num])
            Q += 1
        print(Q)

# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def iin(): return int(input())
def lin(): return list(map(int, input().split()))
# endregion

if __name__ == "__main__":
    main()


from collections import deque

t = int(input().split()[0])
for case in range(t):
    n,s = map(int,input().split())
    graph = {}
    edgeToWeight = {}
    for i in range(1,n+1):
        graph[i] = []
    for i in range(n-1):
        u,v,w = map(int,input().split())
        graph[u].append(v)
        graph[v].append(u)
        edgeToWeight[(u,v)] = w
        edgeToWeight[(v,u)] = w
    nodeToParent = {}

    #BFS
    visited = [False]*(n+1)
    queue = deque()
    queue.append(1)
    visited[1] = True
    order = []
    while queue:
        curr = queue.popleft()
        order.append(curr)
        for neighbor in graph[curr]:
            if visited[neighbor] == False:
                nodeToParent[neighbor] = curr
                queue.append(neighbor)
                visited[neighbor] = True

    order.reverse()
    edgeFreq = {}
    for i in range(2,n+1):
        edgeFreq[i] = 0
    for node in order:
        if node == 1:
            continue
        if len(graph[node]) == 1:
            edgeFreq[node] = 1
        if nodeToParent[node] != 1:
            edgeFreq[nodeToParent[node]] += edgeFreq[node]

    ans = 0
    currSum = 0
    moves = []
    for i in range(2,n+1):
        wt = edgeToWeight[(i,nodeToParent[i])]
        currSum += wt*edgeFreq[i]
        while wt > 0:
            moves.append((wt - wt//2)*edgeFreq[i])
            wt = wt // 2
    if currSum > s:
        moves.sort()
    pt = len(moves)-1
    while currSum > s:
        currSum -= moves[pt]
        pt -= 1
        ans += 1
##    print(graph)
##    print(edgeToWeight)
##    print(edgeFreq)
##    print(nodeToParent)
##    print(moves)
    print(ans)


from collections import deque

t = int(input().split()[0])
for case in range(t):
    n,s = map(int,input().split())
    graph = {}
    edgeToWeight = {}
    for i in range(1,n+1):
        graph[i] = []
    for i in range(n-1):
        u,v,w = map(int,input().split())
        graph[u].append(v)
        graph[v].append(u)
        edgeToWeight[(u,v)] = w
        edgeToWeight[(v,u)] = w
    nodeToParent = {}

    #BFS
    visited = [False]*(n+1)
    queue = deque()
    queue.append(1)
    visited[1] = True
    order = []
    while queue:
        curr = queue.popleft()
        order.append(curr)
        for neighbor in graph[curr]:
            if visited[neighbor] == False:
                nodeToParent[neighbor] = curr
                queue.append(neighbor)
                visited[neighbor] = True

    order.reverse()
    edgeFreq = {}
    for i in range(2,n+1):
        edgeFreq[i] = 0
    for node in order:
        if node == 1:
            continue
        if len(graph[node]) == 1:
            edgeFreq[node] = 1
        if nodeToParent[node] != 1:
            edgeFreq[nodeToParent[node]] += edgeFreq[node]

    ans = 0
    currSum = 0
    moves = []
    for i in range(2,n+1):
        wt = edgeToWeight[(i,nodeToParent[i])]
        currSum += wt*edgeFreq[i]
        while wt > 0:
            moves.append((wt - wt//2)*edgeFreq[i])
            wt = wt // 2
    moves.sort()
    pt = len(moves)-1
    while currSum > s:
        currSum -= moves[pt]
        pt -= 1
        ans += 1
##    print(graph)
##    print(edgeToWeight)
##    print(edgeFreq)
##    print(nodeToParent)
##    print(moves)
    print(ans)


from collections import deque
import heapq

t = int(input().split()[0])
for case in range(t):
    n,s = map(int,input().split())
    graph = {}
    edgeToWeight = {}
    for i in range(1,n+1):
        graph[i] = []
    for i in range(n-1):
        u,v,w = map(int,input().split())
        graph[u].append(v)
        graph[v].append(u)
        edgeToWeight[(u,v)] = w
        edgeToWeight[(v,u)] = w
    nodeToParent = {}

    #BFS
    visited = [False]*(n+1)
    queue = deque()
    queue.append(1)
    visited[1] = True
    order = []
    while queue:
        curr = queue.popleft()
        order.append(curr)
        for neighbor in graph[curr]:
            if visited[neighbor] == False:
                nodeToParent[neighbor] = curr
                queue.append(neighbor)
                visited[neighbor] = True

    order.reverse()
    edgeFreq = {}
    for i in range(2,n+1):
        edgeFreq[i] = 0
    for node in order:
        if node == 1:
            continue
        if len(graph[node]) == 1:
            edgeFreq[node] = 1
        if nodeToParent[node] != 1:
            edgeFreq[nodeToParent[node]] += edgeFreq[node]

    ans = 0
    currSum = 0
    moves = []
    for i in range(2,n+1):
        wt = edgeToWeight[(i,nodeToParent[i])]
        currSum += wt*edgeFreq[i]
        while wt > 0:
            heapq.heappush(moves,(-(wt - wt//2)*edgeFreq[i],0))
            wt = wt // 2
##    if currSum > s:
##        moves.sort()
##    pt = len(moves)-1
    while currSum > s:
        currSum += heapq.heappop(moves)[0]
##        pt -= 1
        ans += 1
##    print(graph)
##    print(edgeToWeight)
##    print(edgeFreq)
##    print(nodeToParent)
    print(ans)


import math,sys,bisect,heapq
from collections import defaultdict,Counter,deque
from itertools import groupby,accumulate
#sys.setrecursionlimit(200000000)
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda: map(int,input().split())
alele = lambda: list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
#MOD = 1000000000 + 7
def Y(c):  print(["NO","YES"][c])
def y(c):  print(["no","yes"][c])
def Yy(c):  print(["No","Yes"][c])

G = defaultdict(list)
first = 0
parents = [];order = []
def addEdge(a,b):
    G[a].append(b)
    G[b].append(a)

def DFSutil(node,parents,order):
    my_que = deque()
    my_que.append(node)
    while len(my_que)>0:
        index = my_que.pop()
        for edge in G.get(index,[]):
            if parents[edge]==-2:
                my_que.append(edge)
                parents[edge] = index
                order.append(edge)

def findsubtree(first,N):
    parents = [-2 for i in range(N)]
    parents[0] = -1
    order = [0]
    size = [0]*(N)
    for i in range(N):
        if len(G[i]) == 1:
            size[i] = 1
    DFSutil(first,parents,order)
    for z in range(N-1, 0, -1):
        bb = order[z]
        size[parents[bb]] += size[bb]
    return size,parents

def fun(a,b):
    return a*b - (a//2)*b

for _ in range(int(input())):
    G = defaultdict(list)
    parents = [];order = []
    N,S = ilele()
    E =[]
    mark = {}
    for i in range(N-1):
        u,v,w = ilele()
        u-=1;v-=1
        addEdge(u,v)
        mark[(u,v)] = w
        mark[(v,u)] = w
    X,par = findsubtree(first,N)
    #print(X)
    H = []
    tot = 0
    for i in range(1,N):
        e = par[i]
        c = mark[(e,i)]
        d = X[i]
        tot += d*c
        #print(e+1,i+1,d*c)
        while fun(c,d):
            H.append(fun(c,d))
            c = c//2
            #print(c)
    #print(tot,S)
    if tot <= S:
        print(0)
    else:
        H.sort()
        #print(H,tot)
        count = 0
        while tot > S and len(H) > 0:
            tot -= H.pop()
            count += 1
        print(count)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    tot = 0

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            jjj= E[(v,nxt_v)] // 2
            gain = temp - (jjj*T[nxt_v])
            tot += temp
            nums.append((-gain, E[(v,nxt_v)], T[nxt_v]))

    heapq.heapify(nums)
    c = 0
    #print(tot)

    while tot>s:
        if n==10 and s==28:
            print(tot, nums)

        gain, xx, yy = heapq.heappop(nums)
        tot += gain

        xx = xx//2

        gg = ((xx//2) * yy) - xx*yy
        heapq.heappush(nums, (gg,xx,yy))
        c+=1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    tot = 0

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            jjj= E[(v,nxt_v)] // 2
            gain = temp - (jjj*T[nxt_v])
            tot += temp
            nums.append((-gain, E[(v,nxt_v)], T[nxt_v]))

    heapq.heapify(nums)
    c = 0

    while tot>s:
        gain, xx, yy = heapq.heappop(nums)

        if s < n:
            tot -= ((xx-1)*yy)
            while xx>1:
                xx = xx//2
                c += 1

        else:
            tot += gain
            xx = xx//2

            gg = ((xx//2) * yy) - xx*yy
            heapq.heappush(nums, (gg,xx,yy))
            c+=1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            nums.append(-temp)

    tot = abs(sum(nums))
    heapq.heapify(nums)
    c = 0

    if n==10 and s==28:
        print(nums)

    while tot>s:
        #print(tot,c,nums)
        t = heapq.heappop(nums)
        t = abs(t)
        new_t = t//2

        tot -= t
        tot += new_t
        heapq.heappush(nums, -new_t)
        c += 1

    print(c)


from bisect import bisect_left
import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * 1# T[nxt_v]
            nums.append(-temp)

    tot = abs(sum(nums))
    heapq.heapify(nums)
    c = 0

    #print(nums)

    while tot>s:
        #print(tot,c,nums)
        t = heapq.heappop(nums)
        t = abs(t)
        new_t = t//2

        tot -= t
        tot += new_t
        heapq.heappush(nums, -new_t)
        c += 1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    tot = 0

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            jjj= E[(v,nxt_v)] // 2
            gain = temp - (jjj*T[nxt_v])
            tot += temp
            nums.append((-gain, E[(v,nxt_v)], T[nxt_v]))

    heapq.heapify(nums)
    c = 0
    #print(tot)

    while tot>s:
        #if n==10 and s==28:
        #    print(tot, nums)

        gain, xx, yy = heapq.heappop(nums)
        tot += gain

        xx = xx//2

        gg = ((xx//2) * yy) - xx*yy
        heapq.heappush(nums, (gg,xx,yy))
        c+=1

    print(c)


from bisect import bisect_left
import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            nums.append(-temp)

    tot = abs(sum(nums))
    heapq.heapify(nums)
    c = 0

    while tot>s:
        #print(tot,c,nums)
        t = heapq.heappop(nums)
        t = abs(t)
        new_t = t//2

        tot -= t
        tot += new_t
        heapq.heappush(nums, -new_t)
        c += 1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            nums.append((-temp, T[nxt_v]))

    tot = 0
    for i in range(len(nums)):
        tot += abs(nums[i][0])

    heapq.heapify(nums)
    c = 0

    while tot>s:
        if n==10 and s==28:
            print(nums)
        #print(tot,c,nums)
        t = heapq.heappop(nums)
        hh = abs(t[0])
        tot -= hh

        hh = hh // t[1]
        new_t = hh//2
        new_t *= t[1]

        tot += new_t
        heapq.heappush(nums, (-new_t, t[1]))
        c += 1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            nums.append((-temp, T[nxt_v]))

    tot = abs(sum(nums))
    heapq.heapify(nums)
    c = 0

    if n==10 and s==28:
        print(nums)

    while tot>s:
        #print(tot,c,nums)
        t = heapq.heappop(nums)
        hh = abs(t[0])
        hh = hh // t[1]
        new_t = hh//2
        new_t *= t[1]

        tot -= hh
        tot += new_t
        heapq.heappush(nums, (-new_t, t[1]))
        c += 1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            nums.append((-temp, T[nxt_v]))

    tot = 0
    for i in range(len(nums)):
        tot += nums[i][0]

    heapq.heapify(nums)
    c = 0

    if n==10 and s==28:
        print(nums)

    while tot>s:
        #print(tot,c,nums)
        t = heapq.heappop(nums)
        hh = abs(t[0])
        hh = hh // t[1]
        new_t = hh//2
        new_t *= t[1]

        tot -=
        tot += new_t
        heapq.heappush(nums, (-new_t, t[1]))
        c += 1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    stack = [0]
    nums = []

    tot = 0

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            jjj= E[(v,nxt_v)] // 2
            gain = temp - (jjj*T[nxt_v])
            tot += temp
            nums.append((-gain, E[(v,nxt_v)], T[nxt_v]))

    heapq.heapify(nums)
    c = 0

    while tot>s:

        gain, xx, yy = heapq.heappop(nums)
        if s==1:
            tot -= (xx*yy)
            while xx>0:
                xx = xx // 2
                c += 1
        else:
            tot += gain

            xx = xx//2
            gg = ((xx//2) * yy) - xx*yy
            heapq.heappush(nums, (gg,xx,yy))
            c+=1

    if s==1 and tot==0:
        c -= 1
    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            nums.append((-temp, T[nxt_v]))

    tot = 0
    for i in range(len(nums)):
        tot += abs(nums[i][0])

    heapq.heapify(nums)
    c = 0

    if n==10 and s==28:
        print(nums)

    while tot>s:
        #print(tot,c,nums)
        t = heapq.heappop(nums)
        hh = abs(t[0])
        tot -= hh

        hh = hh // t[1]
        new_t = hh//2
        new_t *= t[1]

        tot += new_t
        heapq.heappush(nums, (-new_t, t[1]))
        c += 1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    stack = [0]
    nums = []

    tot = 0

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            jjj= E[(v,nxt_v)] // 2
            gain = temp - (jjj*T[nxt_v])
            tot += temp
            nums.append((-gain, E[(v,nxt_v)], T[nxt_v]))

    heapq.heapify(nums)
    c = 0

    while tot>s:

        gain, xx, yy = heapq.heappop(nums)
        if s==1:
            tot -= (xx*yy)
            while xx>0:
                xx = xx // 2
                c += 1
        else:
            tot += gain

            xx = xx//2
            gg = ((xx//2) * yy) - xx*yy
            heapq.heappush(nums, (gg,xx,yy))
            c+=1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    tot = 0

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            jjj= E[(v,nxt_v)] // 2
            gain = temp - (jjj*T[nxt_v])
            tot += temp
            nums.append((-gain, E[(v,nxt_v)], T[nxt_v]))

    heapq.heapify(nums)
    c = 0

    while tot>s:
        gain, xx, yy = heapq.heappop(nums)

        if s < n:
            tot -= (xx*yy)
            while xx>0:
                xx = xx//2
                c += 1

        else:
            tot += gain
            xx = xx//2

            gg = ((xx//2) * yy) - xx*yy
            heapq.heappush(nums, (gg,xx,yy))
            c+=1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            nums.append((-temp, T[nxt_v]))

    tot = abs(sum(nums))
    heapq.heapify(nums)
    c = 0

    if n==10 and s==28:
        print(nums)

    while tot>s:
        #print(tot,c,nums)
        t = heapq.heappop(nums)
        hh = abs(t[0])
        hh = hh // t[1]
        new_t = hh//2
        new_t *= t[1]

        tot -= t
        tot += new_t
        heapq.heappush(nums, (-new_t, t[1]))
        c += 1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    tot = 0

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            jjj= E[(v,nxt_v)] // 2
            gain = temp - (jjj*T[nxt_v])
            tot += temp
            nums.append((-gain, E[(v,nxt_v)], T[nxt_v]))

    heapq.heapify(nums)
    c = 0

    while tot>s:
        gain, xx, yy = heapq.heappop(nums)

        if s < n and xx>1:
            tot -= ((xx-1)*yy)
            while xx>1:
                xx = xx//2
                c += 1
            heapq.heappush(nums, (-(xx*yy),xx,yy))

        else:
            tot += gain
            xx = xx//2

            gg = ((xx//2) * yy) - xx*yy
            heapq.heappush(nums, (gg,xx,yy))
            c+=1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    stack = [0]
    nums = []

    tot = 0

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            jjj= E[(v,nxt_v)] // 2
            gain = temp - (jjj*T[nxt_v])
            tot += temp
            nums.append((-gain, E[(v,nxt_v)], T[nxt_v]))

    heapq.heapify(nums)
    c = 0

    while tot>s:
        gain, xx, yy = heapq.heappop(nums)
        tot += gain

        xx = xx//2
        gg = ((xx//2) * yy) - xx*yy
        heapq.heappush(nums, (gg,xx,yy))
        c+=1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            nums.append((-temp, T[nxt_v]))

    tot = 0
    for i in range(len(nums)):
        tot += nums[i][0]

    heapq.heapify(nums)
    c = 0

    if n==10 and s==28:
        print(nums)

    while tot>s:
        #print(tot,c,nums)
        t = heapq.heappop(nums)
        hh = abs(t[0])
        hh = hh // t[1]
        new_t = hh//2
        new_t *= t[1]

        tot -= hh
        tot += new_t
        heapq.heappush(nums, (-new_t, t[1]))
        c += 1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            nums.append((-temp, T[nxt_v]))

    tot = 0
    for i in range(len(nums)):
        tot += abs(nums[i][0])

    heapq.heapify(nums)
    c = 0

    if n==10 and s==28:
        print(nums)

    while tot>s:
        #print(tot,c,nums)
        t = heapq.heappop(nums)
        hh = abs(t[0])
        hh = hh // t[1]
        new_t = hh//2
        new_t *= t[1]

        tot -= hh
        tot += new_t
        heapq.heappush(nums, (-new_t, t[1]))
        c += 1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    tot = 0

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            jjj= E[(v,nxt_v)] // 2
            gain = temp - (jjj*T[nxt_v])
            tot += temp
            nums.append((-gain, E[(v,nxt_v)], T[nxt_v]))

    heapq.heapify(nums)
    c = 0

    while tot>s:
        gain, xx, yy = heapq.heappop(nums)

        if s < n:
            while xx>0:
                xx = xx//2
                c += 1

            tot -= (xx*yy)

        else:
            tot += gain
            xx = xx//2

            gg = ((xx//2) * yy) - xx*yy
            heapq.heappush(nums, (gg,xx,yy))
            c+=1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            nums.append((-temp, T[nxt_v]))

    tot = 0
    for i in range(len(nums)):
        tot += abs(nums[i][0])

    heapq.heapify(nums)
    c = 0

    while tot>s:
        if n==10 and s==28:
            print(tot, nums)
        #print(tot,c,nums)
        t = heapq.heappop(nums)
        hh = abs(t[0])
        tot -= hh

        hh = hh // t[1]
        new_t = hh//2
        new_t *= t[1]

        tot += new_t
        heapq.heappush(nums, (-new_t, t[1]))
        c += 1

    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    stack = [0]
    nums = []

    tot = 0

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            jjj= E[(v,nxt_v)] // 2
            gain = temp - (jjj*T[nxt_v])
            tot += temp
            nums.append((-gain, E[(v,nxt_v)], T[nxt_v]))

    heapq.heapify(nums)
    c = 0

    while tot>s:

        gain, xx, yy = heapq.heappop(nums)
        if s==1:
            tot -= (xx*yy)
            while xx>0:
                xx = xx // 2
                c += 1
        else:
            tot += gain

            xx = xx//2
            gg = ((xx//2) * yy) - xx*yy
            heapq.heappush(nums, (gg,xx,yy))
            c+=1

    if ss==1 and tot==0:
        c -= 1
    print(c)


import sys
import heapq
input=sys.stdin.readline
t = int(input())

for _ in range(t):
    n,s = list(map(int,input().split()))

    E = []
    N = [[] for _ in range(n)]
    V = []
    P = [-1 for _ in range(n)]
    E = {}

    for i in range(n-1):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        N[a].append(b)
        N[b].append(a)
        E[(a,b)] = c
        E[(b,a)] = c

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    NN = [[] for _ in range(n)]
    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            NN[v].append(nxt_v)
            P[nxt_v] = v
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

    T = [1 for _ in range(n)]

    tp_sorted = tp_sorted[::-1]

    for v in tp_sorted:
        if len(NN[v])>0:
            temp = 0
            for nxt_v in NN[v]:
                temp += T[nxt_v]
            T[v] = temp

    visited = [False] * n
    visited[0] = True
    tp_sorted = [0]
    stack = [0]
    nums = []

    tot = 0

    while stack:
        v = stack.pop()
        for nxt_v in N[v]:
            if visited[nxt_v]:
                continue
            visited[nxt_v] = True
            stack.append(nxt_v)
            tp_sorted.append(nxt_v)

            temp = E[(v,nxt_v)] * T[nxt_v]
            jjj= E[(v,nxt_v)] // 2
            gain = temp - (jjj*T[nxt_v])
            tot += temp
            nums.append((-gain, E[(v,nxt_v)], T[nxt_v]))

    heapq.heapify(nums)
    c = 0

    while tot>s:
        gain, xx, yy = heapq.heappop(nums)
        tot += gain

        if s < n:
            while xx>0:
                xx = xx//2
                c += 1

        else:
            xx = xx//2

            gg = ((xx//2) * yy) - xx*yy
            heapq.heappush(nums, (gg,xx,yy))
            c+=1

    print(c)


# by the authority of GOD   author: manhar singh sachdev #

import os,sys
from io import BytesIO, IOBase
from collections import defaultdict,Counter
from copy import deepcopy
from heapq import *

class Node:
    def __init__(self,val,occ):
        self.val = val
        self.occ = occ
        self.dec = (val-val//2)*occ
    def __lt__(self,other):
        return self.dec > other.dec
    def __str__(self):
       return f'{self.val} {self.occ} {self.dec}'

def main():
    for _ in range(int(input())):
        n,s = map(int,input().split())
        path = defaultdict(set)
        cost = {}
        for _ in range(n-1):
            u,v,w = map(int,input().split())
            path[u].add(v)
            path[v].add(u)
            cost[(u,v)] = w
        path1 = deepcopy(path)
        child = Counter()
        st = [1]
        while len(st):
            if not len(path[st[-1]]):
                if not child[st[-1]]:
                    child[st[-1]] = 1
                if len(st) > 1:
                    child[st[-2]] += child[st[-1]]
                st.pop()
                continue
            i = path[st[-1]].pop()
            path[i].remove(st[-1])
            st.append(i)
        st,y = [1],[]
        while len(st):
            if not len(path1[st[-1]]):
                st.pop()
                continue
            i = path1[st[-1]].pop()
            path1[i].remove(st[-1])
            st.append(i)
            if cost.get((st[-1],st[-2])):
                y.append(Node(cost[(st[-1],st[-2])],child[st[-1]]))
                cost[(st[-1],st[-2])] *= child[st[-1]]
            else:
                y.append(Node(cost[(st[-2],st[-1])],child[st[-1]]))
                cost[(st[-2],st[-1])] *= child[st[-1]]
        x = sum(cost.values())
        heapify(y)
        ans = 0
        while x > s:
            z = heappop(y)
            x -= z.dec
            z = Node(z.val//2,z.occ)
            if z.val:
                heappush(y,z)
            ans += 1
        print(ans)

#Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == '__main__':
    main()


# by the authority of GOD   author: manhar singh sachdev #

import os,sys
from io import BytesIO, IOBase
from collections import defaultdict,Counter
from copy import deepcopy
from heapq import *

def main():
    for _ in range(int(input())):
        n,s = map(int,input().split())
        path = defaultdict(set)
        cost = {}
        for _ in range(n-1):
            u,v,w = map(int,input().split())
            path[u].add(v)
            path[v].add(u)
            cost[(u,v)] = w
        path1 = deepcopy(path)
        child = Counter()
        st = [1]
        while len(st):
            if not len(path[st[-1]]):
                if not child[st[-1]]:
                    child[st[-1]] = 1
                if len(st) > 1:
                    child[st[-2]] += child[st[-1]]
                st.pop()
                continue
            i = path[st[-1]].pop()
            path[i].remove(st[-1])
            st.append(i)
        st = [1]
        while len(st):
            if not len(path1[st[-1]]):
                st.pop()
                continue
            i = path1[st[-1]].pop()
            path1[i].remove(st[-1])
            st.append(i)
            if cost.get((st[-1],st[-2])):
                cost[(st[-1],st[-2])] *= child[st[-1]]
            else:
                cost[(st[-2],st[-1])] *= child[st[-1]]
        x = sum(cost.values())
        y = list(map(lambda k: k*-1,cost.values()))
        heapify(y)
        ans = 0
        while x > s:
            z = abs(heappop(y))
            x -= (z-z//2)
            if z//2:
                heappush(y,-(z//2))
            ans += 1
        print(ans)

#Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
import io
import os
import sys
import threading
import heapq

input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

def get_str():
    return input().decode().strip()

def rint():
    return map(int, input().split())

def oint():
    return int(input())

def solve():
    def dfs(cur):
        if len(adj[cur]) == 1 and cur != 1:
            lc[cur] = 1
            return
        ret = 0
        for ni, nw in adj[cur]:
            if v[ni]:
                continue
            v[ni] = 1
            dfs(ni)
            sl.append((lc[ni]*(nw//2) - lc[ni]*nw, ni))
            w[ni]= nw
            ret += lc[ni]
        lc[cur] = ret
        return

    t = oint()

    for _ in range(t):
        n, s = rint()
        adj = [[] for i in range(n+1)]
        v = [0]*(n+1)
        lc = [0]*(n+1)
        w = [0]*(n+1)
        sl = []
        for i in range(n-1):
            vv, uu, ww = rint()
            adj[vv].append((uu, ww))
            adj[uu].append((vv, ww))
        v[1] = 1
        dfs(1)
        sum_ = 0
        for item in sl:
            i = item[1]
            mul = lc[i]
            sum_ += mul*w[i]

        cnt = 0
        heapq.heapify(sl)
        #continue
        adj = []
        v = []
        while sum_ > s:
            item = heapq.heappop(sl)
            i = item[1]
            mul = lc[i]
            sum_ = sum_ - w[i]*mul + (w[i]//2)*mul
            w[i] //=2
            if w[i] != 0:
                heapq.heappush(sl, (mul*(w[i]//2) - mul*w[i], i))
            cnt += 1
        print(cnt)

max_recursion_size = 10**5+100
max_stack_size = max_recursion_size * 100
sys.setrecursionlimit(max_recursion_size)
threading.stack_size(max_stack_size)
thread = threading.Thread(target=solve)
thread.start()