#!/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 dfs(cur):
    stack =[cur]
    while stack:
        cur = stack[-1]
        if v[cur] == 0:
            v[cur] = 1
            for ni, nw in adj[cur]:
                if v[ni]:
                    continue
                stack.append(ni)
        else:
            if cur != 1 and len(adj[cur]) == 1:
                ret = 1
            else:
                ret = 0
            for ni, nw in adj[cur]:
                if v[ni] == 1:
                    w[cur] = nw
                    continue
                ret += lc[ni]
            lc[cur] = ret
            sl.append((lc[cur]*(w[cur]//2) - lc[cur]*w[cur], cur))
            v[cur] = 2
            stack.pop()

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))
    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)


#!/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 * 200
sys.setrecursionlimit(max_recursion_size)
threading.stack_size(max_stack_size)
thread = threading.Thread(target=solve)
thread.start()


#!/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 dfs(cur):
    stack =[cur]
    while stack:
        cur = stack[-1]
        if v[cur] == 0:
            v[cur] = 1
            for ni, nw in adj[cur]:
                if v[ni]:
                    continue
                stack.append(ni)
        else:
            if cur != 1 and len(adj[cur]) == 1:
                ret = 1
            else:
                ret = 0
            for ni, nw in adj[cur]:
                if v[ni] == 1:
                    w[cur] = nw
                    continue
                ret += lc[ni]
            lc[cur] = ret
            sl.append((lc[cur]*(w[cur]//2) - lc[cur]*w[cur], cur))
            v[cur] = 2
            stack.pop()

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))
    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)


