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.util.concurrent.TimeUnit;

public class e1399 implements Runnable{
	
    public static void main(String[] args) {
    	try{
            new Thread(null, new e1399(), "process", 1<<26).start();
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
	public void run() {
		FastReader scan = new FastReader();
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		//PrintWriter out = new PrintWriter("file.out");
		Task solver = new Task();
		int t = scan.nextInt();
		//int t = 1;
		for(int i = 1; i <= t; i++) solver.solve(i, scan, out);
		out.close();
	}

	static class Task {
		static final int oo = Integer.MAX_VALUE;
		static final long OO = Long.MAX_VALUE;

		static List<List<tup>> adj = new ArrayList<>();
		
		public void solve(int testNumber, FastReader sc, PrintWriter out) {
			// Easy version
			
			/*
			int N = sc.nextInt();
			long S = sc.nextLong();
			
			adj.clear();
			for(int i = 0; i <= N; i++) {
				adj.add(new ArrayList<>());
			}
			
			List<tup> edges = new ArrayList<>();
			for(int i = 0; i < N-1; i++) {
				int u = sc.nextInt();
				int v = sc.nextInt();
				int w = sc.nextInt();
				adj.get(u).add(new tup(v, w));
				adj.get(v).add(new tup(u, w));
				edges.add(new tup(u, v, w));
			}
//			dbg(adj);
			
			long[] size = new long[N+1];
			int[] depth = new int[N+1];
			dfs(1, 0, size, depth, 0);
//			dbg(size);
			
			PriorityQueue<tup> values = new PriorityQueue<>();
			long sum = 0;
			for(tup each: edges) {
				if(depth[each.b] > depth[each.a]) {
					int swap = each.b;
					each.b = each.a;
					each.a = swap;
				}
				values.add(new tup(each.a, each.b, each.c/2, (each.c+1)/2 * size[each.a]));
				sum += each.c * size[each.a];
			}
//			dbg(sum);
//			dbg(values);
			
			int count = 0;
			while(sum > S) {
				//dbg(sum);
				tup each = values.poll();
				sum -= each.d;
				values.add(new tup(each.a, each.b, each.c/2, (each.c+1)/2 * size[each.a]));
				count++;
			}
			
			out.println(count);
			*/
			
			// Hard Version
			int N = sc.nextInt();
			long S = sc.nextLong();
			
			adj.clear();
			for(int i = 0; i <= N; i++) {
				adj.add(new ArrayList<>());
			}
			
			List<tup> edges = new ArrayList<>();
			for(int i = 0; i < N-1; i++) {
				int u = sc.nextInt();
				int v = sc.nextInt();
				int w = sc.nextInt();
				int c = sc.nextInt();
				adj.get(u).add(new tup(v, w));
				adj.get(v).add(new tup(u, w));
				edges.add(new tup(u, v, w, c));
			}
//			dbg(adj);
			
			long[] size = new long[N+1];
			int[] depth = new int[N+1];
			dfs(1, 0, size, depth, 0);
			//dbg(size);
			
			PriorityQueue<tup> cost1 = new PriorityQueue<>();
			PriorityQueue<tup> cost2 = new PriorityQueue<>();
			long sum = 0;
			for(tup each: edges) {
				if(depth[each.b] > depth[each.a]) {
					int swap = each.b;
					each.b = each.a;
					each.a = swap;
				}
				if(each.d == 1) {
					cost1.add(new tup(each.a, each.b, each.c/2, (each.c+1)/2 * size[each.a]));
				} else {
					cost2.add(new tup(each.a, each.b, each.c/2, (each.c+1)/2 * size[each.a]));
				}
				sum += each.c * size[each.a];
			}
//			dbg(sum);
//			dbg(cost1);
//			dbg(cost2);
			
			int count = 0;
			Stack<tup> ones = new Stack<>();
			while(sum > S) {
				if(cost1.size() == 0) {
					tup each = cost2.poll();
					sum -= each.d;
					count+=2;
					cost2.add(new tup(each.a, each.b, each.c/2, (each.c+1)/2 * size[each.a]));
					continue;
				}
				if(cost2.size() == 0) {
					tup each = cost1.poll();
					sum -= each.d;
					count++;
					cost1.add(new tup(each.a, each.b, each.c/2, (each.c+1)/2 * size[each.a]));
					ones.add(each);
					continue;
				}
				//dbg(sum);
				if(sum - cost1.peek().d <= S) {
					count++;
					sum -= cost1.peek().d;
					ones.add(cost1.poll());
					break;
				}
				if(sum - cost2.peek().d <= S) {
					count+=2;
					sum -= cost2.peek().d;
					//dbg(cost2.peek());
					break;
				}
				//dbg(sum);
				if(cost1.peek().d * 2 > cost2.peek().d) {
					tup each = cost1.poll();
					ones.add(each);
					//dbg(each);
					sum -= each.d;
					count++;
					cost1.add(new tup(each.a, each.b, each.c/2, (each.c+1)/2 * size[each.a]));
				} else {
					tup each = cost2.poll();
					//dbg(each);
					sum -= each.d;
					count+=2;
					cost2.add(new tup(each.a, each.b, each.c/2, (each.c+1)/2 * size[each.a]));
				}
			}
//			if(testNumber == 1166 && count == 37) {
//				dbg(S);
//				dbg(edges);
//			}
			
//			dbg(ones);
//			dbg(sum);
			while(ones.size() > 0 && sum + ones.peek().d <= S) {
				count--;
				sum += ones.peek().d;
				ones.pop();
			}
			
			out.println(count);
		}
		
		public long dfs(int node, int parent, long[] sizes, int[] depth, int dep) {
			long size = 0;
			depth[node] = dep;
			for(tup each: adj.get(node)) {
				if(each.a != parent) {
					size += dfs(each.a, node, sizes, depth, dep + 1);
				}
			}
			if(adj.get(node).size() <= 1) {
				size++;
			}
			sizes[node] = size;
			return size;
		}
	}
	
	static long modDivide(long a, long b, long MOD) {
		a %= MOD;
		return (binpow(b, MOD-2, MOD) * a) % MOD;
	}
	static long binpow(long a, long b, long m) {
		a %= m;
		long res = 1;
		while (b > 0) {
			if ((b & 1) == 1)
				res = res * a % m;
			a = a * a % m;
			b >>= 1;
		}
		return res;
	}
	static int[] reverse(int a[]) 
    { 
        int[] b = new int[a.length]; 
        for (int i = 0, j = a.length; i < a.length; i++, j--) { 
            b[j - 1] = a[i]; 
        } 
        
        return b;
    }
	static long[] reverse(long a[]) 
    { 
        long[] b = new long[a.length]; 
        for (int i = 0, j = a.length; i < a.length; i++, j--) { 
            b[j - 1] = a[i]; 
        } 
        
        return b;
    }
	static void sort(int[] x){
		shuffle(x);
		Arrays.sort(x);
	}
	static void sort(long[] x){
		shuffle(x);
		Arrays.sort(x);
	}
	static class tup implements Comparable<tup>, Comparator<tup>{
		int a, b, c; long d;
		tup(int a,int b, int c, long d){
			this.a=a;
			this.b=b;
			this.c=c;
			this.d=d;
		}
		tup(int a,int b, int c){
			this.a=a;
			this.b=b;
			this.c=c;
		}
		tup(int a,int b){
			this.a=a;
			this.b=b;
		}
		public tup() {
		}
		@Override
		public int compareTo(tup o){
			return -Long.compare(d,o.d);
		}
		@Override
		public int compare(tup o1, tup o2) {
			return -Long.compare(o1.d, o2.d);
		}
		
		@Override
	    public int hashCode() {
			return Objects.hash(a, b);
	    }
 
	    @Override
	    public boolean equals(Object obj) {
	    	if (this == obj)
                return true;
	    	if (obj == null)
                return false;
	    	if (getClass() != obj.getClass())
                return false;
	    	tup other = (tup) obj;
	    	return a==other.a && b==other.b;
	    }
	    
	    @Override
	    public String toString() {
	    	return a + " " + b + " " + c + " " + d;
	    }
	}
	
	static void shuffle(int[] a) {
		Random get = new Random();
		for (int i = 0; i < a.length; i++) {
			int r = get.nextInt(a.length);
			int temp = a[i];
			a[i] = a[r];
			a[r] = temp;
		}
	}

	static void shuffle(long[] a) {
		Random get = new Random();
		for (int i = 0; i < a.length; i++) {
			int r = get.nextInt(a.length);
			long temp = a[i];
			a[i] = a[r];
			a[r] = temp;
		}
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

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

		public FastReader(String s) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(new File(s)));
		}

		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());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
		
		int[] readArray(int size) {
			int[] a = new int[size];
			for(int i = 0; i < size; i++) {
				a[i] = nextInt();
			}
			return a;
		}
		
		long[] readLongArray(int size) {
			long[] a = new long[size];
			for(int i = 0; i < size; i++) {
				a[i] = nextLong();
			}
			return a;
		}
	}
	
	static void dbg(int[] arr) {
		System.out.println(Arrays.toString(arr));
	}
	static void dbg(long[] arr) {
		System.out.println(Arrays.toString(arr));
	}
	static void dbg(boolean[] arr) {
		System.out.println(Arrays.toString(arr));
	}

	static void dbg(Object... args) {
        for (Object arg : args)
            System.out.print(arg + " ");
        System.out.println();
    }
}