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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
void display(vector<int> &a) {
  for (int z : a) cout << z << " ";
  cout << endl;
}
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
const int mod = (int)1e9 + 7;
const long long INF64 = 3e18;
void smxl(long long &a, long long b) {
  if (a < b) a = b;
}
void smnl(long long &a, long long b) {
  if (a > b) a = b;
}
void adsl(long long &a, long long b) {
  a += b;
  if (a >= mod) a -= mod;
}
void misl(long long &a, long long b) {
  a -= b;
  if (a >= mod) a -= mod;
  if (a < 0) a += mod;
}
void smx(long long &a, long long b) {
  if (a < b) a = b;
}
void smn(long long &a, long long b) {
  if (a > b) a = b;
}
void ads(long long &a, long long b) {
  a += b;
  if (a >= mod) a -= mod;
}
void mis(long long &a, long long b) {
  a -= b;
  if (a >= mod) a -= mod;
  if (a < 0) a += mod;
}
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
long long egcd(long long a, long long b, long long &x, long long &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long d = egcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return d;
}
long long mbinp(long long a, long long b) {
  a %= mod;
  if (b == 0) return 1;
  long long ans = mbinp(a, b / 2);
  long long tmp = (ans * ans) % mod;
  if (b % 2) return ((tmp * a) % mod);
  return ((tmp) % mod);
}
long long binp(long long a, long long b) {
  if (b == 0) return 1;
  long long ans = binp(a, b / 2);
  long long tmp = (ans * ans);
  if (b % 2) return ((tmp * a));
  return ((tmp));
}
long long C(int n, int m) {
  long long ret = 1;
  for (int i = 1; i <= m; i++) {
    ret *= (n - i + 1);
    ret /= i;
  }
  return ret;
}
long long overbinp(long long a, int b) {
  long long res = 1;
  while (b) {
    if (b & 1) {
      if (res < INF64 / a)
        res *= a;
      else
        return INF64;
    }
    if (b > 1) {
      if (a < INF64 / a)
        a *= a;
      else
        return INF64;
    }
    b >>= 1;
  }
  return res;
}
class DSU {
  vector<int> par;
  vector<int> siize;

 public:
  DSU(int n) {
    par.resize(n);
    siize.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      siize[i] = 1;
    }
  }

 public:
  int get(int x) { return (x == par[x] ? x : par[x] = get(par[x])); }

 public:
  void merge(int a, int b) {
    int x = get(a);
    int y = get(b);
    if (x == y) return;
    if (siize[x] < siize[y]) swap(x, y);
    par[y] = x;
    siize[x] += siize[y];
  }
};
class BinaryLift {
  vector<vector<int> > binlift;
  int n;

 public:
  BinaryLift(vector<int> rnk, vector<int> par) {
    n = (int)par.size();
    binlift.resize(n);
    for (int i = 0; i < n; i++) binlift[i].resize(20);
    for (int i = 0; i < n; i++) binlift[i][0] = par[i];
    for (int j = 1; j < 20; j++)
      for (int i = 0; i < n; i++) {
        if ((1 << j) < rnk[i])
          binlift[i][j] = binlift[binlift[i][j - 1]][j - 1];
        else
          binlift[i][j] = -1;
      }
  }

 public:
  int get_kth_ancestor(int x, int k) {
    int pt = x;
    for (int i = 19; i >= 0; i--) {
      if (pt == -1) exit(0);
      if (k & (1 << i)) pt = binlift[pt][i];
    }
    return pt;
  }

 public:
  int get_th_ancestor(int x, int k) {
    int pt = x;
    for (int i = 19; i >= 0; i--) {
      if (k & (1 << i)) pt = binlift[pt][i];
    }
    return pt;
  }
};
class SparseTable2D {
  vector<vector<vector<vector<int> > > > sparse;
  vector<vector<int> > inp;
  int m, n;

 private:
  int lg2(int x) {
    int out = 0;
    while ((1 << out) <= x) out++;
    return out - 1;
  }

 public:
  int rmin(int x1, int y1, int x2, int y2) {
    int lenx = x2 - x1 + 1;
    int lx = lg2(lenx) + 1;
    int leny = y2 - y1 + 1;
    int ly = lg2(leny) + 1;
    return min(
        min(sparse[lx][x1][ly][y1], sparse[lx][x1][ly][y2 + 1 - (1 << ly)]),
        min(sparse[lx][x2 + 1 - (1 << lx)][ly][y1],
            sparse[lx][x2 + 1 - (1 << lx)][ly][y2 + 1 - (1 << ly)]));
  }

