Problem: Easy and hard versions are actually different problems, so we advise you to read both statements carefully.

You are given a weighted rooted tree, vertex 1 is the root of this tree.

A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last different from v vertex on the path from the root to the vertex v. Children of vertex v are all vertices for which v is the parent. A vertex is a leaf if it has no children. The weighted tree is such a tree that each edge of this tree has some weight.

The weight of the path is the sum of edges weights on this path. The weight of the path from the vertex to itself is 0.

You can make a sequence of zero or more moves. On each move, you select an edge and divide its weight by 2 rounding down. More formally, during one move, you choose some edge i and divide its weight by 2 rounding down (w_i := \left⌊(w_i)/(2)\right⌋).

Your task is to find the minimum number of moves required to make the sum of weights of paths from the root to each leaf at most S. In other words, if w(i, j) is the weight of the path from the vertex i to the vertex j, then you have to make ∑_{v ∈ leaves} w(root, v) ≤ S, where leaves is the list of all leaves.

You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow.

The first line of the test case contains two integers n and S (2 ≤ n ≤ 10^5; 1 ≤ S ≤ 10^{16}) — the number of vertices in the tree and the maximum possible sum of weights you have to obtain. The next n-1 lines describe edges of the tree. The edge i is described as three integers v_i, u_i and w_i (1 ≤ v_i, u_i ≤ n; 1 ≤ w_i ≤ 10^6), where v_i and u_i are vertices the edge i connects and w_i is the weight of this edge.

It is guaranteed that the sum of n does not exceed 10^5 (∑ n ≤ 10^5).

Output

For each test case, print the answer: the minimum number of moves required to make the sum of weights of paths from the root to each leaf at most S.

Example

Input


3
3 20
2 1 8
3 1 7
5 50
1 3 100
1 5 10
2 3 123
5 4 55
2 100
1 2 409


Output


0
8
3

Solution (in JAVA):

Solution:
import java.io.*;
import java.util.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
static class edd implements Comparable<edd>{
	int w;
	int cnt;
	int cost;
	public edd(int w, int cnt, int cost) {
		this.w = w;
		this.cnt = cnt;
		this.cost = cost;
	}
	long oneadd() {
		return (1L * (w - w/2) * cnt); // sort on the basis of benefits they provide
	}
	public int compareTo(edd e) {
		return -Long.compare(oneadd(), e.oneadd());
	}
}
static class Pair<E, V, K> implements Comparable<Pair<E, V, K>>{


       E a;
       V b;
       K c;
//       int ind;
//       public Pair(int x, long y) {a = x;b=y;}
       public Pair(E x, V y, K z) {a = x;b=y; c = z;}
//       public Pair(int x,int y, int z){a=x;b=y;ind = z;}
       public int compareTo(Pair<E, V, K> p){
           return Integer.compare((Integer)a, (Integer)p.a);
       }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + (Integer)a;
            result = prime * result + (Integer)b;
           
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            Pair<E, V, K> cur = (Pair<E, V, K>)obj;
            if((Integer)a == (Integer)cur.a && (Integer)b == (Integer)cur.b)return true;
            if((Integer)b == (Integer)cur.a && (Integer)a == (Integer)cur.b)return true;
            return false;
        }
 } 
