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.*;
 
public class A {
 
	static ArrayList<pair> adj[];
	static pair[] num;
	static boolean vis[];
	static long s;
 
	static pair dfs(int u, long w, int c) {
		vis[u] = true;
		int sum = 0;
		for (pair v : adj[u]) {
			if (!vis[v.x]) {
				sum += dfs(v.x, v.y, v.c).x;
			}
		}
		if (adj[u].size() == 1) {
			return num[u] = new pair(1, w, c);
		}
		return num[u] = new pair(sum, w, c);
	}
 
	static ArrayList<Long> half(PriorityQueue<pair> pq) {
		ArrayList<Long> ans = new ArrayList<>();
		long sum = 0;
		while (true && !pq.isEmpty()) {
			pair p = pq.poll();
			if (p.y == 0)
				break;
			sum += p.y;
			num[p.x].y /= 2;
			long dif = num[p.x].x * num[p.x].y - num[p.x].x * (num[p.x].y / 2);
			pq.add(new pair(p.x, dif, 0));
			ans.add(sum);
		}
		return ans;
	}
 
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int t = sc.nextInt();
		while (t-- > 0) {
			int n = sc.nextInt();
			s = sc.nextLong();
			adj = new ArrayList[n];
			num = new pair[n];
			vis = new boolean[n];
			for (int i = 0; i < n; i++) {
				adj[i] = new ArrayList<>();
			}
			for (int i = 0; i < n - 1; i++) {
				int u = sc.nextInt() - 1;
				int v = sc.nextInt() - 1;
				int w = sc.nextInt();
				int c = sc.nextInt();
				adj[u].add(new pair(v, w, c));
				adj[v].add(new pair(u, w, c));
			}
			dfs(0, 0, 0);
			PriorityQueue<pair> pq1 = new PriorityQueue<>();
			PriorityQueue<pair> pq2 = new PriorityQueue<>();
			long sum = 0;
			for (int i = 1; i < n; i++) {
				if (num[i] != null) {
					long dif = num[i].x * num[i].y - num[i].x * (num[i].y / 2);
					sum +=num[i].x * num[i].y;
					if (num[i].c == 1)
						pq1.add(new pair(i, dif, 1));
					else
						pq2.add(new pair(i, dif, 2));
				}
			}
			ArrayList<Long> ans1 = half(pq1);
			ArrayList<Long> ans2 = half(pq2);
			int min = Integer.MAX_VALUE;
			for (int i = 0; i < ans1.size() + 1; i++) {
				int x = 0;
				long temp = sum;
				if (i < ans1.size()) {
					x = i + 1;
					temp = sum - ans1.get(i);
				}
				if (temp <= s) {
					min = Math.min(min, x);
				}
				int lo = 0;
				int hi = ans2.size() - 1;
				int ans = 0;
				while (lo <= hi) {
					int mid = (lo + hi) >> 1;
					if (temp-ans2.get(mid)<=s ) {
						ans = mid;
						hi = mid - 1;
					} else
						lo = mid + 1;
				}
				if (!ans2.isEmpty() && temp-ans2.get(ans)<=s) {
					min = Math.min(min, x + (ans+1) * 2);
				}
			}
			System.out.println(min);
 
		}
	}
 
	/////////////////////////////////////////////////////////////////////////////////////////////
 
	static class pair implements Comparable<pair> {
		int x;
		long y;
		int c;
 
		pair(int s, long d, int e) {
			x = s;
			y = d;
			c = e;
		}
 
		@Override
		public int compareTo(pair p) {
			return (y > p.y) ? -1 : ((y == p.y) ? 0 : 1);
		}
 
		@Override
		public String toString() {
			return x + " " + y;
 
		}
	}
 
	static long mod(long ans, int mod) {
		return (ans % mod + mod) % mod;
	}
 
	static long gcd(long a, long b) {
		if (a == 0)
			return b;
		return gcd(b % a, a);
	}
 
	static long lcm(long a, long b) {
		return (a * b) / gcd(a, b);
	}
 
	public static int log(int n, int base) {
		int ans = 0;
		while (n + 1 > base) {
			ans++;
			n /= base;
		}
		return ans;
	}
 
	static long pow(long b, long e) {
		long ans = 1;
		while (e > 0) {
			if ((e & 1) == 1)
				ans = ((ans * 1l * b));
			e >>= 1;
			{
 
			}
			b = ((b * 1l * b));
		}
		return ans;
	}
 
	static int powmod(int b, long e, int mod) {
		int ans = 1;
		b %= mod;
		while (e > 0) {
			if ((e & 1) == 1)
				ans = (int) ((ans * 1l * b) % mod);
			e >>= 1;
			b = (int) ((b * 1l * b) % mod);
		}
		return ans;
	}
 
	static int ceil(int a, int b) {
		int ans = a / b;
		return a % b == 0 ? ans : ans + 1;
	}
 
	static long ceil(long a, long b) {
		long ans = a / b;
		return a % b == 0 ? ans : ans + 1;
	}
 
	// Returns nCr % p
	static int nCrModp(int n, int r, int p) {
		if (r > n - r)
			r = n - r;
 
		// The array C is going to store last
		// row of pascal triangle at the end.
		// And last entry of last row is nCr
		int C[] = new int[r + 1];
 
		C[0] = 1; // Top row of Pascal Triangle
 
		// One by constructs remaining rows of Pascal
		// Triangle from top to bottom
		for (int i = 1; i <= n; i++) {
 
			// Fill entries of current row using previous
			// row values
			for (int j = Math.min(i, r); j > 0; j--)
 
				// nCj = (n-1)Cj + (n-1)C(j-1);
				C[j] = (C[j] + C[j - 1]) % p;
		}
		return C[r];
	}
 
	static class Scanner {
		StringTokenizer st;
		BufferedReader br;
 
		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}
 
		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}
 
		public int[] intArr(int n) throws IOException {
			int a[] = new int[n];
			for (int i = 0; i < a.length; i++) {
				a[i] = nextInt();
			}
			return a;
		}
 
		public long[] longArr(int n) throws IOException {
			long a[] = new long[n];
			for (int i = 0; i < a.length; i++) {
				a[i] = nextLong();
			}
			return a;
		}
 
		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
 
		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}
 
		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}
 
		public String nextLine() throws IOException {
			return br.readLine();
		}
 
		public double nextDouble() throws IOException {
			String x = next();
			StringBuilder sb = new StringBuilder("0");
			double res = 0, f = 1;
			boolean dec = false, neg = false;
			int start = 0;
			if (x.charAt(0) == '-') {
				neg = true;
				start++;
			}
			for (int i = start; i < x.length(); i++)
				if (x.charAt(i) == '.') {
					res = Long.parseLong(sb.toString());
					sb = new StringBuilder("0");
					dec = true;
				} else {
					sb.append(x.charAt(i));
					if (dec)
						f *= 10;
				}
			res += Long.parseLong(sb.toString()) / f;
			return res * (neg ? -1 : 1);
		}
 
		public boolean ready() throws IOException {
			return br.ready();
		}
 
	}
 
	public static void shuffle(int[] a) {
		int n = a.length;
		for (int i = 0; i < n; i++) {
			int r = i + (int) (Math.random() * (n - i));
			int tmp = a[i];
			a[i] = a[r];
			a[r] = tmp;
		}
	}
 
}