 public:
  SparseTable2D(vector<vector<int> > input, string param) {
    n = input.size();
    m = input[0].size();
    inp = input;
    if (param == "min") prepromin();
  }

 private:
  void prepromin() {
    int lln, lm;
    lln = lg2(n) + 1;
    lm = lg2(m) + 1;
    sparse.resize(lln);
    for (int i = 0; i < lln; i++) sparse[i].resize(n);
    for (int i = 0; i < lln; i++)
      for (int j = 0; j < n; j++) sparse[i][j].resize(lm);
    for (int i = 0; i < lln; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < lm; k++) sparse[i][j][k].resize(m);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) sparse[0][i][0][j] = inp[i][j];
      for (int j = 1; j < lm; j++)
        for (int k = 0; k + (1 << j) - 1 < m; k++)
          sparse[0][i][j][k] = min(sparse[0][i][j - 1][k],
                                   sparse[0][i][j - 1][k + (1 << (j - 1))]);
    }
    for (int i = 1; i < lln; i++)
      for (int j = 0; j + (1 << i) - 1 < n; j++)
        for (int k = 0; k < lm; k++)
          for (int h = 0; h < m; h++)
            sparse[i][j][k][h] = min(sparse[i - 1][j][k][h],
                                     sparse[i - 1][j + (1 << (i - 1))][k][h]);
  }
};
class SparseTable {
  vector<vector<long long> > sparse;
  int n;
  vector<int> input;

 private:
  int lg2(int x) {
    int out = 0;
    while ((1 << out) <= x) out++;
    return out - 1;
  }

 public:
  int rmaxpos(int left, int right) {
    int len = right - left + 1;
    int lg = lg2(len);
    return (input[sparse[left][lg]] > input[sparse[left + len - (1 << lg)][lg]]
                ? sparse[left][lg]
                : sparse[left + len - (1 << lg)][lg]);
  }

 public:
  int rmaxval(int left, int right) {
    int len = right - left + 1;
    int lg = lg2(len);
    return (input[sparse[left][lg]] > input[sparse[left + len - (1 << lg)][lg]]
                ? input[sparse[left][lg]]
                : input[sparse[left + len - (1 << lg)][lg]]);
  }

 public:
  int rminpos(int left, int right) {
    int len = right - left + 1;
    int lg = lg2(len);
    return (input[sparse[left][lg]] < input[sparse[left + len - (1 << lg)][lg]]
                ? sparse[left][lg]
                : sparse[left + len - (1 << lg)][lg]);
  }

 public:
  int rminval(int left, int right) {
    int len = right - left + 1;
    int lg = lg2(len);
    return (input[sparse[left][lg]] < input[sparse[left + len - (1 << lg)][lg]]
                ? input[sparse[left][lg]]
                : input[sparse[left + len - (1 << lg)][lg]]);
  }

 public:
  long long rsum(int left, int right) {
    long long ans = 0;
    int pos;
    while (left <= right) {
      for (int i = 19; i >= 0; i--)
        if ((1 << i) <= right - left + 1) {
          pos = i;
          break;
        }
      ans += sparse[left][pos];
      left = left + (1 << pos);
    }
    return ans;
  }

 public:
  SparseTable(vector<int> inp, string operation) {
    input = inp;
    n = inp.size();
    if (operation == "min")
      prepromin();
    else if (operation == "max")
      prepromax();
    else if (operation == "sum")
      preprosum();
  }