public static void main(String[] args) throws Exception {
   new Thread(null, null, "Anshum Gupta", 99999999) {
        public void run() {
            try {
                solve();
            } catch(Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }.start();
}
public static long gcd(long a,long b)
{
  if(a<b)
    return gcd(b,a);
  if(b==0)
    return a;
  return gcd(b,a%b);
  
}
static long lcm(int a,int b) {
  return  a*b / gcd(a,b);
}
static int[] z(char[]arr) {
    int l = 0, r = 0;
    int[]z = new int[n];
    for(int i=1; i<n; i++) {
        if(i > r) {
            l = r = i;
            while(r < n && arr[r-l] == arr[r])r++;
            z[i] = r-- - l;
        }else {
            int k = i - l;
            if(z[k] < r - i + l)z[i] = z[k];
            else {
                l = i;
                while(r < n && arr[r-l] == arr[r])r++;
                z[i] = r-- - l;
            }
        }
    }
    return z;
}   
static long pow(long x,long y){
    if(y == 0)return 1;
    if(y==1)return x;
    long a = pow(x,y/2);
    a = (a*a)%mod;
    if(y%2==0){
        return a;
    }
    return (a*x)%mod;
}
static long my_inv(long a) {
    return pow(a,mod-2);
}
static long bin(int a,int b) {
    if(a < b || a<0 || b<0)return 0;
    return ((fact[a]*inv_fact[a-b])%mod * inv_fact[b])%mod;
}
static void make_facts() {
  fact=new long[mxN];
  inv_fact = new long[mxN];
  fact[0]=inv_fact[0]=1L;
  for(int i=1;i<mxN;i++) {
       fact[i] = (i*fact[i-1])%mod;
       inv_fact[i] = my_inv(fact[i]);
  }
}
static void lazy(int st, int e, int tn) {
    if(lazy[tn] != INF) {
        tree[tn] = Math.min(tree[tn], lazy[tn]);
        if(st != e) {
            lazy[2*tn] = Math.min(lazy[2*tn], lazy[tn]);
            lazy[2*tn+1] = Math.min(lazy[2*tn+1], lazy[tn]);
        }
        lazy[tn] = INF;
    }
}
static void update(int ind, int val, int st, int e, int tn) {
    if(st == e) {
        tree[tn] = val;
        return;
    }
    int mid = (st + e) >> 1;    
      if(ind <= mid)
        update(ind, val, st, mid, 2*tn);
      else
        update(ind, val, mid+1, e, 2*tn+1);
    tree[tn] = Math.min(tree[2*tn], tree[2*tn+1]);
}
static int query(int l, int r, int st, int e, int tn) {
    if(st > r || e < l)return INF;
    
    if(st >= l && e <= r) {
        return tree[tn];
    }

    int mid = (st + e) >> 1;
    int x = query(l, r, st, mid, 2 * tn);
    int y = query(l, r, mid+1, e, 2 * tn + 1);
    return Math.min(x, y);
}
static final long mxx = (long)(1e18+5);
static final int mxN = (int)(1e5);
static final int mxV = (int)(1e5+5), log = 18;
static long mod = (long)(1e9+7); //998244353;//
static int[]tree, lazy, dep;
static long[]fact, inv_fact, a;
static final int INF = (int)1e9+5;
static boolean[]vis;
static ArrayList<ArrayList<Pair<Integer, Integer, Integer>>> adj;
static int n, m, k, x, y, z, q;
static char[]arr;
static int[]lc;
static long sum, cur;
static PriorityQueue<edd> p1, p2;
static long lastOne;
public static PrintWriter out;
public static MyScanner s;
static void dfs(int u, int par) {
	if(adj.get(u).size() == 1 && u != 1) {
		lc[u] = 1;
		return;
	}
	for(Pair<Integer, Integer, Integer> x : adj.get(u)) {
		if(x.a != par) {
			dfs(x.a, u);
			lc[u] += lc[x.a];
			if(lc[x.a] != 0){
				if(x.c == 1)
				p1.add(new edd(x.b , lc[x.a], x.c));
				else p2.add(new edd(x.b, lc[x.a], x.c));
				cur += 1L * x.b * lc[x.a];
			}
		}
	}
}
static void takeOne(PriorityQueue<edd> p) {
	edd x = p.poll();
	x.w /= 2;
	p.add(x);
}
static void takeTwo(PriorityQueue<edd> p) {
	takeOne(p);
	lastOne = benefitOfOne(p);
	takeOne(p);
}
static long benefitOfOne(PriorityQueue<edd> p) {
	if(p.isEmpty()) {
		return 0;
	}
	return p.peek().oneadd();
}
static long benefitOfTwo(PriorityQueue<edd> p) {
	if(p.isEmpty())return 0L;
	edd x = p.poll();
	int orig = x.w;
	long ans = x.oneadd();
	x.w /= 2;
	long ans2 = Math.max(x.oneadd(), benefitOfOne(p));
	x.w = orig;
	p.add(x);
	return ans + ans2;
}
	public static void solve() throws Exception {
	   // solve the problem here
			s = new MyScanner();
	   		out = new PrintWriter(new BufferedOutputStream(System.out), true);
	        int tc = s.nextInt();
	        while(tc-->0){
	        	n = s.nextInt();
	        	sum = s.nextLong();
	        	adj = new ArrayList<ArrayList<Pair<Integer,Integer,Integer>>>();
	        	for(int i=0; i<=n; i++)adj.add(new ArrayList<Solution.Pair<Integer,Integer,Integer>>());
	        	for(int i=0; i<n-1; i++) {
	        		int u = s.nextInt();
	        		int v = s.nextInt();
	        		int w = s.nextInt();
	        		int c = s.nextInt();
	        		adj.get(u).add(new Pair<Integer, Integer, Integer>(v, w, c));
	        		adj.get(v).add(new Pair<Integer, Integer, Integer>(u, w, c));
	        	}
	        	p1 = new PriorityQueue<edd>();
	        	p2 = new PriorityQueue<edd>();
	        	cur = 0;
	        	lc = new int[n+1];
	        	dfs(1, 0);
	        	int cnt = 0;
	        	lastOne = mxx;
	        	while(cur > sum) {
	        		if(benefitOfOne(p1) >= cur - sum) {
	        			cnt++;
	        			break;
	        		}
	        		if(benefitOfTwo(p1) >= benefitOfOne(p2)) {
	        			cur -= benefitOfTwo(p1);
	        			takeTwo(p1);
	        		}else {
	        			cur -= benefitOfOne(p2);
	        			takeOne(p2);
	        		}
	        		cnt += 2;
	        	}
	        	if(cur + lastOne <= sum) {
	        		cnt--;
	        	}
	        	out.println(cnt);
	        }   
	           
	        out.flush();
	        out.close();
	}
	
static class MyScanner {

    BufferedReader br;
    StringTokenizer st;

    public MyScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() { return Integer.parseInt(next()); }
    long nextLong() { return Long.parseLong(next()); }
    double nextDouble() { return Double.parseDouble(next()); }
    int[] nextIntArray(int n){
    	int[]a = new int[n];
    	for(int i=0; i<n; i++) {
    		a[i] = this.nextInt();
    	}
    	return a;
    }
    long[] nextLongArray(int n) {
    	long[]a = new long[n];
    	for(int i=0; i<n; i++) {
    		a[i] = this.nextLong();
    	}
    	return a;
    }
    String nextLine(){
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}

 
}