#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<long long int, pair<long long int, long long int>>>> adj(
    100005);
long long int cnt[100005];
multiset<pair<long long int, pair<long long int, long long int>>> s1;
multiset<pair<long long int, pair<long long int, long long int>>> s2;
vector<long long int> v1;
vector<long long int> v2;
long long int Sum1;
long long int Sum2;
void dfs(long long int node, long long int par) {
  cnt[node] = 0;
  long long int temp = 0;
  for (auto it : adj[node]) {
    if (it.first == par) {
      continue;
    }
    temp++;
    dfs(it.first, node);
    if (it.second.second == 1) {
      s1.insert({(cnt[it.first] * it.second.first) -
                     (cnt[it.first] * (it.second.first / 2)),
                 {it.second.first, cnt[it.first]}});
      Sum1 += cnt[it.first] * it.second.first;
    }
    if (it.second.second == 2) {
      s2.insert({(cnt[it.first] * it.second.first) -
                     (cnt[it.first] * (it.second.first / 2)),
                 {it.second.first, cnt[it.first]}});
      Sum2 += cnt[it.first] * it.second.first;
    }
    cnt[node] += cnt[it.first];
  }
  if (temp == 0) {
    cnt[node] = 1;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, S;
    cin >> n >> S;
    for (long long int i = 1; i <= n; i++) {
      adj[i].clear();
    }
    s1.clear();
    s2.clear();
    v1.clear();
    v2.clear();
    Sum1 = 0;
    Sum2 = 0;
    for (long long int i = 1; i <= n - 1; i++) {
      long long int u, v, w, c;
      cin >> u >> v >> w >> c;
      adj[u].push_back({v, {w, c}});
      adj[v].push_back({u, {w, c}});
    }
    dfs(1, 0);
    v1.push_back(Sum1);
    while (Sum1 > 0) {
      auto it = *s1.rbegin();
      auto it1 = s1.find(it);
      s1.erase(it1);
      Sum1 -= it.first;
      v1.push_back(Sum1);
      it.second.first /= 2;
      it.first = it.second.second * 1ll *
                 ((long long int)it.second.first -
                  ((long long int)it.second.first / (long long int)2));
      if (it.first > 0) {
        s1.insert(it);
      }
    }
    v2.push_back(Sum2);
    while (Sum2 > 0) {
      auto it = *s2.rbegin();
      auto it1 = s2.find(it);
      s2.erase(it1);
      Sum2 -= it.first;
      v2.push_back(Sum2);
      it.second.first /= 2;
      it.first = it.second.second * 1ll *
                 ((long long int)it.second.first -
                  ((long long int)it.second.first / (long long int)2));
      if (it.first > 0) {
        s2.insert(it);
      }
    }
    long long int ans = INT_MAX;
    long long int indx1 = 0;
    long long int indx2 = v2.size() - 1;
    while (indx1 < v1.size() && indx2 >= 0) {
      long long int x = v1[indx1];
      while (indx2 >= 0 && x + v2[indx2] <= S) {
        indx2--;
      }
      if (x + v2[indx2 + 1] <= S) {
        ans = min(ans, indx1 + 2 * (indx2 + 1));
      }
      indx1++;
    }
    cout << ans << "\n";
  }
}