 private:
  void prepromin() {
    sparse.resize(n);
    int x = lg2(n) + 1;
    for (int i = 0; i < n; i++) sparse[i].resize(x);
    for (int i = 0; i < n; i++) sparse[i][0] = i;
    for (int j = 1; j < x; j++)
      for (int i = 0; i + (1 << (j)) - 1 < n; i++)
        sparse[i][j] =
            (input[sparse[i][j - 1]] < input[sparse[i + (1 << (j - 1))][j - 1]]
                 ? sparse[i][j - 1]
                 : sparse[i + (1 << (j - 1))][j - 1]);
  }
  void prepromax() {
    sparse.resize(n);
    int x = lg2(n) + 1;
    for (int i = 0; i < n; i++) sparse[i].resize(x);
    for (int i = 0; i < n; i++) sparse[i][0] = i;
    for (int j = 1; j < x; j++)
      for (int i = 0; i + (1 << (j)) - 1 < n; i++)
        sparse[i][j] =
            (input[sparse[i][j - 1]] > input[sparse[i + (1 << (j - 1))][j - 1]]
                 ? sparse[i][j - 1]
                 : sparse[i + (1 << (j - 1))][j - 1]);
  }
  void preprosum() {
    sparse.resize(n);
    int x = lg2(n) + 1;
    for (int i = 0; i < n; i++) sparse[i].resize(x);
    for (int i = 0; i < n; i++) sparse[i][0] = input[i];
    for (int j = 1; j < x; j++)
      for (int i = 0; i + (1 << (j)) - 1 < n; i++)
        sparse[i][j] = sparse[i][j - 1] + sparse[i + (1 << (j - 1))][j - 1];
  }
};
class Vector {
 public:
  pair<long long, long long> x;

 public:
  Vector(pair<long long, long long> a, pair<long long, long long> b) {
    x.first = b.first - a.first;
    x.second = b.second - a.second;
  }

 public:
  long double getMagnitude() {
    return sqrtl(x.first * x.first + x.second * x.second);
  }
};
class Line {
 public:
  pair<long long, long long> x, y;

 public:
  Line(pair<long long, long long> a, pair<long long, long long> b) {
    x = a;
    y = b;
  }

 private:
  long double dotProduct(Vector a, Vector b) {
    return a.x.first * b.x.first + a.x.second * b.x.second;
  }

 private:
  long double crossProduct(Vector a, Vector b) {
    return a.x.first * b.x.second - a.x.second * b.x.first;
  }

 private:
  long double magnitude(Vector a) { return a.getMagnitude(); }

 public:
  long double distanceToA(pair<long long, long long> c) {
    return dotProduct(Vector(x, y), Vector(x, c)) / magnitude(Vector(x, y));
  }

 public:
  long double orthogonalDistance(pair<long long, long long> c) {
    return crossProduct(Vector(x, y), Vector(x, c)) / magnitude(Vector(x, y));
  }

 public:
  pair<long double, long double> intersection(Line l) {
    pair<long double, long double> ans;
    ans.first = (long double)((x.first * y.second - x.second * y.first) *
                                  (l.x.first - l.y.first) -
                              (x.first - y.first) * (l.x.first * l.y.second -
                                                     l.x.second * l.y.first)) /
                ((x.first - y.first) * (l.x.second - l.y.second) -
                 (x.second - y.second) * (l.x.first - l.y.first));
    ans.second =
        (long double)((x.first * y.second - x.second * y.first) *
                          (l.x.second - l.y.second) -
                      (x.second - y.second) *
                          (l.x.first * l.y.second - l.x.second * l.y.first)) /
        ((x.first - y.first) * (l.x.second - l.y.second) -
         (x.second - y.second) * (l.x.first - l.y.first));
    return ans;
  }
};
class PruferCode {
  vector<int> code;
  vector<pair<int, int> > edges;

 public:
  PruferCode(vector<int> cc) {
    code = cc;
    findEdges();
  }

 private:
  void findEdges() {
    map<int, int> mp;
    set<int> has;
    set<int> wait;
    for (int z : code) {
      mp[z]++;
      has.insert(z);
    }
    for (int i = 0; i < code.size() + 2; i++)
      if (!has.count(i)) wait.insert(i);
    for (int i = 0; i < code.size(); i++) {
      int now = *wait.begin();
      edges.push_back(make_pair(now, code[i]));
      mp[now]++;
      mp[code[i]]--;
      if (mp[code[i]] == 0) {
        has.erase(code[i]);
        wait.insert(code[i]);
      }
      wait.erase(now);
    }
    assert(wait.size() == 2);
    edges.push_back(make_pair(*wait.begin(), *wait.rbegin()));
  }

 public:
  vector<pair<int, int> > getEdges() { return edges; }
};
class Segment {
  pair<long long, long long> x, y;

