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. Also, each edge has its own cost.

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

Each edge i has an associated cost c_i which is either 1 or 2 coins. Each move with edge i costs c_i coins.

Your task is to find the minimum total cost 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 four integers v_i, u_i, w_i and c_i (1 ≤ v_i, u_i ≤ n; 1 ≤ w_i ≤ 10^6; 1 ≤ c_i ≤ 2), where v_i and u_i are vertices the edge i connects, w_i is the weight of this edge and c_i is the cost 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 total cost required to make the sum of weights paths from the root to each leaf at most S.

Example

Input


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


Output


0
0
11
6
// package com.company;
import java.util.*;
import java.lang.*;
import java.io.*;
//****Use Integer Wrapper Class for Arrays.sort()****
public class EN5 {
    static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
    public static void main(String[] Args)throws Exception{
        FastReader scan=new FastReader(System.in);
        int t=1;
        t=scan.nextInt();
        while(t-->0){
            int n=scan.nextInt();
            long s=scan.nextLong();
            Node[] gr=new Node[n+1];
            for(int i=1;i<=n;i++){
                gr[i]=new Node();
            }
            for(int i=1;i<n;i++){
                int u=scan.nextInt();
                int v=scan.nextInt();
                long w=scan.nextInt();
                int c=scan.nextInt();
                gr[u].ns.put(v,w);
                gr[v].ns.put(u,w);
                gr[u].mode.put(v,c);
                gr[v].mode.put(u,c);
            }
            Set<Integer> vis=new HashSet<>();
            PriorityQueue<pair> pq1=new PriorityQueue<>(n,new mc());
            PriorityQueue<pair> pq2=new PriorityQueue<>(n,new mc());
            vis.add(1);
            long[] to=new long[1];
            dfs(1,gr,vis,pq1,pq2,to);
            long sum=to[0];
            int ans=Integer.MAX_VALUE;
            ArrayList<Long> reds1=new ArrayList<>();
            ArrayList<Long> reds2=new ArrayList<>();
            long sum1=sum;
            while(sum1>s&&!pq1.isEmpty()){
                pair cur=pq1.remove();
                long red=cur.red;
                if(red==0){
                    break;
                }
                reds1.add(red);
                sum1-=red;
                long c=cur.ls;
                long w=cur.val;
                if(w!=0) {
                    pq1.add(new pair(c * w - c * (w / 2), c, w / 2));
                }
            }
            long sum2=sum;
            while(sum2>s&&!pq2.isEmpty()){
                pair cur=pq2.remove();
                long red=cur.red;
                if(red==0){
                    break;
                }
                reds2.add(red);
                sum2-=red;
                long c=cur.ls;
                long w=cur.val;
                if(w!=0) {
                    pq2.add(new pair(c * w - c * (w / 2), c, w / 2));
                }
            }
            for(int i=1;i<reds1.size();i++){
                reds1.set(i,reds1.get(i-1)+reds1.get(i));
            }for(int i=1;i<reds2.size();i++){
                reds2.set(i,reds2.get(i-1)+reds2.get(i));
            }
            for(int i=0;i<=reds1.size();i++){
                long ss=sum;
                long fi=0;
                if(i!=0){
                    fi=reds1.get(i-1);
                }
                ss-=fi;
                int temp=i;
                boolean pos=true;
                if(ss>s){
                    pos=false;
                    int l=0;
                    int r=reds2.size()-1;
                    int in=-1;
                    while(l<=r){
                        int mid=(l+r)/2;
                        if(ss-reds2.get(mid)<=s){
                            in=mid;
                            r=mid-1;
                        }else{
                            l=mid+1;
                        }
                    }
                    if(in!=-1){
                        pos=true;
                        temp+=2*(in+1);
                    }
                }
                if(pos) {
                    ans = Math.min(temp, ans);
                }
            }
            out.println(ans);
        }
        out.flush();
        out.close();
    }
    static class mc implements Comparator<pair>{
        @Override
        public int compare(pair o1, pair o2) {
            if(o1.red>o2.red){
                return -1;
            }
            return 1;
        }
    }
    static class pair{
        long ls;
        long val;
        long red;
        pair(long red,long ls,long val){
            this.red=red;
            this.ls=ls;
            this.val=val;
        }
    }
    static int dfs(int u,Node[] gr,Set<Integer> vis,PriorityQueue<pair> pq1,PriorityQueue<pair> pq2,long[] to){
        long intr=0;
        int c=0;
        for(Integer v:gr[u].ns.keySet()){
            if(!vis.contains(v)){
                vis.add(v);
                int ls=dfs(v,gr,vis,pq1,pq2,to);
                c+=ls;
                long cur=ls*gr[u].ns.get(v);
                intr+=cur;
                long nw=gr[u].ns.get(v)/2;
                int mode=gr[u].mode.get(v);
                if(mode==1){
                    pq1.add(new pair(cur-ls*nw,ls,nw));
                }else{
                    pq2.add(new pair(cur-ls*nw,ls,nw));
                }
            }
        }
        to[0]+=intr;
        if(c==0){
            return 1;
        }else {
            return c;
        }
    }
    static class Node{
        Map<Integer,Long> ns;
        Map<Integer,Integer> mode;
        Node(){
            ns=new HashMap<>();
            mode=new HashMap<>();
        }
    }
    static class FastReader {

        byte[] buf = new byte[2048];
        int index, total;
        InputStream in;

        FastReader(InputStream is) {
            in = is;
        }

        int scan() throws IOException {
            if (index >= total) {
                index = 0;
                total = in.read(buf);
                if (total <= 0) {
                    return -1;
                }
            }
            return buf[index++];
        }

        String next() throws IOException {
            int c;
            for (c = scan(); c <= 32; c = scan()) ;
            StringBuilder sb = new StringBuilder();
            for (; c > 32; c = scan()) {
                sb.append((char) c);
            }
            return sb.toString();
        }

        int nextInt() throws IOException {
            int c, val = 0;
            for (c = scan(); c <= 32; c = scan()) ;
            boolean neg = c == '-';
            if (c == '-' || c == '+') {
                c = scan();
            }
            for (; c >= '0' && c <= '9'; c = scan()) {
                val = (val << 3) + (val << 1) + (c & 15);
            }
            return neg ? -val : val;
        }
        long nextLong() throws IOException {
            int c;
            long val = 0;
            for (c = scan(); c <= 32; c = scan()) ;
            boolean neg = c == '-';
            if (c == '-' || c == '+') {
                c = scan();
            }
            for (; c >= '0' && c <= '9'; c = scan()) {
                val = (val << 3) + (val << 1) + (c & 15);
            }
            return neg ? -val : val;
        }
    }
}
