#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const int INFMEM = 63;
const int INF = 1061109567;
const long long LINF = 4557430888798830399LL;
const double DINF = numeric_limits<double>::infinity();
const long long MOD = 1000000007;
const int dx[8] = {0, 0, 1, -1, 1, -1, 1, -1};
const int dy[8] = {1, -1, 0, 0, 1, -1, -1, 1};
const char dch[4] = {'R', 'L', 'D', 'U'};
const double PI = 3.141592653589793;
inline void open(string a) {
  freopen((a + ".in").c_str(), "r", stdin);
  freopen((a + ".out").c_str(), "w", stdout);
}
inline void fasterios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long currentS;
long long edgeValue[100005];
long long leafonEdge[100005];
bool isExpensive[100005];
vector<pair<pair<long long, long long>, int>> edge[100005];
vector<long long> antri[3];
inline void process(long long leafCount, long long price, int coin) {
  currentS += price * leafCount;
  while (price) {
    antri[coin].push_back(leafCount * (price - (price / 2)));
    price /= 2;
  }
  return;
}
void dfs(long long pos, long long sum = 0, long long par = -1) {
  int travCount = 0;
  leafonEdge[pos] = 0;
  for (auto& nx : edge[pos]) {
    if (nx.first.first == par) continue;
    travCount++;
    dfs(nx.first.first, sum + nx.first.second, pos);
    leafonEdge[pos] += leafonEdge[nx.first.first];
    edgeValue[nx.first.first] = nx.first.second;
    process(leafonEdge[nx.first.first], nx.first.second, nx.second);
  }
  if (travCount == 0) leafonEdge[pos]++;
}
int main() {
  fasterios();
  int testCases;
  cin >> testCases;
  int tc = 0;
  while (++tc <= testCases) {
    long long n, s;
    cin >> n >> s;
    currentS = 0;
    for (int i = 1; i <= n; i++) edge[i].clear();
    for (int i = 0; i < (3); ++i) antri[i].clear();
    for (int i = 1; i < n; i++) {
      long long u, v, w, c;
      cin >> u >> v >> w >> c;
      edge[u].push_back({{v, w}, c});
      edge[v].push_back({{u, w}, c});
    }
    dfs(1);
    long long ptra = 0;
    long long ptrb = 0;
    sort(begin(antri[1]), end(antri[1]), greater<long long>());
    sort(begin(antri[2]), end(antri[2]), greater<long long>());
    long long curans = 0, ans = LINF;
    while (ptra < (int)(antri[2]).size()) {
      if (currentS <= s) break;
      currentS -= antri[2][ptra++];
      curans += 2;
    }
    if (currentS <= s) ans = min(ans, curans);
    while (ptrb < (int)(antri[1]).size()) {
      while (currentS > s) {
        if (ptrb >= (int)(antri[1]).size()) break;
        currentS -= antri[1][ptrb++];
        curans++;
      }
      if (currentS <= s) ans = min(ans, curans);
      if (ptra == 0) break;
      if (ptrb == (int)(antri[1]).size()) break;
      curans -= 2;
      currentS += antri[2][--ptra];
    }
    cout << ans << '\n';
  }
  return 0;
}