 public:
  Segment(pair<long long, long long> a, pair<long long, long long> b) {
    x = a;
    y = b;
  }

 private:
  long double dotProduct(Vector a, Vector b) {
    return a.x.first * b.x.first + a.x.second * b.x.second;
  }

 private:
  long double crossProduct(Vector a, Vector b) {
    return a.x.first * b.x.second - a.x.second * b.x.first;
  }

 private:
  long double magnitude(Vector a) { return a.getMagnitude(); }

 public:
  long double distanceToA(pair<long long, long long> c) {
    return dotProduct(Vector(x, y), Vector(x, c)) / magnitude(Vector(x, y));
  }

 public:
  long double distanceToSegment(pair<long long, long long> c) {
    if (distanceToA(c) >= 0 && distanceToA(c) <= magnitude(Vector(x, y)))
      return crossProduct(Vector(x, y), Vector(x, c)) / magnitude(Vector(x, y));
    else
      return min(magnitude(Vector(x, c)), magnitude(Vector(y, c)));
  }
};
class SegmentTree {
  int len;
  vector<long long> all;
  vector<long long> seg;
  vector<long long> lazy;

 public:
  SegmentTree(vector<long long> inp, char op) {
    len = inp.size();
    all = inp;
    seg.resize(4 * len);
    lazy.resize(4 * len);
    build(1, 0, len - 1);
  }
  void build(int id, int l, int r) {
    if (l == r) {
      seg[id] = all[l];
      return;
    }
    int mid = (l + r) / 2;
    build(2 * id, l, mid);
    build(2 * id + 1, mid + 1, r);
    seg[id] = seg[2 * id] + seg[2 * id + 1];
  }
  void update(int id, int l, int r, int sl, int sr, long long val) {
    if (lazy[id] != -1) {
      seg[id] = lazy[id] * (r - l + 1);
      if (l != r) {
        lazy[2 * id] = lazy[id];
        lazy[2 * id + 1] = lazy[id];
      }
      lazy[id] = -1;
    }
    if (r < sl || sr < l) return;
    if (sl <= l && sr >= r) {
      seg[id] = val * (r - l + 1);
      if (l != r) {
        lazy[2 * id] = val;
        lazy[2 * id + 1] = val;
      }
      return;
    }
    int mid = (l + r) / 2;
    update(2 * id, l, mid, sl, sr, val);
    update(2 * id + 1, mid + 1, r, sl, sr, val);
    seg[id] = seg[2 * id] + seg[2 * id + 1];
  }
  long long query(int id, int l, int r, int sl, int sr) {
    if (lazy[id] != -1) {
      seg[id] = lazy[id] * (r - l + 1);
      if (r != l) {
        lazy[2 * id] = lazy[id];
        lazy[2 * id + 1] = lazy[id];
      }
      lazy[id] = -1;
    }
    if (r < sl || sr < l) return 0;
    if (sl <= l && sr >= r) return seg[id];
    int mid = (l + r) / 2;
    return (query(2 * id, l, mid, sl, sr) +
            query(2 * id + 1, mid + 1, r, sl, sr));
  }
};
class HopcroftKarp {
  vector<int> matched;
  vector<vector<pair<int, int> > > adj;
  int left;
  int right;

 public:
  HopcroftKarp(vector<vector<pair<int, int> > > inp, int l, int r) {
    adj = inp;
    left = l;
    matched.resize(l);
    for (int i = 0; i < l; i++) matched[i] = -1;
    right = r;
  }