#!/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):
        ret = 0
        if len(adj[cur]) == 1 and cur != 1:
            lc[cur] = 1
            return 1
        for next_ in adj[cur]:
            ni = next_[0]
            nw = next_[1]
            if v[ni]:
                continue
            v[ni] = 1
            llc = dfs(ni)
            sl.append((llc*(nw//2) - llc*nw, ni, nw))
            ret += llc
        lc[cur] = ret
        return ret

    t = oint()

    for _ in range(t):
        n, s = rint()
        adj = [[] for i in range(n+1)]
        v = [0]*(n+1)
        lc = [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]
            w = item[2]
            mul = lc[i]
            sum_ += mul*w

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

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


#!/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):
        ret = 0
        if len(adj[cur]) == 1 and cur != 1:
            lc[cur] = 1
            return 1
        for next_ in adj[cur]:
            ni = next_[0]
            nw = next_[1]
            if v[ni]:
                continue
            v[ni] = 1
            llc = dfs(ni)
            sl.append((llc*(nw//2) - llc*nw, ni, nw))
            ret += llc
        lc[cur] = ret
        return ret

    t = oint()

    for _ in range(t):
        n, s = rint()
        adj = [[] for i in range(n+1)]
        v = [0]*(n+1)
        lc = [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]
            w = item[2]
            mul = lc[i]
            sum_ += mul*w

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

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


#!/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):
        ret = 0
        if len(adj[cur]) == 1 and cur != 1:
            lc[cur] = 1
            return 1
        for next_ in adj[cur]:
            ni = next_[0]
            nw = next_[1]
            if v[ni]:
                continue
            v[ni] = 1
            llc = dfs(ni)
            sl.append((llc*(nw//2) - llc*nw, ni, nw))
            ret += llc
        lc[cur] = ret
        return ret

    t = oint()

    for _ in range(t):
        n, s = rint()
        adj = [[] for i in range(n+1)]
        v = [0]*(n+1)
        lc = [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]
            w = item[2]
            mul = lc[i]
            sum_ += mul*w

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

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


#!/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):
        ret = 0
        if len(adj[cur]) == 1 and cur != 1:
            lc[cur] = 1
            return 1
        for next_ in adj[cur]:
            ni = next_[0]
            nw = next_[1]
            if v[ni]:
                continue
            v[ni] = 1
            llc = dfs(ni)
            sl.append((llc*(nw//2) - llc*nw, ni, nw))
            ret += llc
        lc[cur] = ret
        return ret

    t = oint()

    for _ in range(t):
        n, s = rint()
        adj = [[] for i in range(n+1)]
        v = [0]*(n+1)
        lc = [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]
            w = item[2]
            mul = lc[i]
            sum_ += mul*w

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

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


#!/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
        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

    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 * 200
sys.setrecursionlimit(max_recursion_size)
threading.stack_size(max_stack_size)
thread = threading.Thread(target=solve)
thread.start()


#!/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):
        ret = 0
        if len(adj[cur]) == 1 and cur != 1:
            lc[cur] = 1
            return 1
        for ni, nw in adj[cur]:
            if v[ni]:
                continue
            v[ni] = 1
            llc = dfs(ni)
            sl.append((llc*(nw//2) - llc*nw, ni))
            w[ni]= nw
            ret += llc
        lc[cur] = ret
        return ret

    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 * 200
sys.setrecursionlimit(max_recursion_size)
threading.stack_size(max_stack_size)
thread = threading.Thread(target=solve)
thread.start()


#!/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):
        ret = 0
        if len(adj[cur]) == 1 and cur != 1:
            lc[cur] = 1
            return 1
        for next_ in adj[cur]:
            ni = next_[0]
            nw = next_[1]
            if v[ni]:
                continue
            v[ni] = 1
            llc = dfs(ni)
            sl.append((llc*(nw//2)*(-1), ni, nw))
            ret += llc
        lc[cur] = ret
        return ret

    t = oint()

    for _ in range(t):
        n, s = rint()
        adj = [[] for i in range(n+1)]
        v = [0]*(n+1)
        lc = [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]
            w = item[2]
            mul = lc[i]
            sum_ += mul*w

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

max_recursion_size = 10**6
max_stack_size = max_recursion_size * 400
sys.setrecursionlimit(max_recursion_size)
threading.stack_size(max_stack_size)
thread = threading.Thread(target=solve)
thread.start()


#!/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+1
max_stack_size = max_recursion_size * 150
sys.setrecursionlimit(max_recursion_size)
threading.stack_size(max_stack_size)
thread = threading.Thread(target=solve)
thread.start()


#!/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):
        ret = 0
        if len(adj[cur]) == 1 and cur != 1:
            lc[cur] = 1
            return 1
        for next_ in adj[cur]:
            ni = next_[0]
            nw = next_[1]
            if v[ni]:
                continue
            v[ni] = 1
            llc = dfs(ni)
            sl.append((llc*(nw//2) - llc*nw, ni, nw))
            ret += llc
        lc[cur] = ret
        return ret

    t = oint()

    for _ in range(t):
        n, s = rint()
        adj = [[] for i in range(n+1)]
        v = [0]*(n+1)
        lc = [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]
            w = item[2]
            mul = lc[i]
            sum_ += mul*w

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

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


#!/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):
        ret = 0
        if len(adj[cur]) == 1 and cur != 1:
            lc[cur] = 1
            return 1
        for next_ in adj[cur]:
            ni = next_[0]
            nw = next_[1]
            if v[ni]:
                continue
            v[ni] = 1
            llc = dfs(ni)
            sl.append((llc*(nw//2)*(-1), ni, nw))
            ret += llc
        lc[cur] = ret
        return ret

    t = oint()

    for _ in range(t):
        n, s = rint()
        adj = [[] for i in range(n+1)]
        v = [0]*(n+1)
        lc = [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]
            w = item[2]
            mul = lc[i]
            sum_ += mul*w

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

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


#!/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):
        ret = 0
        if len(adj[cur]) == 1 and cur != 1:
            lc[cur] = 1
            return 1
        for next_ in adj[cur]:
            ni = next_[0]
            nw = next_[1]
            if v[ni]:
                continue
            v[ni] = 1
            llc = dfs(ni)
            sl.append((llc*(nw//2) - llc*nw, ni, nw))
            ret += llc
        lc[cur] = ret
        return ret

    t = oint()

    for _ in range(t):
        n, s = rint()
        adj = [[] for i in range(n+1)]
        v = [0]*(n+1)
        lc = [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]
            w = item[2]
            mul = lc[i]
            sum_ += mul*w

        cnt = 0
        heapq.heapify(sl)
        #continue
        while sum_ > s:
            item = heapq.heappop(sl)
            i = item[1]
            w = item[2]
            mul = lc[i]
            sum_ = sum_ - w*mul + (w//2)*mul
            w //=2
            if w != 0:
                heapq.heappush(sl, (mul*(w//2) - mul*w, i, w))
            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()


#!/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):
        ret = 0
        if len(adj[cur]) == 1 and cur != 1:
            lc[cur] = 1
            return 1
        for ni, nw in adj[cur]:
            if v[ni]:
                continue
            v[ni] = 1
            llc = dfs(ni)
            sl.append((llc*(nw//2) - llc*nw, ni, nw))
            ret += llc
        lc[cur] = ret
        return ret

    t = oint()

    for _ in range(t):
        n, s = rint()
        adj = [[] for i in range(n+1)]
        v = [0]*(n+1)
        lc = [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]
            w = item[2]
            mul = lc[i]
            sum_ += mul*w

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

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


#!/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 * 250
sys.setrecursionlimit(max_recursion_size)
threading.stack_size(max_stack_size)
thread = threading.Thread(target=solve)
thread.start()


#!/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):
        ret = 0
        if len(adj[cur]) == 1 and cur != 1:
            lc[cur] = 1
            return 1
        for ni, nw in adj[cur]:
            if v[ni]:
                continue
            v[ni] = 1
            llc = dfs(ni)
            sl.append((llc*(nw//2) - llc*nw, ni))
            w[ni]= nw
            ret += llc
        lc[cur] = ret
        return ret

    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 * 300
sys.setrecursionlimit(max_recursion_size)
threading.stack_size(max_stack_size)
thread = threading.Thread(target=solve)
thread.start()


#!/usr/bin/env python3
import io
import os

import heapq

import sys
import resource
sys.setrecursionlimit(10**7)
resource.setrlimit(resource.RLIMIT_STACK,(10**7*400, -1))

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 dfs(cur):
    #print('cur', cur)
    ret = 0
    if len(adj[cur]) == 1 and cur != 1:
        lc[cur] = 1
        #sl.append(adj[cur][0][1])
        #v[adj[cur][0][0]] = 1
        return 1
    for next_ in adj[cur]:
        #print('next_', next_)
        if v[next_[0]]:
            continue
        v[next_[0]] = 1
        llc = dfs(next_[0])
        sl.append(llc*next_[1]*(-1))
        ret += llc
    lc[cur] = ret
    return ret

t = oint()

for _ in range(t):
    n, s = rint()
    adj = [[] for i in range(n+1)]
    v = [0]*(n+1)
    lc = [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
    #print(adj)
    rr = dfs(1)
    sum_ = sum(sl)*(-1)
    cnt = 0
    heapq.heapify(sl)
    #continue
    while sum_ > s:
        ss = heapq.heappop(sl)*(-1)
        sum_ = sum_ - ss + ss//2
        heapq.heappush(sl, ss//2*(-1))
        cnt += 1
    print(cnt)


#!/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 dfs(cur):
    stack =[cur]
    while stack:
        cur = stack[-1]
        if v[cur] == 0:
            v[cur] = 1
            for ni, nw in adj[cur]:
                if v[ni]:
                    continue
                stack.append(ni)
        else:
            if cur != 1 and len(adj[cur]) == 1:
                ret = 1
            else:
                ret = 0
            for ni, nw in adj[cur]:
                if v[ni] == 1:
                    w[cur] = nw
                    continue
                ret += lc[ni]
            lc[cur] = ret
            sl.append((lc[cur]*(w[cur]//2) - lc[cur]*w[cur], cur))
            v[cur] = 2
            stack.pop()

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))
    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)


#!/usr/bin/env python3
import io
import os

import heapq

import sys
#import resource
sys.setrecursionlimit(10**5+10)
#resource.setrlimit(resource.RLIMIT_STACK,(10**7*400, -1))

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 dfs(cur):
    #print('cur', cur)
    ret = 0
    if len(adj[cur]) == 1 and cur != 1:
        lc[cur] = 1
        #sl.append(adj[cur][0][1])
        #v[adj[cur][0][0]] = 1
        return 1
    for next_ in adj[cur]:
        #print('next_', next_)
        if v[next_[0]]:
            continue
        v[next_[0]] = 1
        llc = dfs(next_[0])
        sl.append(llc*next_[1]*(-1))
        ret += llc
    lc[cur] = ret
    return ret

t = oint()

for _ in range(t):
    n, s = rint()
    adj = [[] for i in range(n+1)]
    v = [0]*(n+1)
    lc = [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
    #print(adj)
    rr = dfs(1)
    sum_ = sum(sl)*(-1)
    cnt = 0
    heapq.heapify(sl)
    #continue
    while sum_ > s:
        ss = heapq.heappop(sl)*(-1)
        sum_ = sum_ - ss + ss//2
        heapq.heappush(sl, ss//2*(-1))
        cnt += 1
    print(cnt)


#!/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):
        ret = 0
        if len(adj[cur]) == 1 and cur != 1:
            lc[cur] = 1
            return 1
        for ni, nw in adj[cur]:
            if v[ni]:
                continue
            v[ni] = 1
            llc = dfs(ni)
            sl.append((llc*(nw//2) - llc*nw, ni))
            w[ni]= nw
            ret += llc
        lc[cur] = ret
        return ret

    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 * 400
sys.setrecursionlimit(max_recursion_size)
threading.stack_size(max_stack_size)
thread = threading.Thread(target=solve)
thread.start()


#!/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 * 300
sys.setrecursionlimit(max_recursion_size)
threading.stack_size(max_stack_size)
thread = threading.Thread(target=solve)
thread.start()


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):
        ret = 0
        if len(adj[cur]) == 1 and cur != 1:
            lc[cur] = 1
            return 1
        for next_ in adj[cur]:
            ni = next_[0]
            nw = next_[1]
            if v[ni]:
                continue
            v[ni] = 1
            llc = dfs(ni)
            sl.append((llc*(nw//2) - llc*nw, ni, nw))
            ret += llc
        lc[cur] = ret
        return ret

    t = oint()

    for _ in range(t):
        n, s = rint()
        adj = [[] for i in range(n+1)]
        v = [0]*(n+1)
        lc = [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]
            w = item[2]
            mul = lc[i]
            sum_ += mul*w

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

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


#!/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)).readli

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

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

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

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

    t = oint()

    for _ in range(t):
        n, s = rint()
        adj = [[] for i in range(n+1)]
        v = [0]*(n+1)
        lc = [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]
            w = item[2]
            mul = lc[i]
            sum_ += mul*w

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

max_recursion_size = 10**6
max_stack_size = max_recursion_size * 400
sys.setrecursionlimit(max_recursion_size)
threading.stack_size(250*10**6)
thread = threading.Thread(target=solve)
thread.start()


import heapq

t = int(input())

for _ in range(t):
    n,s = map(int,input().split())
    vertexScore = [] # number of leaves
    motherEdge = []
    edgeList = []
    inputEdgeList = []
    for _ in range(n):
        edgeList.append([])
        motherEdge.append(-1)
        vertexScore.append(0)
    for _ in range(n-1):
        r,m,w = map(int,input().split())
        inputEdgeList.append((r-1,m-1,w))
        edgeList[r-1].append(m-1)
        edgeList[m-1].append(r-1)
    isAdded = [False] * n
    isAdded[0] = True
    vertexStack = [(0,-1)]
    vertexScoreStack = [] # vertex whose daughters have vertexScore calculated
    daughterCountList = [0] * n
    while vertexStack:
        elem = vertexStack.pop()
        motherEdge[elem[0]] = elem[1]
        daughterCount = 0
        for vertex in edgeList[elem[0]]:
            if not isAdded[vertex]:
                isAdded[vertex] = True
                daughterCount += 1
                vertexStack.append((vertex,elem[0]))
        daughterCountList[elem[0]] = daughterCount
        if daughterCount == 0:
            vertexScoreStack.append(elem[0])
    while vertexScoreStack:
        score = 0
        vertex = vertexScoreStack.pop()
        for daughter in edgeList[vertex]:
            if daughter != motherEdge[vertex]:
                score += vertexScore[daughter]
        if score == 0:
            score = 1
        vertexScore[vertex] = score
        if motherEdge[vertex] != -1:
            daughterCountList[motherEdge[vertex]] -= 1
            if daughterCountList[motherEdge[vertex]] == 0:
                vertexScoreStack.append(motherEdge[vertex])
    print(vertexScore)
    edgeHeapQ = [] # (decremented score, multiplier, weight)
    totalScore = 0
    for elem in inputEdgeList:
        if motherEdge[elem[0]] == elem[1]:
            decScore = elem[2] % 2 + elem[2] // 2
            decScore *= vertexScore[elem[0]]
            heapq.heappush(edgeHeapQ,(-decScore,vertexScore[elem[0]],elem[2]))
            totalScore += vertexScore[elem[0]] * elem[2]
        else:
            decScore = elem[2] % 2 + elem[2] // 2
            decScore *= vertexScore[elem[1]]
            heapq.heappush(edgeHeapQ,(-decScore,vertexScore[elem[1]],elem[2]))
            totalScore += vertexScore[elem[1]] * elem[2]
    cnt = 0
    while totalScore > s:
        elem = edgeHeapQ[0]
        newWeight = elem[2] // 2
        newDecScore = newWeight % 2 + newWeight // 2
        newDecScore *= elem[1]
        elem = heapq.heappushpop(edgeHeapQ,(-newDecScore,elem[1],newWeight))
        totalScore += elem[0]
        cnt += 1
    print(cnt)


# Fast IO (only use in integer input)

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

import heapq

t = int(input())

for _ in range(t):
    n,s = map(int,input().split())
    vertexScore = [] # number of leaves
    motherEdge = []
    edgeList = []
    inputEdgeList = []
    for _ in range(n):
        edgeList.append([])
        motherEdge.append(-1)
        vertexScore.append(0)
    for _ in range(n-1):
        r,m,w = map(int,input().split())
        inputEdgeList.append((r-1,m-1,w))
        edgeList[r-1].append(m-1)
        edgeList[m-1].append(r-1)
    isAdded = [False] * n
    isAdded[0] = True
    vertexStack = [(0,-1)]
    vertexScoreStack = [] # vertex whose daughters have vertexScore calculated
    daughterCountList = [0] * n
    while vertexStack:
        elem = vertexStack.pop()
        motherEdge[elem[0]] = elem[1]
        daughterCount = 0
        for vertex in edgeList[elem[0]]:
            if not isAdded[vertex]:
                isAdded[vertex] = True
                daughterCount += 1
                vertexStack.append((vertex,elem[0]))
        daughterCountList[elem[0]] = daughterCount
        if daughterCount == 0:
            vertexScoreStack.append(elem[0])
    while vertexScoreStack:
        score = 0
        vertex = vertexScoreStack.pop()
        for daughter in edgeList[vertex]:
            if daughter != motherEdge[vertex]:
                score += vertexScore[daughter]
        if score == 0:
            score = 1
        vertexScore[vertex] = score
        if motherEdge[vertex] != -1:
            daughterCountList[motherEdge[vertex]] -= 1
            if daughterCountList[motherEdge[vertex]] == 0:
                vertexScoreStack.append(motherEdge[vertex])
    decrementList = []
    totalScore = 0
    for elem in inputEdgeList:
        if motherEdge[elem[0]] == elem[1]:
            weight = elem[2]
            while weight > 0:
                decScore = weight % 2 + weight // 2
                decScore *= vertexScore[elem[0]]
                decrementList.append(-decScore)
                weight //= 2
            totalScore += vertexScore[elem[0]] * elem[2]
        else:
            weight = elem[2]
            while weight > 0:
                decScore = weight % 2 + weight // 2
                decScore *= vertexScore[elem[1]]
                decrementList.append(-decScore)
                weight //= 2
            totalScore += vertexScore[elem[1]] * elem[2]
    decrementList.sort()
    cnt = 0
    while totalScore > s:
        totalScore += decrementList[cnt]
        cnt += 1
    print(cnt)


# Fast IO (only use in integer input)

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

import heapq

t = int(input())

for _ in range(t):
    n,s = map(int,input().split())
    vertexScore = [] # number of leaves
    motherEdge = []
    edgeList = []
    inputEdgeList = []
    for _ in range(n):
        edgeList.append([])
        motherEdge.append(-1)
        vertexScore.append(0)
    for _ in range(n-1):
        r,m,w = map(int,input().split())
        inputEdgeList.append((r-1,m-1,w))
        edgeList[r-1].append(m-1)
        edgeList[m-1].append(r-1)
    isAdded = [False] * n
    isAdded[0] = True
    vertexStack = [(0,-1)]
    vertexScoreStack = [] # vertex whose daughters have vertexScore calculated
    daughterCountList = [0] * n
    while vertexStack:
        elem = vertexStack.pop()
        motherEdge[elem[0]] = elem[1]
        daughterCount = 0
        for vertex in edgeList[elem[0]]:
            if not isAdded[vertex]:
                isAdded[vertex] = True
                daughterCount += 1
                vertexStack.append((vertex,elem[0]))
        daughterCountList[elem[0]] = daughterCount
        if daughterCount == 0:
            vertexScoreStack.append(elem[0])
    while vertexScoreStack:
        score = 0
        vertex = vertexScoreStack.pop()
        for daughter in edgeList[vertex]:
            if daughter != motherEdge[vertex]:
                score += vertexScore[daughter]
        if score == 0:
            score = 1
        vertexScore[vertex] = score
        if motherEdge[vertex] != -1:
            daughterCountList[motherEdge[vertex]] -= 1
            if daughterCountList[motherEdge[vertex]] == 0:
                vertexScoreStack.append(motherEdge[vertex])
    edgeHeapQ = [] # (decremented score, multiplier, weight)
    totalScore = 0
    for elem in inputEdgeList:
        if motherEdge[elem[0]] == elem[1]:
            decScore = elem[2] % 2 + elem[2] // 2
            decScore *= vertexScore[elem[0]]
            heapq.heappush(edgeHeapQ,(-decScore,vertexScore[elem[0]],elem[2]))
            totalScore += vertexScore[elem[0]] * elem[2]
        else:
            decScore = elem[2] % 2 + elem[2] // 2
            decScore *= vertexScore[elem[1]]
            heapq.heappush(edgeHeapQ,(-decScore,vertexScore[elem[1]],elem[2]))
            totalScore += vertexScore[elem[1]] * elem[2]
    cnt = 0
    while totalScore > s:
        elem = edgeHeapQ[0]
        newWeight = elem[2] // 2
        newDecScore = newWeight % 2 + newWeight // 2
        newDecScore *= elem[1]
        elem = heapq.heappop(edgeHeapQ)
        heapq.heappush(edgeHeapQ,(-newDecScore,elem[1],newWeight))
        totalScore += elem[0]
        cnt += 1
    print(cnt)


import heapq

t = int(input())

for _ in range(t):
    n,s = map(int,input().split())
    vertexScore = [] # number of leaves
    motherEdge = []
    edgeList = []
    inputEdgeList = []
    for _ in range(n):
        edgeList.append([])
        motherEdge.append(-1)
        vertexScore.append(0)
    for _ in range(n-1):
        r,m,w = map(int,input().split())
        inputEdgeList.append((r-1,m-1,w))
        edgeList[r-1].append(m-1)
        edgeList[m-1].append(r-1)
    isAdded = [False] * n
    isAdded[0] = True
    vertexStack = [(0,-1)]
    vertexScoreStack = [] # vertex whose daughters have vertexScore calculated
    daughterCountList = [0] * n
    while vertexStack:
        elem = vertexStack.pop()
        motherEdge[elem[0]] = elem[1]
        daughterCount = 0
        for vertex in edgeList[elem[0]]:
            if not isAdded[vertex]:
                isAdded[vertex] = True
                daughterCount += 1
                vertexStack.append((vertex,elem[0]))
        daughterCountList[elem[0]] = daughterCount
        if daughterCount == 0:
            vertexScoreStack.append(elem[0])
    while vertexScoreStack:
        score = 0
        vertex = vertexScoreStack.pop()
        for daughter in edgeList[vertex]:
            if daughter != motherEdge[vertex]:
                score += vertexScore[daughter]
        if score == 0:
            score = 1
        vertexScore[vertex] = score
        if motherEdge[vertex] != -1:
            daughterCountList[motherEdge[vertex]] -= 1
            if daughterCountList[motherEdge[vertex]] == 0:
                vertexScoreStack.append(motherEdge[vertex])
    edgeHeapQ = [] # (decremented score, multiplier, weight)
    totalScore = 0
    for elem in inputEdgeList:
        if motherEdge[elem[0]] == elem[1]:
            decScore = elem[2] % 2 + elem[2] // 2
            decScore *= vertexScore[elem[0]]
            heapq.heappush(edgeHeapQ,(-decScore,vertexScore[elem[0]],elem[2]))
            totalScore += vertexScore[elem[0]] * elem[2]
        else:
            decScore = elem[2] % 2 + elem[2] // 2
            decScore *= vertexScore[elem[1]]
            heapq.heappush(edgeHeapQ,(-decScore,vertexScore[elem[1]],elem[2]))
            totalScore += vertexScore[elem[1]] * elem[2]
    cnt = 0
    while totalScore > s:
        elem = edgeHeapQ[0]
        newWeight = elem[2] // 2
        print(newWeight)
        newDecScore = newWeight % 2 + newWeight // 2
        newDecScore *= elem[1]
        elem = heapQ.heappop(edgeHeapQ)
        heapq.heappush(edgeHeapQ,(-newDecScore,elem[1],newWeight))
        totalScore += elem[0]
        cnt += 1
    print(cnt)


import heapq

t = int(input())

for _ in range(t):
    n,s = map(int,input().split())
    vertexScore = [] # number of leaves
    motherEdge = []
    edgeList = []
    inputEdgeList = []
    for _ in range(n):
        edgeList.append([])
        motherEdge.append(-1)
        vertexScore.append(0)
    for _ in range(n-1):
        r,m,w = map(int,input().split())
        inputEdgeList.append((r-1,m-1,w))
        edgeList[r-1].append(m-1)
        edgeList[m-1].append(r-1)
    isAdded = [False] * n
    isAdded[0] = True
    vertexStack = [(0,-1)]
    vertexScoreStack = [] # vertex whose daughters have vertexScore calculated
    daughterCountList = [0] * n
    while vertexStack:
        elem = vertexStack.pop()
        motherEdge[elem[0]] = elem[1]
        daughterCount = 0
        for vertex in edgeList[elem[0]]:
            if not isAdded[vertex]:
                isAdded[vertex] = True
                daughterCount += 1
                vertexStack.append((vertex,elem[0]))
        daughterCountList[elem[0]] = daughterCount
        if daughterCount == 0:
            vertexScoreStack.append(elem[0])
    while vertexScoreStack:
        score = 0
        vertex = vertexScoreStack.pop()
        for daughter in edgeList[elem[0]]:
            if daughter != motherEdge[vertex]:
                score += vertexScore[daughter]
        if score == 0:
            score = 1
        vertexScore[vertex] = score
        if motherEdge[vertex] != -1:
            daughterCountList[motherEdge[vertex]] -= 1
            if daughterCountList[motherEdge[vertex]] == 0:
                vertexScoreStack.append(motherEdge[vertex])
    edgeHeapQ = [] # (decremented score, multiplier, weight)
    totalScore = 0
    for elem in inputEdgeList:
        if motherEdge[elem[0]] == elem[1]:
            decScore = elem[2] % 2 + elem[2] // 2
            decScore *= vertexScore[elem[0]]
            heapq.heappush(edgeHeapQ,(-decScore,vertexScore[elem[0]],elem[2]))
            totalScore += vertexScore[elem[0]] * elem[2]
        else:
            decScore = elem[2] % 2 + elem[2] // 2
            decScore *= vertexScore[elem[1]]
            heapq.heappush(edgeHeapQ,(-decScore,vertexScore[elem[1]],elem[2]))
            totalScore += vertexScore[elem[1]] * elem[2]
    cnt = 0
    while totalScore > s:
        elem = edgeHeapQ[0]
        newWeight = elem[2] // 2
        newDecScore = newWeight % 2 + newWeight // 2
        newDecScore *= vertexScore[elem[1]]
        elem = heapq.heappushpop(edgeHeapQ,(-newDecScore,vertexScore[elem[1]],newWeight))
        totalScore += elem[0]
        cnt += 1
    print(cnt)


import heapq

t = int(input())

for _ in range(t):
    n,s = map(int,input().split())
    vertexScore = [] # number of leaves
    motherEdge = []
    edgeList = []
    inputEdgeList = []
    for _ in range(n):
        edgeList.append([])
        motherEdge.append(-1)
        vertexScore.append(0)
    for _ in range(n-1):
        r,m,w = map(int,input().split())
        inputEdgeList.append((r-1,m-1,w))
        edgeList[r-1].append(m-1)
        edgeList[m-1].append(r-1)
    isAdded = [False] * n
    isAdded[0] = True
    vertexStack = [(0,-1)]
    vertexScoreStack = [] # vertex whose daughters have vertexScore calculated
    daughterCountList = [0] * n
    while vertexStack:
        elem = vertexStack.pop()
        motherEdge[elem[0]] = elem[1]
        daughterCount = 0
        for vertex in edgeList[elem[0]]:
            if not isAdded[vertex]:
                isAdded[vertex] = True
                daughterCount += 1
                vertexStack.append((vertex,elem[0]))
        daughterCountList[elem[0]] = daughterCount
        if daughterCount == 0:
            vertexScoreStack.append(elem[0])
    while vertexScoreStack:
        score = 0
        vertex = vertexScoreStack.pop()
        for daughter in edgeList[vertex]:
            if daughter != motherEdge[vertex]:
                score += vertexScore[daughter]
        if score == 0:
            score = 1
        vertexScore[vertex] = score
        if motherEdge[vertex] != -1:
            daughterCountList[motherEdge[vertex]] -= 1
            if daughterCountList[motherEdge[vertex]] == 0:
                vertexScoreStack.append(motherEdge[vertex])
    edgeHeapQ = [] # (decremented score, multiplier, weight)
    totalScore = 0
    for elem in inputEdgeList:
        if motherEdge[elem[0]] == elem[1]:
            decScore = elem[2] % 2 + elem[2] // 2
            decScore *= vertexScore[elem[0]]
            heapq.heappush(edgeHeapQ,(-decScore,vertexScore[elem[0]],elem[2]))
            totalScore += vertexScore[elem[0]] * elem[2]
        else:
            decScore = elem[2] % 2 + elem[2] // 2
            decScore *= vertexScore[elem[1]]
            heapq.heappush(edgeHeapQ,(-decScore,vertexScore[elem[1]],elem[2]))
            totalScore += vertexScore[elem[1]] * elem[2]
    cnt = 0
    while totalScore > s:
        elem = edgeHeapQ[0]
        newWeight = elem[2] // 2
        newDecScore = newWeight % 2 + newWeight // 2
        newDecScore *= elem[1]
        elem = heapq.heappushpop(edgeHeapQ,(-newDecScore,elem[1],newWeight))
        totalScore += elem[0]
        cnt += 1
    print(cnt)


import heapq

t = int(input())

for _ in range(t):
    n,s = map(int,input().split())
    vertexScore = [] # number of leaves
    motherEdge = []
    edgeList = []
    inputEdgeList = []
    for _ in range(n):
        edgeList.append([])
        motherEdge.append(-1)
        vertexScore.append(0)
    for _ in range(n-1):
        r,m,w = map(int,input().split())
        inputEdgeList.append((r-1,m-1,w))
        edgeList[r-1].append(m-1)
        edgeList[m-1].append(r-1)
    isAdded = [False] * n
    isAdded[0] = True
    vertexStack = [(0,-1)]
    vertexScoreStack = [] # vertex whose daughters have vertexScore calculated
    daughterCountList = [0] * n
    while vertexStack:
        elem = vertexStack.pop()
        motherEdge[elem[0]] = elem[1]
        daughterCount = 0
        for vertex in edgeList[elem[0]]:
            if not isAdded[vertex]:
                isAdded[vertex] = True
                daughterCount += 1
                vertexStack.append((vertex,elem[0]))
        daughterCountList[elem[0]] = daughterCount
        if daughterCount == 0:
            vertexScoreStack.append(elem[0])
    while vertexScoreStack:
        score = 0
        vertex = vertexScoreStack.pop()
        for daughter in edgeList[elem[0]]:
            if daughter != motherEdge[vertex]:
                score += vertexScore[daughter]
        if score == 0:
            score = 1
        vertexScore[vertex] = score
        if motherEdge[vertex] != -1:
            daughterCountList[motherEdge[vertex]] -= 1
            if daughterCountList[motherEdge[vertex]] == 0:
                vertexScoreStack.append(motherEdge[vertex])
    edgeHeapQ = [] # (decremented score, multiplier, weight)
    totalScore = 0
    for elem in inputEdgeList:
        if motherEdge[elem[0]] == elem[1]:
            decScore = elem[2] % 2 + elem[2] // 2
            decScore *= vertexScore[elem[0]]
            heapq.heappush(edgeHeapQ,(-decScore,vertexScore[elem[0]],elem[2]))
            totalScore += vertexScore[elem[0]] * elem[2]
        else:
            decScore = elem[2] % 2 + elem[2] // 2
            decScore *= vertexScore[elem[1]]
            heapq.heappush(edgeHeapQ,(-decScore,vertexScore[elem[1]],elem[2]))
            totalScore += vertexScore[elem[1]] * elem[2]
    cnt = 0
    while totalScore > s:
        elem = edgeHeapQ[0]
        newWeight = elem[2] // 2
        newDecScore = newWeight % 2 + newWeight // 2
        newDecScore *= elem[1]
        elem = heapq.heappushpop(edgeHeapQ,(-newDecScore,vertexScore[elem[1]],newWeight))
        totalScore += elem[0]
        cnt += 1
    print(cnt)


import heapq

t = int(input())

for _ in range(t):
    n,s = map(int,input().split())
    vertexScore = [] # number of leaves
    motherEdge = []
    edgeList = []
    inputEdgeList = []
    for _ in range(n):
        edgeList.append([])
        motherEdge.append(-1)
        vertexScore.append(0)
    for _ in range(n-1):
        r,m,w = map(int,input().split())
        inputEdgeList.append((r-1,m-1,w))
        edgeList[r-1].append(m-1)
        edgeList[m-1].append(r-1)
    isAdded = [False] * n
    isAdded[0] = True
    vertexStack = [(0,-1)]
    vertexScoreStack = [] # vertex whose daughters have vertexScore calculated
    daughterCountList = [0] * n
    while vertexStack:
        elem = vertexStack.pop()
        motherEdge[elem[0]] = elem[1]
        daughterCount = 0
        for vertex in edgeList[elem[0]]:
            if not isAdded[vertex]:
                isAdded[vertex] = True
                daughterCount += 1
                vertexStack.append((vertex,elem[0]))
        daughterCountList[elem[0]] = daughterCount
        if daughterCount == 0:
            vertexScoreStack.append(elem[0])
    while vertexScoreStack:
        score = 0
        vertex = vertexScoreStack.pop()
        for daughter in edgeList[vertex]:
            if daughter != motherEdge[vertex]:
                score += vertexScore[daughter]
        if score == 0:
            score = 1
        vertexScore[vertex] = score
        if motherEdge[vertex] != -1:
            daughterCountList[motherEdge[vertex]] -= 1
            if daughterCountList[motherEdge[vertex]] == 0:
                vertexScoreStack.append(motherEdge[vertex])
    edgeHeapQ = [] # (decremented score, multiplier, weight)
    totalScore = 0
    for elem in inputEdgeList:
        if motherEdge[elem[0]] == elem[1]:
            decScore = elem[2] % 2 + elem[2] // 2
            decScore *= vertexScore[elem[0]]
            heapq.heappush(edgeHeapQ,(-decScore,vertexScore[elem[0]],elem[2]))
            totalScore += vertexScore[elem[0]] * elem[2]
        else:
            decScore = elem[2] % 2 + elem[2] // 2
            decScore *= vertexScore[elem[1]]
            heapq.heappush(edgeHeapQ,(-decScore,vertexScore[elem[1]],elem[2]))
            totalScore += vertexScore[elem[1]] * elem[2]
    cnt = 0
    while totalScore > s:
        elem = edgeHeapQ[0]
        newWeight = elem[2] // 2
        newDecScore = newWeight % 2 + newWeight // 2
        newDecScore *= elem[1]
        elem = heapq.heappop(edgeHeapQ)
        heapq.heappush(edgeHeapQ,(-newDecScore,elem[1],newWeight))
        totalScore += elem[0]
        cnt += 1
    print(cnt)


from sys import stdin, gettrace
from collections import deque
from heapq import heapify, heappop, heappush

if gettrace():
    inputi = input
else:
    def input():
        return next(stdin)[:-1]

    def inputi():
        return stdin.buffer.readline()

def readGraph(n, m):
    adj = [set() for _ in range(n)]
    for _ in range(m):
        u,v,w= map(int, inputi().split())
        adj[u-1].add((v-1, w))
        adj[v-1].add((u-1, w))
    return adj

def treeOrderByDepth(n, adj, root=0):
    weight = [0]*n
    parent = [-1]*n
    parent[root] = -2
    ordered = []
    q = deque()
    q.append(root)
    while q:
        c =q.popleft()
        ordered.append(c)
        for a,w in adj[c]:
            if parent[a] == -1:
                parent[a] = c
                weight[a] = w
                q.append(a)
    return ordered, parent, weight

def readTree(n):
    return readGraph(n, n-1)

def solve():
    n,s = map(int, inputi().split())
    adj = readTree(n)
    ordered, parent, weight = treeOrderByDepth(n, adj)
    leaves = {i for i in range(1,n) if len(adj[i]) == 1}
    lcount = [1 if i in leaves else 0 for i in range(n)]
    value = [[0, i] for i in range(n)]
    tvalue = 0
    for i in ordered[:0:-1]:
        value[i][0] = -((weight[i]+1)//2 * lcount[i])
        tvalue += weight[i] * lcount[i]
        lcount[parent[i]] += lcount[i]
    heapify(value)
    count = 0
    while tvalue > s:
        v, i = heappop(value)
        while v > s or (v >= value[0][0] and tvalue > s):
            tvalue += v
            weight[i] //= 2
            count+=1
            v = -((weight[i]+1)//2 * lcount[i])
        heappush(value, [v, i])
    print(count)

def main():
    t = int(inputi())
    for _ in range(t):
        solve()

if __name__ == "__main__":
    main()


from sys import stdin, gettrace
from collections import deque
from heapq import heapify, heappop, heappush

if gettrace():
    inputi = input
else:
    def input():
        return next(stdin)[:-1]

    def inputi():
        return stdin.buffer.readline()

def readGraph(n, m):
    adj = [set() for _ in range(n)]
    for _ in range(m):
        u,v,w= map(int, inputi().split())
        adj[u-1].add((v-1, w))
        adj[v-1].add((u-1, w))
    return adj

def treeOrderByDepth(n, adj, root=0):
    weight = [0]*n
    parent = [-1]*n
    parent[root] = -2
    ordered = []
    q = deque()
    q.append(root)
    while q:
        c =q.popleft()
        ordered.append(c)
        for a,w in adj[c]:
            if parent[a] == -1:
                parent[a] = c
                weight[a] = w
                q.append(a)
    return ordered, parent, weight

def readTree(n):
    return readGraph(n, n-1)

def solve():
    n,s = map(int, inputi().split())
    adj = readTree(n)
    ordered, parent, weight = treeOrderByDepth(n, adj)
    leaves = {i for i in range(1,n) if len(adj[i]) == 1}
    lcount = [1 if i in leaves else 0 for i in range(n)]
    value = [[0, i] for i in range(n)]
    tvalue = 0
    for i in ordered[:0:-1]:
        value[i][0] = -(weight[i] * lcount[i])
        tvalue += weight[i] * lcount[i]
        lcount[parent[i]] += lcount[i]
    heapify(value)
    count = 0
    while tvalue > s:
        count+=1
        v, i = heappop(value)
        tvalue += v
        weight[i] //= 2
        v = -(weight[i] * lcount[i])
        tvalue -= v
        heappush(value, [v, i])
    print(count)

def main():
    t = int(inputi())
    for _ in range(t):
        solve()

if __name__ == "__main__":
    main()


from sys import stdin, gettrace
from collections import deque
from heapq import heapify, heappop, heappush

if gettrace():
    inputi = input
else:
    def input():
        return next(stdin)[:-1]

    def inputi():
        return stdin.buffer.readline()

def readGraph(n, m):
    adj = [set() for _ in range(n)]
    for _ in range(m):
        u,v,w= map(int, inputi().split())
        adj[u-1].add((v-1, w))
        adj[v-1].add((u-1, w))
    return adj

def treeOrderByDepth(n, adj, root=0):
    weight = [0]*n
    parent = [-1]*n
    parent[root] = -2
    ordered = []
    q = deque()
    q.append(root)
    while q:
        c =q.popleft()
        ordered.append(c)
        for a,w in adj[c]:
            if parent[a] == -1:
                parent[a] = c
                weight[a] = w
                q.append(a)
    return ordered, parent, weight

def readTree(n):
    return readGraph(n, n-1)

def solve():
    n,s = map(int, inputi().split())
    adj = readTree(n)
    ordered, parent, weight = treeOrderByDepth(n, adj)
    leaves = {i for i in range(1,n) if len(adj[i]) == 1}
    lcount = [1 if i in leaves else 0 for i in range(n)]
    value = [[0, i] for i in range(n)]
    tvalue = 0
    for i in ordered[:0:-1]:
        value[i][0] = -((weight[i]+1)//2 * lcount[i])
        tvalue += weight[i] * lcount[i]
        lcount[parent[i]] += lcount[i]
    heapify(value)
    count = 0
    while tvalue > s:
        v, i = heappop(value)
        while (-v) > s or (v <= value[0][0] and tvalue > s):
            tvalue += v
            weight[i] //= 2
            count+=1
            v = -((weight[i]+1)//2 * lcount[i])
        heappush(value, [v, i])
    print(count)

def main():
    t = int(inputi())
    for _ in range(t):
        solve()

if __name__ == "__main__":
    main()


from sys import stdin, gettrace
from collections import deque
from heapq import heapify, heappop, heappush

if gettrace():
    inputi = input
else:
    def input():
        return next(stdin)[:-1]

    def inputi():
        return stdin.buffer.readline()

def readGraph(n, m):
    adj = [set() for _ in range(n)]
    for _ in range(m):
        u,v,w= map(int, inputi().split())
        adj[u-1].add((v-1, w))
        adj[v-1].add((u-1, w))
    return adj

def treeOrderByDepth(n, adj, root=0):
    weight = [0]*n
    parent = [-1]*n
    parent[root] = -2
    ordered = []
    q = deque()
    q.append(root)
    while q:
        c =q.popleft()
        ordered.append(c)
        for a,w in adj[c]:
            if parent[a] == -1:
                parent[a] = c
                weight[a] = w
                q.append(a)
    return ordered, parent, weight

def readTree(n):
    return readGraph(n, n-1)

def solve():
    n,s = map(int, inputi().split())
    adj = readTree(n)
    ordered, parent, weight = treeOrderByDepth(n, adj)
    leaves = {i for i in range(1,n) if len(adj[i]) == 1}
    lcount = [1 if i in leaves else 0 for i in range(n)]
    value = [[0, i] for i in range(n)]
    tvalue = 0
    for i in ordered[:0:-1]:
        value[i][0] = -(weight[i]//2 * lcount[i])
        tvalue += weight[i] * lcount[i]
        lcount[parent[i]] += lcount[i]
    heapify(value)
    count = 0
    while tvalue > s:
        count+=1
        v, i = heappop(value)
        tvalue += v
        weight[i] //= 2
        v = -(weight[i] * lcount[i])
        tvalue -= v
        heappush(value, [v, i])
    print(count)

def main():
    t = int(inputi())
    for _ in range(t):
        solve()

if __name__ == "__main__":
    main()


from sys import stdin, gettrace
from collections import deque
from heapq import heapify, heappop, heappush

if gettrace():
    inputi = input
else:
    def input():
        return next(stdin)[:-1]

    def inputi():
        return stdin.buffer.readline()

def readGraph(n, m):
    adj = [set() for _ in range(n)]
    for _ in range(m):
        u,v,w= map(int, inputi().split())
        adj[u-1].add((v-1, w))
        adj[v-1].add((u-1, w))
    return adj

def treeOrderByDepth(n, adj, root=0):
    weight = [0]*n
    parent = [-1]*n
    parent[root] = -2
    ordered = []
    q = deque()
    q.append(root)
    while q:
        c =q.popleft()
        ordered.append(c)
        for a,w in adj[c]:
            if parent[a] == -1:
                parent[a] = c
                weight[a] = w
                q.append(a)
    return ordered, parent, weight

def readTree(n):
    return readGraph(n, n-1)

def solve():
    n,s = map(int, inputi().split())
    adj = readTree(n)
    ordered, parent, weight = treeOrderByDepth(n, adj)
    leaves = {i for i in range(1,n) if len(adj[i]) == 1}
    lcount = [1 if i in leaves else 0 for i in range(n)]
    value = [[0, i] for i in range(n)]
    tvalue = 0
    for i in ordered[:0:-1]:
        value[i][0] = -((weight[i]+1)//2 * lcount[i])
        tvalue += weight[i] * lcount[i]
        lcount[parent[i]] += lcount[i]
    heapify(value)
    count = 0
    while tvalue > s:
        count+=1
        v, i = heappop(value)
        tvalue += v
        weight[i] //= 2
        v = -((weight[i]+1)//2 * lcount[i])
        heappush(value, [v, i])
    print(count)

def main():
    t = int(inputi())
    for _ in range(t):
        solve()

if __name__ == "__main__":
    main()


from sys import stdin, gettrace
from collections import deque
from heapq import heapify, heappop, heappush

if gettrace():
    inputi = input
else:
    def input():
        return next(stdin)[:-1]

    def inputi():
        return stdin.buffer.readline()

def readGraph(n, m):
    adj = [set() for _ in range(n)]
    for _ in range(m):
        u,v,w= map(int, inputi().split())
        adj[u-1].add((v-1, w))
        adj[v-1].add((u-1, w))
    return adj

def treeOrderByDepth(n, adj, root=0):
    weight = [0]*n
    parent = [-1]*n
    parent[root] = -2
    ordered = []
    q = deque()
    q.append(root)
    while q:
        c =q.popleft()
        ordered.append(c)
        for a,w in adj[c]:
            if parent[a] == -1:
                parent[a] = c
                weight[a] = w
                q.append(a)
    return ordered, parent, weight

def readTree(n):
    return readGraph(n, n-1)

def solve():
    n,s = map(int, inputi().split())
    adj = readTree(n)
    ordered, parent, weight = treeOrderByDepth(n, adj)
    leaves = {i for i in range(1,n) if len(adj[i]) == 1}
    lcount = [1 if i in leaves else 0 for i in range(n)]
    value = [[0, i] for i in range(n)]
    tvalue = 0
    for i in ordered[:0:-1]:
        value[i][0] = -((weight[i]+1)//2 * lcount[i])
        tvalue += weight[i] * lcount[i]
        lcount[parent[i]] += lcount[i]
    heapify(value)
    count = 0
    while tvalue > s:
        count+=1
        v, i = heappop(value)
        tvalue += v
        weight[i] //= 2
        if weight[i] > 0:
            v = -((weight[i]+1)//2 * lcount[i])
            heappush(value, [v, i])
    print(count)

def main():
    t = int(inputi())
    for _ in range(t):
        solve()

if __name__ == "__main__":
    main()


import java.io.BufferedReader

data class Edge(val v: Int, var w: Long)

fun main() {
    val r = System.`in`.bufferedReader()
    val sb = StringBuilder()
    repeat(r.readInt()) {
        //println("Test case: $it")
        val n = r.readInt()
        val s = r.readInt().toLong()
        val adj = List(n) { mutableListOf<Edge>() }
        repeat(n - 1) {
            val u = r.readInt() - 1
            val v = r.readInt() - 1
            val w = r.readInt().toLong()
            adj[u] += Edge(v, w)
            adj[v] += Edge(u, w)
        }
        val visited = BooleanArray(n) { false }

        // Need leaf number, it is important. Another one is weight, other thing does not matter
        val leaves = arrayOfNulls<Int>(n)
        val weight = arrayOfNulls<Long>(n)
        //val edges = mutableListOf<Edge>()

        //var i = 0
        // return leaf
        weight[0] = 0
        fun dfs(v: Int): Int {
            visited[v] = true
            var leaf = 0
            // back edge, this is leaf
            if (adj[v].size == 1 && visited[adj[v][0].v]) {
                leaf++
            } else {
                // Count up child vertices's leaf
                for (e in adj[v]) {
                    if (!visited[e.v]) {
                        //edges += e
                        weight[e.v] = e.w
                        leaf += dfs(e.v)
                    }
                }
            }
            leaves[v] = leaf
            return leaf
        }
        dfs(0)

        val a = mutableListOf<Long>()
        var cursum = 0L
        /*for ((v, weight) in edges) {
            val l = leaves[v]!!
            var w = weight
            var diff = (w + 1 shr 1) * l
            while (diff > 0) {
                a.add(diff)
                w /= 2
                diff = (w + 1 shr 1) * l
            }
        }*/
        for (i in 1..n - 1) {
            var w = weight[i]!!
            val l = leaves[i]!!
            cursum += w * l
            while (w != 0L) {
                a.add((w + 1 shr 1) * l)
                w /= 2
            }
        }
        //var cursum: Long = edges.fold(0L) { acc, (v, w) -> acc + w * leaves[v]!! }
        //var cursum = a.sum()
        a.sortDescending()
        //println("can remove: $a")
        var b = 0
        while (cursum > s) {
            cursum -= a[b]
            b++
        }
        sb.appendln(b)
    }
    print(sb)
}

// Hakiobo's code for faster reading input
private const val SPACE_INT = ' '.toInt()
private const val ZERO_INT = '0'.toInt()
private const val NL_INT = '\n'.toInt()

private fun BufferedReader.readInt(): Int {
    var ret = read()
    while (ret <= SPACE_INT) {
        ret = read()
    }
    val neg = ret == '-'.toInt()
    if (neg) {
        ret = read()
    }
    ret -= ZERO_INT
    var read = read()
    while (read >= ZERO_INT) {
        ret *= 10
        ret += read - ZERO_INT
        read = read()
    }

    while (read <= SPACE_INT && read != -1 && read != NL_INT) {
        mark(1)
        read = read()
    }
    if (read > SPACE_INT) {
        reset()
    }
    return if (neg) -ret else ret
}


import java.io.BufferedReader

data class Edge(val v: Int, var w: Long)

fun main() {
    val r = System.`in`.bufferedReader()
    val sb = StringBuilder()
    repeat(r.readInt()) {
        //println("Test case: $it")
        val n = r.readInt()
        val s = r.readInt().toLong()
        val adj = List(n) { mutableListOf<Edge>() }
        repeat(n - 1) {
            val u = r.readInt() - 1
            val v = r.readInt() - 1
            val w = r.readInt().toLong()
            adj[u] += Edge(v, w)
            adj[v] += Edge(u, w)
        }
        val visited = BooleanArray(n) { false }

        // Need leaf number, it is important. Another one is weight, other thing does not matter
        val leaves = arrayOfNulls<Int>(n)
        val edges = mutableListOf<Edge>()
        //var i = 0
        fun dfs(v: Int): Int {
            visited[v] = true
            var leaf = 0
            // back edge, this is leaf
            if (adj[v].size == 1 && visited[adj[v][0].v]) {
                leaf++
            } else {
                // Count up child vertices's leaf
                for (e in adj[v]) {
                    if (!visited[e.v]) {
                        edges += e
                        leaf += dfs(e.v)
                    }
                }
            }
            leaves[v] = leaf
            return leaf
        }
        dfs(0)

        val a = mutableListOf<Long>()
        var cursum = edges.fold(0L) { acc, (v, w) -> acc + w * leaves[v]!! }

        for ((v, weight) in edges) {
            val l = leaves[v]!!
            var w = weight
            var diff = (w - (w / 2)) * l
            while (diff>0) {
                a.add(diff)
                w /= 2
                diff = (w - (w / 2)) * l
            }
        }
        a.sortDescending()
        //println("can remove: $a")
        var b = 0
        while (cursum > s) {
            cursum -= a[b++]
        }
        sb.appendln(b)
    }
    print(sb)
}

// Hakiobo's code for faster reading input
private const val SPACE_INT = ' '.toInt()
private const val ZERO_INT = '0'.toInt()
private const val NL_INT = '\n'.toInt()

private fun BufferedReader.readInt(): Int {
    var ret = read()
    while (ret <= SPACE_INT) {
        ret = read()
    }
    val neg = ret == '-'.toInt()
    if (neg) {
        ret = read()
    }
    ret -= ZERO_INT
    var read = read()
    while (read >= ZERO_INT) {
        ret *= 10
        ret += read - ZERO_INT
        read = read()
    }

    while (read <= SPACE_INT && read != -1 && read != NL_INT) {
        mark(1)
        read = read()
    }
    if (read > SPACE_INT) {
        reset()
    }
    return if (neg) -ret else ret
}


//package com.example.codetest

import java.io.BufferedReader
import java.util.*
import kotlin.collections.ArrayList

class BinaryHeap<WeightedEdge : Comparable<WeightedEdge>> {

    // A dynamic array to track the elements inside the heap
    private lateinit var heap: ArrayList<WeightedEdge>

    // Construct a priority queue with an initial capacity, default capacity is 1
    constructor(sz: Int = 1) {
        heap = ArrayList(sz)
    }

    // Construct a priority queue using heapify in O(n) time,
    constructor(vararg elems: WeightedEdge) {

        val heapSize = elems.size
        heap = ArrayList(heapSize)

        // Place all element in heap
        heap.addAll(elems)

        // Heapify process, O(n)
        (maxOf(0, heapSize / 2 - 1) downTo 0).forEach { sink(it) }
    }

    // Construct a priority queue
    constructor(elems: Collection<WeightedEdge>) {

        val heapSize = elems.size
        heap = ArrayList(heapSize)

        // Place all element of the given collection to the heap
        heap.addAll(elems)

        // Heapify process, O(n)
        (maxOf(0, heapSize / 2 - 1) downTo 0).forEach { sink(it) }
    }

    // Returns true/false depending on if the priority queue is empty
    private fun isEmpty() = size() == 0

    // Clears everything inside the heap O(n)
    fun clear() {
        heap.clear()
    }

    // Return the size of teh heap
    private fun size() = heap.size

    // Return the value of the element with the lowest priority in this priority queue,
    // If the priority queue is empty, null is returned.
    fun peek(): WeightedEdge? = if (isEmpty()) null else heap[0]

    // Removes the root of the heap, O(log(n))
    fun poll() = removeAt(0)

    // Test if an element is in the heap, O(n)
    fun contains(elem: WeightedEdge) = heap.contains(elem)

    // Adds an element to the priority queue, O(log(n))
    fun add(elem: WeightedEdge) {

        heap.add(elem)

        val indexOfLastElem = size() - 1
        swim(indexOfLastElem)
    }

    // Test if the value of node i <= node j, assume i & j are valid indices, O(1)
    private fun less(i: Int, j: Int) = heap[i] <= heap[j]

    // Perform bottom up node swim, O(log(n))
    private fun swim(k: Int) {

        var child = k

        // Grab the index of the next parent node WRT to child
        var parent = (child - 1) / 2

        // Keep swimming while we have not reached the root and while
        // we're less than our parent
        while (child > 0 && less(child, parent)) {

            // Exchange child with parent
            swap(parent, child)

            // Get new child and parent
            child = parent
            parent = (child - 1) / 2
        }
    }

    // Top down node sink, O(log(n))
    private fun sink(k: Int) {
        var parent = k
        val heapSize = size()
        while (true) {
            val left = 2 * parent + 1 // Left node
            val right = 2 * parent + 2 // Right node
            var smallest = left // Assume left is the smallest node of the two children

            // Find which is smaller ? left or right
            if (right < heapSize && less(right, left)) smallest = right

            // Stop if we're outside the bounds of the tree
            // or stop early if we cannot sink k any more
            if (left >= heapSize || less(parent, smallest)) break

            // Move down the tree following the smallest node
            swap(parent, smallest)
            parent = smallest
        }
    }

    // Swap two nodes. Assume i & j are valid, O(1)
    private fun swap(i: Int, j: Int) {
        heap[i] = heap[j].also { heap[j] = heap[i] }
    }

    // Removes a particular element in the heap, O(n)
    fun remove(elem: WeightedEdge) = heap.remove(elem)

    // Remove a node at a paticular index, O(log(n))
    private fun removeAt(i: Int): WeightedEdge? {
        if (isEmpty()) return null

        val indexOfLastElem = size() - 1
        val removedData = heap[i]
        swap(i, indexOfLastElem)

        // Obliterate the value
        heap.removeAt(indexOfLastElem)

        // Check if the last element was removed
        if (i == indexOfLastElem) return removedData
        val elem = heap[i]

        // Try sinking element
        sink(i)

        // If sinking did not work try swimming
        if (elem == heap[i]) swim(i)
        return removedData
    }

    // Recursively check if this heap is a min heap
    private fun isMinHeap(k: Int): Boolean {
        // if we are outside the bounds of the heap return true
        val heapSize = size()
        if (k >= heapSize) return true

        val left = 2 * k + 1
        val right = 2 * k + 2

        // Make sure that the current node k is less than both of its children if they exist
        if (left < heapSize && !less(k, left)) return false
        if (right < heapSize && !less(k, right)) return false

        // Recurse on both children to make sure they're also valid
        return isMinHeap(left) && isMinHeap(right)
    }

    fun checkValid() {
        println(if (isMinHeap(0)) "Is MinHeap" else "Something goes wrong!!!")
    }

    override fun toString() = heap.toString()
}

data class Edge(val des: Int, val weight: Long)

data class WeightedEdge(val weight: Long, val leaf: Int) : Comparable<WeightedEdge> {
    private val diff: Long
        get() = (weight - weight / 2) * leaf

    override fun compareTo(other: WeightedEdge): Int {
        return if (this.diff < other.diff) 1
        else -1
    }
}

fun main() {
    val r = System.`in`.bufferedReader()
    val sb = StringBuilder()
    repeat(r.readInt()) {
        val n = r.readInt()
        val maxWeight = r.readInt()
        val adj = List(n) { mutableListOf<Edge>() }
        repeat(n - 1) {
            val u = r.readInt() - 1
            val v = r.readInt() - 1
            val w = r.readInt().toLong()
            adj[u] += Edge(v, w)
            adj[v] += Edge(u, w)
        }

        val parent = IntArray(n) { -1 }
        val childLeaf = IntArray(n) { 0 }
        val visited = BooleanArray(n) { false }

        fun dfs(i: Int): Int {
            visited[i] = true
            var leaf = 0
            adj[i].forEach {
                val (des, _) = it
                if (!visited[des]) {
                    leaf += dfs(des)
                    parent[des] = i
                }
            }
            if (i != 0 && adj[i].size == 1) leaf++
            childLeaf[i] = leaf
            return leaf
        }
        dfs(0)

        val we = arrayListOf<WeightedEdge>()
        val queue = LinkedList<Int>()
        queue.add(0)
        var totalWeight = 0L
        while (queue.isNotEmpty()) {
            val u = queue.removeFirst()
            adj[u].forEach {
                val (des, weight) = it
                if (parent[des] == u) {
                    we.add(WeightedEdge(weight, childLeaf[des]))
                    queue.add(des)
                    totalWeight += weight * childLeaf[des]
                }
            }
        }

        val heap = BinaryHeap<WeightedEdge>(we)
        var move = 0
        while (totalWeight > maxWeight) {
            val (wei, multi) = heap.poll()!!
            heap.add(WeightedEdge(wei / 2, multi))
            totalWeight -= (wei - wei / 2) * multi
            move++
        }
        sb.appendln(move)
    }
    print(sb)
}

// Hakiobo's code for faster reading input
private const val SPACE_INT = ' '.toInt()
private const val ZERO_INT = '0'.toInt()
private const val NL_INT = '\n'.toInt()

private fun BufferedReader.readInt(): Int {
    var ret = read()
    while (ret <= SPACE_INT) {
        ret = read()
    }
    val neg = ret == '-'.toInt()
    if (neg) {
        ret = read()
    }
    ret -= ZERO_INT
    var read = read()
    while (read >= ZERO_INT) {
        ret *= 10
        ret += read - ZERO_INT
        read = read()
    }

    while (read <= SPACE_INT && read != -1 && read != NL_INT) {
        mark(1)
        read = read()
    }
    if (read > SPACE_INT) {
        reset()
    }
    return if (neg) -ret else ret
}


import java.io.BufferedReader
import java.util.*

data class Edge(val d: Int, var w: Long, var l: Int = 0) : Comparable<Edge> {
    override fun compareTo(other: Edge): Int =
        if (w * l - other.w * other.l < 0) 1 else -1
}

fun main() {
    val r = System.`in`.bufferedReader()
    val sb = StringBuilder()
    repeat(r.readInt()) {
        val n = r.readInt()
        val s = r.readInt().toLong()
        val adj = List(n) { mutableListOf<Edge>() }
        repeat(n - 1) {
            val u = r.readInt() - 1
            val v = r.readInt() - 1
            val w = r.readInt().toLong()
            adj[u] += Edge(v, w)
            adj[v] += Edge(u, w)
        }
        val pq = PriorityQueue<Edge>()
        val visited = BooleanArray(n) { false }
        val leafs = IntArray(n) { 0 }

        // return number of leaf
        fun dfs(v: Int): Int {
            visited[v] = true
            var leaf = 0
            adj[v].forEach {
                if (!visited[it.d]) {
                    pq.add(it)
                    leaf += dfs(it.d)
                }
            }
            if (adj[v].size == 1 && v != 0) leaf++
            leafs[v] = leaf
            return leaf
        }
        dfs(0)
        pq.forEach { it.l = leafs[it.d] }
        //println(pq)

        var cursum = pq.fold(0L) { sum, edge -> sum + edge.w * edge.l }
        //println("cs: $cursum")
        var step = 0
        while (cursum > s) {
            //println("cs: $cursum, s: $s")
            val e = pq.poll()!!
            step++
            val weight = e.w
            pq.add(Edge(e.d, e.w / 2, e.l))
            //println(e.leave * (weight - e.wei))
            cursum -= e.l * (weight - e.w / 2)
            //println("after - : cs = $cursum")
        }
        sb.appendln(step)
    }
    print(sb)
}

// Hakiobo's code for faster reading input
private const val SPACE_INT = ' '.toInt()
private const val ZERO_INT = '0'.toInt()
private const val NL_INT = '\n'.toInt()

private fun BufferedReader.readInt(): Int {
    var ret = read()
    while (ret <= SPACE_INT) {
        ret = read()
    }
    val neg = ret == '-'.toInt()
    if (neg) {
        ret = read()
    }
    ret -= ZERO_INT
    var read = read()
    while (read >= ZERO_INT) {
        ret *= 10
        ret += read - ZERO_INT
        read = read()
    }

    while (read <= SPACE_INT && read != -1 && read != NL_INT) {
        mark(1)
        read = read()
    }
    if (read > SPACE_INT) {
        reset()
    }
    return if (neg) -ret else ret
}


import java.io.BufferedReader

data class Edge(val v: Int, var w: Long)

fun main() {
    val r = System.`in`.bufferedReader()
    val sb = StringBuilder()
    repeat(r.readInt()) {
        //println("Test case: $it")
        val n = r.readInt()
        val s = r.readInt().toLong()
        val adj = List(n) { mutableListOf<Edge>() }
        repeat(n - 1) {
            val u = r.readInt() - 1
            val v = r.readInt() - 1
            val w = r.readInt().toLong()
            adj[u] += Edge(v, w)
            adj[v] += Edge(u, w)
        }
        val visited = BooleanArray(n) { false }

        // Need leaf number, it is important. Another one is weight, other thing does not matter
        val leaves = arrayOfNulls<Int>(n)
        val edges = mutableListOf<Edge>()

        //var i = 0
        // return leaf
        fun dfs(v: Int): Int {
            visited[v] = true
            var leaf = 0
            // back edge, this is leaf
            if (adj[v].size == 1 && visited[adj[v][0].v]) {
                leaf++
            } else {
                // Count up child vertices's leaf
                for (e in adj[v]) {
                    if (!visited[e.v]) {
                        edges += e
                        leaf += dfs(e.v)
                    }
                }
            }
            leaves[v] = leaf
            return leaf
        }
        dfs(0)

        val a = mutableListOf<Long>()
        var cursum: Long = edges.fold(0L) { acc, (v, w) -> acc + w * leaves[v]!! }

        for ((v, weight) in edges) {
            val l = leaves[v]!!
            var w = weight
            var diff = (w + 1 shr 1) * l
            while (diff > 0) {
                a.add(diff)
                w /= 2
                diff = (w + 1 shr 1) * l
            }
        }
        a.sortDescending()
        //println("can remove: $a")
        var b = 0
        while (cursum > s) {
            cursum -= a[b++]
        }
        sb.appendln(b)
    }
    print(sb)
}

// Hakiobo's code for faster reading input
private const val SPACE_INT = ' '.toInt()
private const val ZERO_INT = '0'.toInt()
private const val NL_INT = '\n'.toInt()

private fun BufferedReader.readInt(): Int {
    var ret = read()
    while (ret <= SPACE_INT) {
        ret = read()
    }
    val neg = ret == '-'.toInt()
    if (neg) {
        ret = read()
    }
    ret -= ZERO_INT
    var read = read()
    while (read >= ZERO_INT) {
        ret *= 10
        ret += read - ZERO_INT
        read = read()
    }

    while (read <= SPACE_INT && read != -1 && read != NL_INT) {
        mark(1)
        read = read()
    }
    if (read > SPACE_INT) {
        reset()
    }
    return if (neg) -ret else ret
}


import java.io.BufferedReader

data class Edge(val v: Int, var w: Long)

fun main() {
    val r = System.`in`.bufferedReader()
    val sb = StringBuilder()
    repeat(r.readInt()) {
        //println("Test case: $it")
        val n = r.readInt()
        val s = r.readInt().toLong()
        val adj = List(n) { mutableListOf<Edge>() }
        repeat(n - 1) {
            val u = r.readInt() - 1
            val v = r.readInt() - 1
            val w = r.readInt().toLong()
            adj[u] += Edge(v, w)
            adj[v] += Edge(u, w)
        }

        val visited = BooleanArray(n) { false }
        val leaves = IntArray(n) { 0 }
        val weight = LongArray(n) { 0L }

        fun dfs(v: Int): Int {
            visited[v] = true
            var childLeaf = 0
            for (e in adj[v]) {
                if (!visited[e.v]) {
                    weight[e.v] = e.w
                    childLeaf += dfs(e.v)
                }
            }
            val leaf = maxOf(1, childLeaf)
            leaves[v] = leaf
            return leaf
        }
        dfs(0)

        val a = mutableListOf<Long>()
        var cursum = 0L

        for (i in 0..n - 1) {
            var w = weight[i]
            val l = leaves[i]
            cursum += w * l
            while (w != 0L) {
                a.add((w + 1 shr 1) * l)
                w /= 2
            }
        }
        a.sortDescending()

        var b = 0
        while (cursum > s) {
            cursum -= a[b]
            b++
        }
        sb.appendln(b)
    }
    print(sb)
}

// Hakiobo's code for faster reading input
private const val SPACE_INT = ' '.toInt()
private const val ZERO_INT = '0'.toInt()
private const val NL_INT = '\n'.toInt()

private fun BufferedReader.readInt(): Int {
    var ret = read()
    while (ret <= SPACE_INT) {
        ret = read()
    }
    val neg = ret == '-'.toInt()
    if (neg) {
        ret = read()
    }
    ret -= ZERO_INT
    var read = read()
    while (read >= ZERO_INT) {
        ret *= 10
        ret += read - ZERO_INT
        read = read()
    }

    while (read <= SPACE_INT && read != -1 && read != NL_INT) {
        mark(1)
        read = read()
    }
    if (read > SPACE_INT) {
        reset()
    }
    return if (neg) -ret else ret
}