 public:
  vector<int> match() {
    bool cont = true;
    set<int> lfree, rfree;
    for (int i = 0; i < left; i++) lfree.insert(i);
    for (int i = left; i < left + right; i++) rfree.insert(i);
    vector<bool> yet(left, 0);
    for (int i = 0; i < left; i++)
      for (int j = 0; j < adj[i].size(); j++)
        if (adj[i][j].second == 1 && rfree.count(adj[i][j].first) && !yet[i]) {
          yet[i] = true;
          matched[i] = adj[i][j].first;
          adj[i][j].second = 2;
          for (int x = 0; x < adj[adj[i][j].first].size(); x++)
            if (adj[adj[i][j].first][x].first == i)
              adj[adj[i][j].first][x].second = 2;
          rfree.erase(adj[i][j].first);
          lfree.erase(i);
        }
    while (cont) {
      vector<int> par(left + right, -1);
      queue<pair<int, int> > kyou;
      for (int z : lfree) kyou.push(make_pair(z, 0));
      int update = -1;
      vector<int> vis(left + right, 0);
      while (kyou.size()) {
        pair<int, int> frt = kyou.front();
        kyou.pop();
        if (rfree.count(frt.first)) {
          update = frt.first;
          break;
        }
        if (frt.second == 0) {
          for (pair<int, int> z : adj[frt.first]) {
            if (z.second == 1 && !vis[z.first]) {
              par[z.first] = frt.first;
              vis[z.first] = 1;
              kyou.push(make_pair(z.first, 1));
            }
          }
        } else {
          for (pair<int, int> z : adj[frt.first]) {
            if (z.second == 2 && !vis[z.first]) {
              par[z.first] = frt.first;
              vis[z.first] = 1;
              kyou.push(make_pair(z.first, 0));
            }
          }
        }
      }
      int x = update;
      int cnt = 0;
      while (x != -1 && par[x] != -1) {
        for (int i = 0; i < adj[x].size(); i++)
          if (adj[x][i].first == par[x]) {
            adj[x][i].second = (cnt == 0 ? 2 : 1);
            if (cnt == 0) {
              matched[par[x]] = x;
              rfree.erase(x);
              lfree.erase(par[x]);
            }
          }
        for (int i = 0; i < adj[par[x]].size(); i++)
          if (adj[par[x]][i].first == x)
            adj[par[x]][i].second = (cnt == 0 ? 2 : 1);
        cnt++;
        cnt %= 2;
        x = par[x];
      }
      if (update == -1) cont = false;
    }
    return matched;
  }
};
class Triangle {
  pair<long long, long long> x, y, z;

 public:
  Triangle(pair<long long, long long> a, pair<long long, long long> b,
           pair<long long, long long> c) {
    x = a;
    y = b;
    z = c;
  }

 private:
  long double crossProduct(Vector a, Vector b) {
    return a.x.first * b.x.second - a.x.second * b.x.first;
  }

 public:
  long double perimeter() {
    long double s1, s2, s3;
    s1 = Vector(x, y).getMagnitude();
    s2 = Vector(y, z).getMagnitude();
    s3 = Vector(x, z).getMagnitude();
    return s1 + s2 + s3;
  }

 public:
  long double area() { return abs(crossProduct(Vector(x, y), Vector(x, z))); }
};
class SuffixArray {
  vector<int> order;
  vector<int> lcp;
  string str;

 public:
  SuffixArray(string in) {
    str = in;
    str += '$';
    order.resize(str.length());
    lcp.resize(str.length());
    compute();
  }
  void compute() {
    vector<pair<char, int> > a;
    vector<int> equi(order.size());
    vector<int> bij(order.size());
    for (int i = 0; i < str.length(); i++) a.push_back(make_pair(str[i], i));
    sort(a.begin(), a.end());
    for (int i = 0; i < str.length(); i++) order[i] = a[i].second;
    equi[order[0]] = 0;
    int r = 0;
    for (int i = 1; i < str.length(); i++) {
      if (a[i].first == a[i - 1].first)
        equi[order[i]] = r;
      else
        equi[order[i]] = ++r;
    }
    int k = 0;
    while ((1 << k) < str.length()) {
      vector<pair<pair<int, int>, int> > a(str.length());
      for (int i = 0; i < str.length(); i++)
        a[i] = make_pair(
            make_pair(equi[i], equi[(i + (1 << k)) % (str.length())]), i);
      sort(a.begin(), a.end());
      for (int i = 0; i < str.length(); i++) {
        order[i] = a[i].second;
        bij[order[i]] = i;
      }
      int r = 0;
      equi[order[0]] = 0;
      for (int i = 1; i < str.length(); i++) {
        if (a[i].first == a[i - 1].first)
          equi[order[i]] = r;
        else
          equi[order[i]] = ++r;
      }
      k++;
    }
    k = 0;
    for (int i = 0; i < str.length() - 1; i++) {
      int p = bij[i];
      int j = order[p - 1];
      while (i + k < str.length() && j + k < str.length() &&
             str[i + k] == str[j + k])
        k++;
      lcp[p] = k;
      k = max(k - 1, 0);
    }
  }

 public:
  int count(string ptr) {
    int low = 0;
    int hi = str.length() - 1;
    int res1, res2;
    res1 = 0;
    res2 = 1e9;
    while (low <= hi) {
      int mid = (low + hi) / 2;
      bool gr = false;
      int i = 0;
      for (i; i < min(ptr.length(), str.length() - order[mid]); i++) {
        if (ptr[i] != str[order[mid] + i]) {
          if (ptr[i] > str[order[mid] + i]) gr = true;
          break;
        }
      }
      if (i == ptr.length()) {
        res2 = min(res2, mid);
        hi = mid - 1;
      } else if (!gr)
        hi = mid - 1;
      else
        low = mid + 1;
    }
    low = 0;
    hi = str.length() - 1;
    while (low <= hi) {
      int mid = (low + hi) / 2;
      bool gr = false;
      int i = 0;
      for (i; i < min(ptr.length(), str.length() - order[mid] + 1); i++) {
        if (ptr[i] != str[order[mid] + i]) {
          if (ptr[i] > str[order[mid] + i]) gr = true;
          break;
        }
      }
      if (i == ptr.length()) {
        res1 = max(res1, mid);
        low = mid + 1;
      } else if (!gr)
        hi = mid - 1;
      else
        low = mid + 1;
    }
    if (res2 == 1e9) return 0;
    return (res1 - res2 + 1);
  }

 public:
  vector<int> get() { return order; }

 public:
  vector<int> getLcp() { return lcp; }

 public:
  long long diffSubstrings() {
    long long out = 0;
    for (int i = 1; i < str.length(); i++)
      out += str.length() - order[i] - lcp[i] - 1;
    return out;
  }
};
string longestCommonSubstring(string a, string b) {
  int len = 0;
  string res = a + '%' + b;
  SuffixArray sf = SuffixArray(res);
  vector<int> order = sf.get();
  vector<int> lcp = sf.getLcp();
  vector<int> col(order.size());
  for (int i = 0; i < order.size(); i++) {
    if (order[i] < a.length())
      col[order[i]] = 1;
    else if (order[i] > a.length())
      col[order[i]] = 2;
  }
  int pos = -1;
  for (int i = 1; i < order.size(); i++)
    if (col[order[i]] + col[order[i - 1]] == 3) {
      if (lcp[i] > len) {
        len = max(len, lcp[i]);
        pos = (col[order[i]] == 1 ? order[i] : order[i - 1]);
      }
    }
  return a.substr(pos, len);
}
void dfs(int x, int par, vector<long long> &has, vector<int> &rank,
         vector<vector<int> > &adj) {
  bool in = true;
  for (int z : adj[x]) {
    if (z != par) {
      in = false;
      rank[z] = rank[x] + 1;
      dfs(z, x, has, rank, adj);
      has[x] += has[z];
    }
  }
  has[x] += in;
}
void solve() {
  long long n, s;
  cin >> n >> s;
  vector<vector<int> > adj(n);
  vector<pair<pair<int, int>, long long> > mp;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
    mp.push_back(make_pair(make_pair(min(a, b), max(a, b)), c));
  }
  vector<long long> has(n, 0);
  vector<int> rank(n, 0);
  dfs(0, -1, has, rank, adj);
  set<pair<long long, int> > st;
  long long sum = 0;
  for (int i = 0; i < n - 1; i++) {
    sum += has[(rank[mp[i].first.first] < rank[mp[i].first.second]
                    ? mp[i].first.second
                    : mp[i].first.first)] *
           (mp[i].second);
    st.insert(make_pair(has[(rank[mp[i].first.first] < rank[mp[i].first.second]
                                 ? mp[i].first.second
                                 : mp[i].first.first)] *
                            (mp[i].second - mp[i].second / 2),
                        i));
  }
  int cnt = 0;
  while (sum > s) {
    pair<long long, int> x = *st.rbegin();
    sum -= x.first;
    mp[x.second].second /= 2;
    st.erase(x);
    st.insert(make_pair(
        has[(rank[mp[x.second].first.first] < rank[mp[x.second].first.second]
                 ? mp[x.second].first.second
                 : mp[x.second].first.first)] *
            (mp[x.second].second - mp[x.second].second / 2),
        x.second));
    cnt++;
  }
  cout << cnt << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
}