#include <bits/stdc++.h>
typedef int OwO; typedef double UwU;
#define int long long
#define double long double

using namespace std;

OwO main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  while(q--){
    int k,n,a,b;
    cin >> k >> n >> a >> b;
    int turns = k/a;
    if(k%n==0 && k/n==a)
      turns--;
    if(turns>=n){
      cout << n << endl;
      continue;
    }
    bool did = false;
    double quant = (double)(0.0+-b*n+k)/(0.0+a-b);
    if(quant <= 0.000000001)
      cout << -1 << endl;
    else
      cout << (int)floor(quant-0.0000000000001) << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
typedef int OwO; typedef double UwU;
#define int long long
#define double long double

using namespace std;

OwO main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  while(q--){
    int k,n,a,b;
    cin >> k >> n >> a >> b;
    k -= n*b;
    if(k<=0)
      cout << -1 << endl;
    else{
      a-=b;
      cout << min((k-1)/a, n) << endl;
    }
  }

  return 0;
}


#include <bits/stdc++.h>
typedef int OwO; typedef double UwU;
#define int long long
#define double long double

using namespace std;

OwO main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  while(q--){
    int k,n,a,b;
    cin >> k >> n >> a >> b;
    int turns = k/a;
    if(k%n==0 && k/n==a)
      turns--;
    if(turns>=n){
      cout << n << endl;
      continue;
    }
    bool did = false;
    double quant = (double)(0.0+-b*n+k)/(0.0+a-b);
    if(quant <= 0)
      cout << -1 << endl;
    else
      cout << floor(quant-0.0000001) << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
typedef int OwO; typedef double UwU;
#define int long long
#define double long double

using namespace std;

OwO main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  while(q--){
    int k,n,a,b;
    cin >> k >> n >> a >> b;
    int turns = k/a;
    if(k%n==0 && k/n==a)
      turns--;
    if(turns>=n){
      cout << n << endl;
      continue;
    }
    bool did = false;
    while(turns>=0){
      if(b*(n-turns) < (k-(turns*a))){
        cout << turns << endl;
        did = true;
        break;
      }
      turns--;
    }
    if(!did)
      cout << -1 << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
typedef int OwO; typedef double UwU;
#define int long long
#define double long double

using namespace std;

OwO main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  while(q--){
    int k,n,a,b;
    cin >> k >> n >> a >> b;
    int turns = k/a;
    if(k%n==0 && k/n==a)
      turns--;
    if(turns>=n){
      cout << n << endl;
      continue;
    }
    bool did = false;
    double quant = (double)(0.0+-b*n+k)/(0.0+a-b);
    if(quant == 0)
      cout << -1 << endl;
    else
      cout << floor(quant-0.0000001) << endl;
  }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
 int k , n , a, b;
int main() {
    int q;
    cin >> q;
    while(q--) {

        cin >> k >> n >> a >> b;
        if(  k/b < n ) {
           // cout << k - (n*b)<<"hhh" << endl;
            cout << -1 << '\n';
        }
        else{
            k--;
            k = k - (n*b);
            a = a - b;
            int ans = min(k/a , n);
            cout << ans << endl;
    }
}
}


#include<bits/stdc++.h>
using namespace std;
 int k , n , a, b;
 int isp(int x) {
     if(x*a + (n-x)*b < k) return 1;
     return 0;
 }
int main() {
    int q;
    cin >> q;
    while(q--) {

        cin >> k >> n >> a >> b;
        if(k - (n*b) < 1) {
           // cout << k - (n*b)<<"hhh" << endl;
            cout << -1 << '\n';
        }
        else{
           // cout<<"lll"<<endl;
            int m , l , r;
            l = 0 , r = 1e9;
            while(r - l > 1) {
                m = (l+r)/2;

                if(isp(m)) l = m;
                else r = m;
            }
            if(l > n) l = n;
            cout << l << '\n';
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
 int k , n , a, b;
 int isp(int x) {
     if(x*a + (n-x)*b < k) return 1;
     return 0;
 }
int main() {
    int q;
    cin >> q;
    while(q--) {

        cin >> k >> n >> a >> b;
        if(k - (n*b) < 1) {
           // cout << k - (n*b)<<"hhh" << endl;
            cout << -1 << '\n';
        }
        else{
           // cout<<"lll"<<endl;
            int m , l , r;
            l = 0 , r = n+1;
            while(r-l > 1) {
                m = (l+r)/2;
                if(m>r){
                    r = m;
                    continue;
                }
                if(isp(m)) l = m;
                else r = m;
            }
            if(l > n) l = n;
            cout << l << '\n';
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
 int k , n , a, b;
 int isp(int x) {
     if(x*a + (n-x)*b < k) return 1;
     return 0;
 }
int main() {
    int q;
    cin >> q;
    while(q--) {

        cin >> k >> n >> a >> b;
        if( ( k - (n*b) < 1 ) || (k<a && k<b)) {
           // cout << k - (n*b)<<"hhh" << endl;
            cout << -1 << '\n';
        }
        else{
           // cout<<"lll"<<endl;
            int m , l , r;
            l = 0 , r = n+1;
            int ans = 0;
            while(r-l > 1) {
                m = (l+r)/2;
                if(m>r){
                    r = m;
                    continue;
                }
                if(isp(m)){
                    l = m;
                    ans = max(ans , m);
                }
                else r = m;
            }

            cout << ans << '\n';
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
 int k , n , a, b;
 int isp(int x) {
     if(x>n) return 0;
     if(x*a + (n-x)*b < k) return 1;
     return 0;
 }
int main() {
    int q;
    cin >> q;
    while(q--) {

        cin >> k >> n >> a >> b;
        if(k - (n*b) < 1) {
           // cout << k - (n*b)<<"hhh" << endl;
            cout << -1 << '\n';
        }
        else{
           // cout<<"lll"<<endl;
            int m , l , r;
            l = 0 , r = n+1;
            while(r - l > 1) {
                m = (l+r)/2;

                if(isp(m)) l = m;
                else r = m;
            }
            if(l > n) l = n;
            cout << l << '\n';
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
 int k , n , a, b;
 int isp(int x) {
     if(x>n) return 0;
     if(x*a + (n-x)*b < k) return 1;
     return 0;
 }
int main() {
    int q;
    cin >> q;
    while(q--) {

        cin >> k >> n >> a >> b;
        if((k-1)/b < n) {
           // cout << k - (n*b)<<"hhh" << endl;
            cout << -1 << '\n';
        }
        else{
           // cout<<"lll"<<endl;
            int m , l , r;
            l = 0 , r = n+1;
            while(r - l > 1) {
                m = (l+r)/2;

                if(isp(m)) l = m;
                else r = m;
            }
            if(l > n) l = n;
            cout << l << '\n';
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
 int k , n , a, b;
int main() {
    int q;
    cin >> q;
    while(q--) {

        cin >> k >> n >> a >> b;
        if( ( k - (n*b) < 1 ) || (k<a && k<b)) {
           // cout << k - (n*b)<<"hhh" << endl;
            cout << -1 << '\n';
        }
        else{
            k--;
            k = k - (n*b);
            a = a - b;
            int ans = min(k/a , n);
            cout << ans << endl;
    }
}
}


#include<bits/stdc++.h>
using namespace std;
 int k , n , a, b;
 int isp(int x) {
     if(x*a + (n-x)*b < k) return 1;
     return 0;
 }
int main() {
    int q;
    cin >> q;
    while(q--) {

        cin >> k >> n >> a >> b;
        if(k - (n*b) <= 0) {
           // cout << k - (n*b)<<"hhh" << endl;
            cout << -1 << '\n';
        }
        else{
           // cout<<"lll"<<endl;
            int m , l , r;
            l = 0 , r = 1e9;
            while(r - l > 1) {
                m = (l+r)/2;

                if(isp(m)) l = m;
                else r = m;
            }
            if(l > n) l = n;
            cout << l << '\n';
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
 int k , n , a, b;
 int isp(int x) {
     if(x*a + (n-x)*b < k) return 1;
     return 0;
 }
int main() {
    int q;
    cin >> q;
    while(q--) {

        cin >> k >> n >> a >> b;
        if(k - (n*b) < 1) {
           // cout << k - (n*b)<<"hhh" << endl;
            cout << -1 << '\n';
        }
        else{
           // cout<<"lll"<<endl;
            int m , l , r;
            l = 0 , r = n+1;
            int ans = 0;
            while(r-l > 1) {
                m = (l+r)/2;
                if(m>r){
                    r = m;
                    continue;
                }
                if(isp(m)){
                    l = m;
                    ans = max(ans , m);
                }
                else r = m;
            }

            cout << ans << '\n';
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
 int k , n , a, b;
int main() {
    int q;
    cin >> q;
    while(q--) {

        cin >> k >> n >> a >> b;
        k--;
        if(  k/b < n ) {
           // cout << k - (n*b)<<"hhh" << endl;
            cout << -1 << '\n';
        }
        else{
            k = k - (n*b);
            a = a - b;
            int ans = min(k/a , n);
            cout << ans << endl;
    }
}
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=1e9+7;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=1e5+5;

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	ll q;cin>>q;
	while(q--){
		ll k,n,a,b;cin>>k>>n>>a>>b;
		if(k-n*b<=0){cout<<-1<<endl;continue;}
		ll st=0,en=n,l=0;
		while(st<=en){
			ll mid=st+en>>1;
			if(k-mid*a-(n-mid)*b>0)l=mid,st=mid+1;
			else en=mid-1;
		}
		cout<<l<<endl;
	}
}


#include <iostream>

const int INF = 1000000000;

int main() {
    int q;

    std::cin >> q;

    for (int i = 0; i < q; ++i) {
        int k, n, a, b;

        std::cin >> k >> n >> a >> b;

        int played = b * n;

        if (played >= k) {
            std::cout << -1 << "\n";
        } else {
            int justPlay = 0;
            int dif = a - b;

            while (k > played && justPlay < (n + 1)) {
                played += dif;
                justPlay++;
            }

            std::cout << (justPlay - 1) << "\n";
        }
    }

    return 0;
}


#include <iostream>

const int INF = 1000000000;

int main() {
    int q;

    std::cin >> q;

    for (int i = 0; i < q; ++i) {
        int k, n, a, b;

        std::cin >> k >> n >> a >> b;

        int played = b * n;

        if (played > k) {
            std::cout << -1 << "\n";
        } else {
            int justPlay = 0;
            int dif = a - b;

            while (k > played && justPlay < (n + 1)) {
                played += dif;
                justPlay++;
            }

            std::cout << (--justPlay) << "\n";
        }
    }

    return 0;
}


#include <iostream>

const int INF = 1000000000;

int main() {
    int q;

    std::cin >> q;

    for (int i = 0; i < q; ++i) {
        long long  k, n, a, b;

        std::cin >> k >> n >> a >> b;

        long long played = b * n;

        if (played >= k) {
            std::cout << -1 << "\n";
        } else {
            long long  dif = a - b;
            long long  rest = k - played;

            while (rest <= dif * n) {
                n--;
            }

            std::cout << n << "\n";
        }
    }

    return 0;
}


#include <iostream>

const int INF = 1000000000;

int main() {
    int q;

    std::cin >> q;

    for (int i = 0; i < q; ++i) {
        int k, n, a, b;

        std::cin >> k >> n >> a >> b;

        int played = b * n;

        if (played >= k) {
            std::cout << -1 << "\n";
        } else {
            int justPlay = 0;
            int dif = a - b;

            while (k >= played && justPlay < (n + 1)) {
                played += dif;
                justPlay++;
            }

            std::cout << (--justPlay) << "\n";
        }
    }

    return 0;
}


#include <iostream>

const int INF = 1000000000;

int main() {
    int q;

    std::cin >> q;

    for (int i = 0; i < q; ++i) {
        int k, n, a, b;

        std::cin >> k >> n >> a >> b;

        int played = b * n;

        if (played > k) {
            std::cout << -1 << "\n";
        } else {
            int justPlay = 0;
            int dif = a - b;
            int rest = k - played;

            if (rest > dif) {
                justPlay = rest / dif;
                if (rest % dif != 0) {
                    std::cout << std::min(justPlay, n) << "\n";
                } else {
                    std::cout << std::min((--justPlay), n) << "\n";
                }
            } else {
                std::cout << 0 << "\n";
            }
        }
    }

    return 0;
}


#include <iostream>

const int INF = 1000000000;

int main() {
    int q;

    std::cin >> q;

    for (int i = 0; i < q; ++i) {
        int k, n, a, b;

        std::cin >> k >> n >> a >> b;

        int played = b * n;

        if (played >= k) {
            std::cout << -1 << "\n";
        } else {
            int justPlay = 0;
            int dif = a - b;
            int rest = k - played;

            if (rest > dif) {
                justPlay = rest / dif;
                if (rest % dif != 0) {
                    std::cout << std::min(justPlay, n) << "\n";
                } else {
                    std::cout << std::min((--justPlay), n) << "\n";
                }
            } else {
                std::cout << 0 << "\n";
            }
        }
    }

    return 0;
}


#include <iostream>

const int INF = 1000000000;

int main() {
    int q;

    std::cin >> q;

    for (int i = 0; i < q; ++i) {
        int k, n, a, b;

        std::cin >> k >> n >> a >> b;

        int played = b * n;

        if (played >= k) {
            std::cout << -1 << "\n";
        } else {
            int dif = a - b;
            int rest = k - played;

            while (rest <= dif * n) {
                n--;
            }

            std::cout << n << "\n";
        }
    }

    return 0;
}


#include <iostream>

const int INF = 1000000000;

int main() {
    int q;

    std::cin >> q;

    for (int i = 0; i < q; ++i) {
        long long  k, n, a, b;

        std::cin >> k >> n >> a >> b;

        long long played = b * n;

        if (played >= k) {
            std::cout << -1 << "\n";
        } else {
            long long justPlay = 0;
            long long dif = a - b;
            long long rest = k - played;

            if (rest > dif) {
                justPlay = rest / dif;
                if (rest % dif != 0) {
                    std::cout << std::min(justPlay, n) << "\n";
                } else {
                    std::cout << std::min((--justPlay), n) << "\n";
                }
            } else {
                std::cout << 0 << "\n";
            }
        }
    }

    return 0;
}


#include <iostream>

const int INF = 1000000000;

int main() {
    int q;

    std::cin >> q;

    for (int i = 0; i < q; ++i) {
        int k, n, a, b;

        std::cin >> k >> n >> a >> b;

        int played = b * n;

        if (played > k) {
            std::cout << -1 << "\n";
        } else {
            int justPlay = 0;
            int dif = a - b;

            while (k > played && justPlay < (n + 1)) {
                played += dif;
                justPlay++;
            }

            std::cout << (justPlay - 1) << "\n";
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll n,a,b,k,l,r,mid,ans;
bool flag;

bool  check(ll x){
    return a*x+(n-x)*b<k;
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld%lld%lld",&k,&n,&a,&b);
        if(b*n>=k){
            printf("-1\n");
            continue;
        }
        l=0;r=n;
        while(l<=r){
            mid=(l+r)/2;
            if(check(mid)){
                ans=mid;
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        printf("%lld\n",ans);

    }
    //system("pause");
}
/*
6
15 5 20 20
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
 */


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll n,a,b,k,l,r,mid,ans;
bool flag;

bool  check(ll x){
    return a*x+(n-x)*b<k;
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld%lld%lld",&k,&n,&a,&b);
        /* if(b*n>=k){
            printf("-1\n");
            continue;
        }*/
        l=0;r=n;
        while(l<=r){
            mid=(l+r)/2;
            if(check(mid)){
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        if(!check(mid)) mid--;
        printf("%lld\n",mid);

    }
    //system("pause");
}
/*
6
15 5 20 20
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
 */


#include<bits/stdc++.h>
using namespace std;
int t,n,a,b,k;
int ans,l,r,mid;
bool flag;

bool  check(int x){
    return a*x+(n-x)*b<k;
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d%d",&k,&n,&a,&b);
        l=0;r=n;
        while(l<=r){
            mid=(l+r)/2;
            if(check(mid)){
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        if(!check(mid)){
            mid=mid-1;
        }
        printf("%d\n",mid);

    }
    //system("pause");
}
/*
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
 */


#include<bits/stdc++.h>
using namespace std;
int t,n,a,b,k;
int ans,l,r,mid;
bool flag;

bool  check(int x){
    if(x>n){
        return a*x<k;
    }
    return a*x+(n-x)*b<k;
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d%d",&k,&n,&a,&b);
        l=0;r=n+5;
        while(l<=r){
            mid=(l+r)/2;
            if(check(mid)){
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        if(!check(mid)){
            mid=mid-1;
        }
        if(mid>n)
        mid=n;
        if(mid<0)
        mid=-1;
        printf("%d\n",mid);

    }
   // system("pause");
}
/*
6
15 5 20 20
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
 */


#include<bits/stdc++.h>
using namespace std;
int t,n,a,b,k;
int ans;

int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d%d",&k,&n,&a,&b);
        ans=k-n*b;
        ans/=(a-b);
        if((k-n*b)%(a+b)==0){
            ans--;
        }
        if(ans>=0){
            if(ans<=n)
                printf("%d\n",ans);
            else{
                printf("%d\n",n);
            }
        }
        else{
            printf("-1\n");
        }

    }
    //system("pause");
}
/*
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
 */


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll n,a,b,k,l,r,mid;
bool flag;

bool  check(ll x){
    return a*x+(n-x)*b<k;
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld%lld%lld",&k,&n,&a,&b);
        if(b*n>=k){
            printf("-1\n");
            continue;
        }
        l=0;r=n;
        while(l<=r){
            mid=(l+r)/2;
            if(check(mid)){
                ans=mid;
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        printf("%lld\n",ans);

    }
   // system("pause");
}
/*
6
15 5 20 20
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
 */


#include<bits/stdc++.h>
using namespace std;
int t,n,a,b,k;
int ans,l,r,mid;
bool flag;

bool  check(int x){
    if(x>n){
        return a*x<k;
    }
    return a*x+(n-x)*b<k;
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d%d",&k,&n,&a,&b);
        l=0;r=(int)1e9+7;
        while(l<=r){
            mid=(l+r)/2;
            if(check(mid)){
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        if(!check(mid)){
            mid=mid-1;
        }
        if(mid>n)
        mid=n;
        if(mid<0)
        mid=-1;
        printf("%d\n",mid);

    }
    //system("pause");
}
/*
6
15 5 20 20
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
 */


#include<bits/stdc++.h>
using namespace std;
int t,n,a,b,k;
int ans,l,r,mid;
bool flag;

bool  check(int x){
    return a*x+(n-x)*b<k;
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d%d",&k,&n,&a,&b);
        if(b*n>=k){
            printf("-1\n");
            continue;
        }
        l=0;r=n;
        while(l<=r){
            mid=(l+r)/2;
            if(check(mid)){
                ans=mid;
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        printf("%d\n",ans);

    }
   // system("pause");
}
/*
6
15 5 20 20
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
 */


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll n,a,b,k,l,r,mid,ans;
bool flag;

bool  check(ll x){
    return a*x+(n-x)*b<k;
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld%lld%lld",&k,&n,&a,&b);
        if(b*n>=k){
            printf("-1\n");
            continue;
        }
        l=0;r=n;
        while(l<=r){
            mid=(l+r)/2;
            if(check(mid)){
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        if(!check(mid)) mid--;
        printf("%lld\n",mid);

    }
    //system("pause");
}
/*
6
15 5 20 20
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
 */


#include<bits/stdc++.h>
using namespace std;
#define ff                first
#define ss                second
#define int              long long
#define pb                push_back
#define mp                make_pair
#define li                list<int>
#define pii               pair<int,int>
#define vit                vector<int>
#define mii               map<int ,int>
#define setbits(x)        __builtin_popcountll(x)
#define zrobits(x)        _builtin_ctzll(x)
#define mod               1000000007
#define inf               1e18
#define ln                cout<<'\n'
#define ps(x,y)           fixed<<setprecision(y)<<x
#define mk(arr,n,type)    type *arr=new type(n);
#define w(x)              int x; cin>>x; while(x--)
#define all(x)            (x).begin(), (x).end()
#define PI                3.14159265358979
#define p_sieve           100000
//const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1}, dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("g.txt", "r", stdin);
	freopen("b.txt", "w", stdout);
#endif
}

int  a[111111], b[111111], c[111111];

void solve()
{
	int k, n, a, b; cin >> k >> n >> a >> b;
	int temp = n;
	int counter = 0;
	int cinter = 0;
	if (a * n < k and k - (a * n) > b)
	{
		cout << n << "\n";
		return;
	}
	for (int i = 1; i <= n; i++)
	{
		if (((k - a) / b) >= (n - i) and  (k - a) > b)
		{
			k = k - a;
			counter++;
		}
		else break;
	}
	int x = n - counter;
	if (counter < n)
	{
		int temp = 0;
		while (k > b)
		{
			k = k - b;
			temp++;
		}
		if (temp >= x)cout << counter << "\n";
		else cout << "-1\n";
	}
	else cout << counter << "\n";

}

int32_t main()
{
	c_p_c();
	w(t)
	{
		solve();
		ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	}

}


#include<bits/stdc++.h>
using namespace std;
#define ff                first
#define ss                second
#define int              long long
#define pb                push_back
#define mp                make_pair
#define li                list<int>
#define pii               pair<int,int>
#define vit                vector<int>
#define mii               map<int ,int>
#define setbits(x)        __builtin_popcountll(x)
#define zrobits(x)        _builtin_ctzll(x)
#define mod               1000000007
#define inf               1e18
#define ln                cout<<'\n'
#define ps(x,y)           fixed<<setprecision(y)<<x
#define mk(arr,n,type)    type *arr=new type(n);
#define w(x)              int x; cin>>x; while(x--)
#define all(x)            (x).begin(), (x).end()
#define PI                3.14159265358979
#define p_sieve           100000
//const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1}, dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("g.txt", "r", stdin);
	freopen("b.txt", "w", stdout);
#endif
}

int  a[111111], b[111111], c[111111];

void solve()
{
	int k, n, a, b; cin >> k >> n >> a >> b;
	int temp = n;
	int counter = 0;
	int cinter = 0;

	for (int i = 1; i <= n; i++)
	{
		if (((k - a) / b) >= (n - i) and  (k - a) > b)
		{
			k = k - a;
			counter++;
		}
		else break;
	}
	int x = n - counter;
	if (counter < n)
	{
		if (k / b > x )cout << counter << "\n";
		else if (k / b == x and k % b > 0)cout << counter << "\n";
		else cout << "-1\n";
	}
	else cout << counter << "\n";

}

int32_t main()
{
	c_p_c();
	w(t)
	{
		solve();
		ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	}

}


#include<bits/stdc++.h>
using namespace std;
#define ff                first
#define ss                second
#define int              long long
#define pb                push_back
#define mp                make_pair
#define li                list<int>
#define pii               pair<int,int>
#define vit                vector<int>
#define mii               map<int ,int>
#define setbits(x)        __builtin_popcountll(x)
#define zrobits(x)        _builtin_ctzll(x)
#define mod               1000000007
#define inf               1e18
#define ln                cout<<'\n'
#define ps(x,y)           fixed<<setprecision(y)<<x
#define mk(arr,n,type)    type *arr=new type(n);
#define w(x)              int x; cin>>x; while(x--)
#define all(x)            (x).begin(), (x).end()
#define PI                3.14159265358979
#define p_sieve           100000
//const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1}, dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("g.txt", "r", stdin);
	freopen("b.txt", "w", stdout);
#endif
}

int  a[111111], b[111111], c[111111];

void solve()
{
	int k, n, a, b; cin >> k >> n >> a >> b;
	int temp = n;
	int counter = 0;
	int cinter = 0;
	if (a * n < k )
	{
		cout << n << "\n";
		return;
	}
	for (int i = 1; i <= n; i++)
	{
		if (((k - a) / b) >= (n - i) and  (k - a) > b)
		{
			k = k - a;
			counter++;
		}
		else break;
	}
	int x = n - counter;
	if (counter < n)
	{
		int temp = 0;
		while (k > b)
		{
			k = k - b;
			temp++;
		}
		if (temp >= x and k > 0)
		{
			cout << counter << "\n";
			return;
		}
		else
		{
			cout << "-1\n";
			return;
		}
	}
	if (k > 0)cout << counter << "\n";
	else cout << "-1\n";
}

int32_t main()
{
	c_p_c();
	w(t)
	{
		solve();
		ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	}

}


#include<bits/stdc++.h>
using namespace std;
#define ff                first
#define ss                second
#define int              long long
#define pb                push_back
#define mp                make_pair
#define li                list<int>
#define pii               pair<int,int>
#define vit                vector<int>
#define mii               map<int ,int>
#define setbits(x)        __builtin_popcountll(x)
#define zrobits(x)        _builtin_ctzll(x)
#define mod               1000000007
#define inf               1e18
#define ln                cout<<'\n'
#define ps(x,y)           fixed<<setprecision(y)<<x
#define mk(arr,n,type)    type *arr=new type(n);
#define w(x)              int x; cin>>x; while(x--)
#define all(x)            (x).begin(), (x).end()
#define PI                3.14159265358979
#define p_sieve           100000
//const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1}, dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("g.txt", "r", stdin);
	freopen("b.txt", "w", stdout);
#endif
}

int  a[111111], b[111111], c[111111];

void solve()
{
	int k, n, a, b; cin >> k >> n >> a >> b;
	int temp = n;
	int counter = 0;
	int cinter = 0;
	if (a * n < k and k - (a * n) >= b)
	{
		cout << n << "\n";
		return;
	}
	for (int i = 1; i <= n; i++)
	{
		if (((k - a) / b) >= (n - i) and  (k - a) > b)
		{
			k = k - a;
			counter++;
		}
		else break;
	}
	int x = n - counter;
	if (counter < n)
	{
		int temp = 0;
		while (k > b)
		{
			k = k - b;
			temp++;
		}
		if (temp >= x)cout << counter << "\n";
		else cout << "-1\n";
	}
	else cout << counter << "\n";

}

int32_t main()
{
	c_p_c();
	w(t)
	{
		solve();
		ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	}

}


#include<bits/stdc++.h>
using namespace std;
#define ff                first
#define ss                second
#define int              long long
#define pb                push_back
#define mp                make_pair
#define li                list<int>
#define pii               pair<int,int>
#define vit                vector<int>
#define mii               map<int ,int>
#define setbits(x)        __builtin_popcountll(x)
#define zrobits(x)        _builtin_ctzll(x)
#define mod               1000000007
#define inf               1e18
#define ln                cout<<'\n'
#define ps(x,y)           fixed<<setprecision(y)<<x
#define mk(arr,n,type)    type *arr=new type(n);
#define w(x)              int x; cin>>x; while(x--)
#define all(x)            (x).begin(), (x).end()
#define PI                3.14159265358979
#define p_sieve           100000
//const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1}, dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("g.txt", "r", stdin);
	freopen("b.txt", "w", stdout);
#endif
}

int  a[111111], b[111111], c[111111];

bool checker(int n, int a, int b, int k, int temp)
{
	if (n * a + (temp - n)*b < k)return true;
	else  return false;
}

void solve()
{
	int k, n, a, b; cin >> k >> n >> a >> b;
	int temp = n;
	int s = 0, e = n;
	int counter = 0;
	while (s <= e)
	{
		int mid = ((s + e) / 2);
		if (checker(mid, a, b, k, temp))
		{
			s = mid + 1;
			counter = mid;
		}
		else
		{
			e = mid - 1;
		}
	}
	if (counter == 0)
	{
		if (n * b >= k)cout << "-1\n";
		else cout << "0\n";
	}
	else cout << counter << "\n";

}

int32_t main()
{
	c_p_c();
	w(t)
	{
		solve();
		ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	}

}


#include<bits/stdc++.h>
using namespace std;
#define ff                first
#define ss                second
#define int              long long
#define pb                push_back
#define mp                make_pair
#define li                list<int>
#define pii               pair<int,int>
#define vit                vector<int>
#define mii               map<int ,int>
#define setbits(x)        __builtin_popcountll(x)
#define zrobits(x)        _builtin_ctzll(x)
#define mod               1000000007
#define inf               1e18
#define ln                cout<<'\n'
#define ps(x,y)           fixed<<setprecision(y)<<x
#define mk(arr,n,type)    type *arr=new type(n);
#define w(x)              int x; cin>>x; while(x--)
#define all(x)            (x).begin(), (x).end()
#define PI                3.14159265358979
#define p_sieve           100000
//const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1}, dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("g.txt", "r", stdin);
	freopen("b.txt", "w", stdout);
#endif
}

int  a[111111], b[111111], c[111111];

void solve()
{
	int k, n, a, b; cin >> k >> n >> a >> b;
	int temp = n;
	int counter = 0;
	int cinter = 0;
	if (a * n < k )
	{
		cout << n << "\n";
		return;
	}
	for (int i = 1; i <= n; i++)
	{
		if (((k - a) / b) >= (n - i) and  (k - a) > b)
		{
			k = k - a;
			counter++;
		}
		else break;
	}
	int x = n - counter;
	if (counter < n)
	{
		int temp = 0;
		while (k >= b)
		{
			k = k - b;
			temp++;
		}
		if (temp >= x)cout << counter << "\n";
		else cout << "-1\n";
	}
	else cout << counter << "\n";
}

int32_t main()
{
	c_p_c();
	w(t)
	{
		solve();
		ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	}

}


#include<bits/stdc++.h>
using namespace std;
#define ff                first
#define ss                second
#define int              long long
#define pb                push_back
#define mp                make_pair
#define li                list<int>
#define pii               pair<int,int>
#define vit                vector<int>
#define mii               map<int ,int>
#define setbits(x)        __builtin_popcountll(x)
#define zrobits(x)        _builtin_ctzll(x)
#define mod               1000000007
#define inf               1e18
#define ln                cout<<'\n'
#define ps(x,y)           fixed<<setprecision(y)<<x
#define mk(arr,n,type)    type *arr=new type(n);
#define w(x)              int x; cin>>x; while(x--)
#define all(x)            (x).begin(), (x).end()
#define PI                3.14159265358979
#define p_sieve           100000
//const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1}, dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("g.txt", "r", stdin);
	freopen("b.txt", "w", stdout);
#endif
}

int  a[111111], b[111111], c[111111];

void solve()
{
	int k, n, a, b; cin >> k >> n >> a >> b;
	int temp = n;
	int counter = 0;
	int cinter = 0;
	if (a * n < k )
	{
		cout << n << "\n";
		return;
	}
	for (int i = 1; i <= n; i++)
	{
		if (((k - a) / b) >= (n - i) and  (k - a) > b)
		{
			k = k - a;
			counter++;
		}
		else break;
	}
	int x = n - counter;
	if (counter < n)
	{
		int temp = 0;
		while (k > b)
		{
			k = k - b;
			temp++;
		}
		if (temp >= x)cout << counter << "\n";
		else cout << "-1\n";
	}
	else cout << counter << "\n";
}

int32_t main()
{
	c_p_c();
	w(t)
	{
		solve();
		ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	}

}


#include<bits/stdc++.h>
using namespace std;
#define ff                first
#define ss                second
#define int              long long
#define pb                push_back
#define mp                make_pair
#define li                list<int>
#define pii               pair<int,int>
#define vit                vector<int>
#define mii               map<int ,int>
#define setbits(x)        __builtin_popcountll(x)
#define zrobits(x)        _builtin_ctzll(x)
#define mod               1000000007
#define inf               1e18
#define ln                cout<<'\n'
#define ps(x,y)           fixed<<setprecision(y)<<x
#define mk(arr,n,type)    type *arr=new type(n);
#define w(x)              int x; cin>>x; while(x--)
#define all(x)            (x).begin(), (x).end()
#define PI                3.14159265358979
#define p_sieve           100000
//const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1}, dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("g.txt", "r", stdin);
	freopen("b.txt", "w", stdout);
#endif
}

int  a[111111], b[111111], c[111111];

void solve()
{
	int k, n, a, b; cin >> k >> n >> a >> b;
	int temp = n;
	int counter = 0;
	int cinter = 0;
	if (a * n < k)
	{
		cout << n << "\n";
		return;
	}
	for (int i = 1; i <= n; i++)
	{
		if (((k - a) / b) >= (n - i) and  (k - a) > b)
		{
			k = k - a;
			counter++;
		}
		else break;
	}
	int x = n - counter;
	if (counter < n)
	{
		int temp = 0;
		while (k > b)
		{
			k = k - b;
			temp++;
		}
		if (temp >= x)cout << counter << "\n";
		else cout << "-1\n";
	}
	else cout << counter << "\n";

}

int32_t main()
{
	c_p_c();
	w(t)
	{
		solve();
		ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	}

}


#include<bits/stdc++.h>
using namespace std;
#define ff                first
#define ss                second
#define int              long long
#define pb                push_back
#define mp                make_pair
#define li                list<int>
#define pii               pair<int,int>
#define vit                vector<int>
#define mii               map<int ,int>
#define setbits(x)        __builtin_popcountll(x)
#define zrobits(x)        _builtin_ctzll(x)
#define mod               1000000007
#define inf               1e18
#define ln                cout<<'\n'
#define ps(x,y)           fixed<<setprecision(y)<<x
#define mk(arr,n,type)    type *arr=new type(n);
#define w(x)              int x; cin>>x; while(x--)
#define all(x)            (x).begin(), (x).end()
#define PI                3.14159265358979
#define p_sieve           100000
//const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1}, dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("g.txt", "r", stdin);
	freopen("b.txt", "w", stdout);
#endif
}

int  a[111111], b[111111], c[111111];

void solve()
{
	int k, n, a, b; cin >> k >> n >> a >> b;
	int temp = n;
	int counter = 0;
	int cinter = 0;
	if (a * n < k)
	{
		cout << n << "\n";
		return;
	}
	for (int i = 1; i <= n; i++)
	{
		if (((k - a) / b) >= (n - i) and  (k - a) > b)
		{
			k = k - a;
			counter++;
		}
		else break;
	}
	int x = n - counter;
	if (counter < n)
	{
		if (x * b  < k)cout << counter << "\n";
		else cout << "-1\n";
	}
	else cout << counter << "\n";

}

int32_t main()
{
	c_p_c();
	w(t)
	{
		solve();
		ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	}

}


#include<bits/stdc++.h>
using namespace std;
#define ff                first
#define ss                second
#define int              long long
#define pb                push_back
#define mp                make_pair
#define li                list<int>
#define pii               pair<int,int>
#define vit                vector<int>
#define mii               map<int ,int>
#define setbits(x)        __builtin_popcountll(x)
#define zrobits(x)        _builtin_ctzll(x)
#define mod               1000000007
#define inf               1e18
#define ln                cout<<'\n'
#define ps(x,y)           fixed<<setprecision(y)<<x
#define mk(arr,n,type)    type *arr=new type(n);
#define w(x)              int x; cin>>x; while(x--)
#define all(x)            (x).begin(), (x).end()
#define PI                3.14159265358979
#define p_sieve           100000
//const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1}, dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("g.txt", "r", stdin);
	freopen("b.txt", "w", stdout);
#endif
}

int  a[111111], b[111111], c[111111];

void solve()
{
	int k, n, a, b; cin >> k >> n >> a >> b;
	int temp = n;
	int counter = 0;
	int cinter = 0;
	if (a * n < k )
	{
		cout << n << "\n";
		return;
	}
	for (int i = 1; i <= n; i++)
	{
		if (((k - a) / b) >= (n - i) and  (k - a) > b)
		{
			k = k - a;
			counter++;
		}
		else break;
	}
	int x = n - counter;
	if (counter < n)
	{
		int temp = 0;
		while (k > b)
		{
			k = k - b;
			temp++;
		}
		if (temp >= x and k > 0)cout << counter << "\n";
		else cout << "-1\n";
	}
	else cout << counter << "\n";
}

int32_t main()
{
	c_p_c();
	w(t)
	{
		solve();
		ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	}

}


#include<bits/stdc++.h>
using namespace std;
#define ff                first
#define ss                second
#define int              long long
#define pb                push_back
#define mp                make_pair
#define li                list<int>
#define pii               pair<int,int>
#define vit                vector<int>
#define mii               map<int ,int>
#define setbits(x)        __builtin_popcountll(x)
#define zrobits(x)        _builtin_ctzll(x)
#define mod               1000000007
#define inf               1e18
#define ln                cout<<'\n'
#define ps(x,y)           fixed<<setprecision(y)<<x
#define mk(arr,n,type)    type *arr=new type(n);
#define w(x)              int x; cin>>x; while(x--)
#define all(x)            (x).begin(), (x).end()
#define PI                3.14159265358979
#define p_sieve           100000
//const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1}, dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("g.txt", "r", stdin);
	freopen("b.txt", "w", stdout);
#endif
}

int  a[111111], b[111111], c[111111];

void solve()
{
	int k, n, a, b; cin >> k >> n >> a >> b;
	int temp = n;
	int counter = 0;
	int cinter = 0;
	if (a * n < k )
	{
		cout << n << "\n";
		return;
	}
	for (int i = 1; i <= n; i++)
	{
		if (((k - a) / (b * 1.0)) > (n - i) and  (k - a) > b)
		{
			k = k - a;
			counter++;
		}
		else break;
	}
	int x = n - counter;
	if (counter < n)
	{
		int temp = 0;
		while (k > b)
		{
			k = k - b;
			temp++;
		}
		if (temp >= x and k > 0)
		{
			cout << counter << "\n";
			return;
		}
		else
		{
			cout << "-1\n";
			return;
		}
	}
	if (k > 0)cout << counter << "\n";
	else cout << "-1\n";
}

int32_t main()
{
	c_p_c();
	w(t)
	{
		solve();
		ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	}

}


#include<bits/stdc++.h>
using namespace std;
#define ff                first
#define ss                second
#define int              long long
#define pb                push_back
#define mp                make_pair
#define li                list<int>
#define pii               pair<int,int>
#define vit                vector<int>
#define mii               map<int ,int>
#define setbits(x)        __builtin_popcountll(x)
#define zrobits(x)        _builtin_ctzll(x)
#define mod               1000000007
#define inf               1e18
#define ln                cout<<'\n'
#define ps(x,y)           fixed<<setprecision(y)<<x
#define mk(arr,n,type)    type *arr=new type(n);
#define w(x)              int x; cin>>x; while(x--)
#define all(x)            (x).begin(), (x).end()
#define PI                3.14159265358979
#define p_sieve           100000
//const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1}, dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

void c_p_c()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("g.txt", "r", stdin);
	freopen("b.txt", "w", stdout);
#endif
}

int  a[111111], b[111111], c[111111];

void solve()
{
	int k, n, a, b; cin >> k >> n >> a >> b;
	int temp = n;
	int counter = 0;
	int cinter = 0;

	for (int i = 1; i <= n; i++)
	{
		if (((k - a) / b) >= (n - i) and  (k - a) > b)
		{
			temp--;
			k = k - a;
			counter++;
		}
		else break;
	}
	if (temp)
	{
		int x, y;
		x = k / b;
		y = k % b;
		if (x > temp)cout << counter << "\n";
		else if (x == temp and y > 0) cout << counter << "\n";
		else
			cout << "-1\n";
	}
	else cout << counter << "\n";

}

int32_t main()
{
	c_p_c();
	w(t)
	{
		solve();
		ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	}

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int inf=0x3f3f3f3f;
const int maxn=1e5+5;

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int t;cin>>t;
    while(t--)
    {
        int sum,n,a,b;
        cin>>sum>>n>>a>>b;
        if((sum / n) < b)
        {
            cout<<-1<<endl;
        }
        else if(sum%n == 0 && sum/n == b)
        {
            cout<<-1<<endl;
        }
        else
        {
            int l = 0,r = sum/a;
            int mid;
            while(l<r)
            {
                mid = (l+r)/2;
                int now = mid*a+(n-mid)*b;
                if(now<sum) l=mid+1;
                else r=mid-1;
            }
            if(mid*a+(n-mid)*b>=sum) mid--;
            if(mid>n) mid--;
            cout<<mid<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int inf=0x3f3f3f3f;
const int maxn=1e5+5;

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int t;cin>>t;
    while(t--)
    {
        int sum,n,a,b;
        cin>>sum>>n>>a>>b;
        if((sum / n) < b)
        {
            cout<<-1<<endl;
        }
        else if(sum%n == 0 && sum/n == b)
        {
            cout<<-1<<endl;
        }
        else
        {
            int x = 0;
            int nn = n;
            while((sum / n) >= b)
            {
                x++;
                n--;
                sum -= a;
                if(x == nn) break;
            }
            if( x != nn) x--;
            if(x == nn && sum == 0) x--;
            cout<<x<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int inf=0x3f3f3f3f;
const int maxn=1e5+5;

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int t;cin>>t;
    while(t--)
    {
        int sum,n,a,b;
        cin>>sum>>n>>a>>b;
        if((sum / n) < b)
        {
            cout<<-1<<endl;
        }
        else if(sum%n == 0 && sum/n == b)
        {
            cout<<-1<<endl;
        }
        else
        {
            int x = 0;
            int nn = n;
            while((sum / n) >= b)
            {
                x++;
                n--;
                sum -= a;
                if(x == nn) break;
            }
            if(x != nn && x!=0){
                x--;
                sum+=a;
                n++;
            }
            if(x==nn&&sum==0) x--;
            else if(x!=nn&&sum%n == 0 &&sum/n == b) x--;
            cout<<x<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int inf=0x3f3f3f3f;
const int maxn=1e5+5;

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int t;cin>>t;
    while(t--)
    {
        int sum,n,a,b;
        cin>>sum>>n>>a>>b;
        if((sum / n) < b)
        {
            cout<<-1<<endl;
        }
        else if(sum%n == 0 && sum/n == b)
        {
            cout<<-1<<endl;
        }
        else
        {
            int x = 0;
            int nn = n;
            while((sum / n) >= b)
            {
                x++;
                n--;
                sum -= a;
                if(x == nn) break;
            }
            if(x != nn){
                x--;
                sum+=a;
            }
            if(sum == 0) x--;
            cout<<x<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define gc getchar_unlocked()
#define pc(x) putchar_unlocked(x)
template<typename T> void scan(T &x){x = 0;register bool _=0;register T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){register bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}

using namespace std;
const int MM = 202;

int n, k, dp[MM][MM], v[MM], sum[MM][MM];
vector<int> adj[MM];

void dfs(int cur, int pre){
    for(int u: adj[cur]){
        if(u == pre)
            continue;
        dfs(u, cur);

        for(int i = 1; i <= k; i++)
            sum[cur][i] += dp[u][i-1];
        //dis++
    }
    dp[cur][0] = v[cur] + sum[cur][k];
    //distance of k away

    for(int u: adj[cur]){
        if(u == pre)
            continue;
        //closest one

        //wait I need another loop

        for(int i = 1; i <= k; i++){

            for(int j = 0; j <= i/2; j++){
                int j2 = i-j;
                //print(cur, i, j, j2);
                dp[cur][i] = max(dp[cur][i], dp[u][j-1] + sum[cur][j2] - (j2 ? dp[u][j2-1] : 0));
            }
        }
    }
    for(int i = k-1; i >= 0; i--)
        dp[cur][i] = max(dp[cur][i], dp[cur][i+1]);
}

int main(){

    //freopen("in.txt", "r", stdin);

    scan(n, k);

    for(int i = 1; i <= n; i++)
        scan(v[i]);

    for(int i = 0,a,b; i < n-1; i++){
        scan(a, b);
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    k++;
    dfs(1, 0);
    print(dp[1][k]);

    return 0;

    for(int i = 1; i <= n; i++){
        for(int j = 0; j <= k; j++)
            printf("%d ", dp[i][j]);
        pc(10);
    }

    puts("sum");

    for(int i = 1; i <= n; i++){
        for(int j = 0; j <= k; j++)
            printf("%d ", sum[i][j]);
        pc(10);
    }

    return 0;
}

//dp[n][k]
//loop through each each child and give d
//each other gets to be at most dp[v][k-d-2]
//sample 1, should take nodes 5, 4, 2


#include <bits/stdc++.h>
#define gc getchar()
#define pc(x) putchar(x)
template<typename T> void scan(T &x){x = 0;register bool _=0;register T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){register bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}

using namespace std;
const int MM = 202;

int n, k, dp[MM][MM], v[MM], sum[MM][MM];
vector<int> adj[MM];

void dfs(int cur, int pre){
    for(int u: adj[cur]){
        if(u == pre)
            continue;
        dfs(u, cur);

        for(int i = 1; i <= k; i++)
            sum[cur][i] += dp[u][i-1];
        //dis++
    }
    dp[cur][0] = v[cur] + sum[cur][k];
    //distance of k away

    for(int u: adj[cur]){
        if(u == pre)
            continue;

        for(int i = 1; i <= k; i++){
            int j = max(k-i, i);
            dp[cur][i] = max(dp[cur][i], dp[u][i-1] + sum[cur][j] - (j ? dp[u][j-1] : 0));
        }
    }
    for(int i = k-1; i >= 0; i--)
        dp[cur][i] = max(dp[cur][i], dp[cur][i+1]);
}

int main(){

    //freopen("in.txt", "r", stdin);

    scan(n, k);

    for(int i = 1; i <= n; i++)
        scan(v[i]);

    for(int i = 0,a,b; i < n-1; i++){
        scan(a, b);
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    k++;
    dfs(1, 0);
    int ans = 0;
    for(int i = 0; i <= k; i++)
        ans = max(ans, dp[1][i]);
    print(ans);

    return 0;

    for(int i = 1; i <= n; i++){
        for(int j = 0; j <= k; j++)
            printf("%d ", dp[i][j]);
        pc(10);
    }

    puts("sum");

    for(int i = 1; i <= n; i++){
        for(int j = 0; j <= k; j++)
            printf("%d ", sum[i][j]);
        pc(10);
    }

    return 0;
}

//dp[n][k]
//loop through each each child and give d
//each other gets to be at most dp[v][k-d-2]
//sample 1, should take nodes 5, 4, 2


#include <bits/stdc++.h>
#define gc getchar()
#define pc(x) putchar(x)
template<typename T> void scan(T &x){x = 0;register bool _=0;register T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){register bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}

using namespace std;
const int MM = 202;

int n, k, dp[MM][MM], v[MM], sum[MM][MM];
vector<int> adj[MM];

void dfs(int cur, int pre){
    for(int u: adj[cur]){
        if(u == pre)
            continue;
        dfs(u, cur);

        for(int i = 1; i <= k; i++)
            sum[cur][i] += dp[u][i-1];
    }
    dp[cur][0] = v[cur] + sum[cur][k];

    for(int u: adj[cur]){
        if(u == pre)
            continue;

        for(int i = 1; i <= k; i++){
            int j = max(k-i, i);
            dp[cur][i] = max(dp[cur][i], dp[u][i-1] + sum[cur][j] - (j ? dp[u][j-1] : 0));
        }
    }
    for(int i = k-1; i >= 0; i--)
        dp[cur][i] = max(dp[cur][i], dp[cur][i+1]);
}

int main(){

    scan(n, k);

    for(int i = 1; i <= n; i++)
        scan(v[i]);

    for(int i = 0,a,b; i < n-1; i++){
        scan(a, b);
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    k++;
    dfs(1, 0);
    int ans = 0;
    for(int i = 0; i <= k; i++)
        ans = max(ans, dp[1][i]);
    print(ans);

    return 0;
}


#include <bits/stdc++.h>
#define gc getchar()
#define pc(x) putchar(x)
template<typename T> void scan(T &x){x = 0;register bool _=0;register T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){register bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}

using namespace std;
const int MM = 202;

int n, k, dp[MM][MM], v[MM], sum[MM][MM];
vector<int> adj[MM];

void dfs(int cur, int pre){
    for(int u: adj[cur]){
        if(u == pre)
            continue;
        dfs(u, cur);

        for(int i = 1; i <= k; i++)
            sum[cur][i] += dp[u][i-1];
        //dis++
    }
    dp[cur][0] = v[cur] + sum[cur][k];
    //distance of k away

    for(int u: adj[cur]){
        if(u == pre)
            continue;
        //closest one

        //wait I need another loop

        for(int i = 1; i <= k; i++){

            for(int j = 0; j <= i/2; j++){
                int j2 = i-j;
                //print(cur, i, j, j2);
                dp[cur][i] = max(dp[cur][i], dp[u][j-1] + sum[cur][j2] - (j2 ? dp[u][j2-1] : 0));
            }
        }
    }
    for(int i = k-1; i >= 0; i--)
        dp[cur][i] = max(dp[cur][i], dp[cur][i+1]);
}

int main(){

    //freopen("in.txt", "r", stdin);

    scan(n, k);

    for(int i = 1; i <= n; i++)
        scan(v[i]);

    for(int i = 0,a,b; i < n-1; i++){
        scan(a, b);
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    k++;
    dfs(1, 0);
    print(dp[1][k]);

    return 0;

    for(int i = 1; i <= n; i++){
        for(int j = 0; j <= k; j++)
            printf("%d ", dp[i][j]);
        pc(10);
    }

    puts("sum");

    for(int i = 1; i <= n; i++){
        for(int j = 0; j <= k; j++)
            printf("%d ", sum[i][j]);
        pc(10);
    }

    return 0;
}

//dp[n][k]
//loop through each each child and give d
//each other gets to be at most dp[v][k-d-2]
//sample 1, should take nodes 5, 4, 2


#include <bits/stdc++.h>
#define gc getchar()
#define pc(x) putchar(x)
template<typename T> void scan(T &x){x = 0;register bool _=0;register T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){register bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}

using namespace std;
const int MM = 201;

int n, k, dp[MM][MM], v[MM], sum[MM][MM];
vector<int> adj[MM];

void dfs(int cur, int pre){
    for(int u: adj[cur]){
        if(u == pre)
            continue;
        dfs(u, cur);

        for(int i = 1; i <= k; i++)
            sum[cur][i] += dp[u][i-1];
    }
    dp[cur][0] = v[cur] + sum[cur][k];

    for(int u: adj[cur]){
        if(u == pre)
            continue;

        for(int i = 1; i <= k; i++){
            int j = max(k-i, i);
            dp[cur][i] = max(dp[cur][i], dp[u][i-1] + sum[cur][j] - (j ? dp[u][j-1] : 0));
        }
    }
    for(int i = k-1; i >= 0; i--)
        dp[cur][i] = max(dp[cur][i], dp[cur][i+1]);
}

int main(){

    scan(n, k);

    for(int i = 1; i <= n; i++)
        scan(v[i]);

    for(int i = 0,a,b; i < n-1; i++){
        scan(a, b);
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    k++;
    dfs(1, 0);
    int ans = 0;
    for(int i = 0; i <= k; i++)
        ans = max(ans, dp[1][i]);
    print(ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); ++i)

const int N = 200 + 13;
const int INF = 1e9;

int n, k;
int a[N];
vector<int> g[N];
int dp[N][N];
int d[N];
int tmp[N];

void dfs(int v, int p = -1){
	d[v] = 1;
	dp[v][0] = a[v];
	for (auto u : g[v]) if (u != p){
		dfs(u, v);
		int nw = max(d[v], d[u] + 1);
		forn(i, nw)
			tmp[i] = -INF;
		forn(i, d[v]) for (int j = max(0, k - i); j < d[u]; ++j)
			tmp[min(i, j + 1)] = max(tmp[min(i, j + 1)], dp[v][i] + dp[u][j]);
		forn(i, d[u])
			tmp[i + 1] = max(tmp[i + 1], dp[u][i]);
		forn(i, d[v])
			dp[v][i] = max(dp[v][i], tmp[i]);
		for (int i = d[v]; i < nw; ++i)
			dp[v][i] = tmp[i];
		d[v] = nw;
		for (int i = d[v] - 1; i > 0; --i)
			dp[v][i - 1] = max(dp[v][i - 1], dp[v][i]);
	}
}

int main(){
	scanf("%d%d", &n, &k);
	forn(i, n)
		scanf("%d", &a[i]);
	forn(i, n - 1){
		int v, u;
		scanf("%d%d", &v, &u);
		--v, --u;
		g[v].push_back(u);
		g[u].push_back(v);
	}
	dfs(0);
	printf("%d\n", dp[0][0]);
}


#include <bits/stdc++.h>

using namespace std;

const int N = 210;

int n, k;
vector<int> a;
vector<vector<int>> g, dp;

void dfs(int v, int p) {
	dp[v][0] = a[v];
	for (auto to : g[v]) {
		if (to != p) dfs(to, v);
	}
	for (int dep = 0; dep < N; ++dep) {
		if (dep == 0) {
			for (auto to : g[v]) {
				if (to == p) continue;
				dp[v][dep] += dp[to][max(0, k - dep - 1)];
			}
		} else {
			for (auto to : g[v]) {
				if (to == p) continue;
				int cur = dp[to][dep - 1];
				for (auto other : g[v]) {
					if (other == p || other == to) continue;
					cur += dp[other][max(dep - 1, k - dep - 1)];
				}
				dp[v][dep] = max(dp[v][dep], cur);
			}
		}
	}
	for (int dep = N - 1; dep > 0; --dep) {
		dp[v][dep - 1] = max(dp[v][dep - 1], dp[v][dep]);
	}
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

	cin >> n >> k;
	++k;
	a = vector<int>(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	g = vector<vector<int>>(n);
	for (int i = 0; i < n - 1; ++i) {
		int x, y;
		cin >> x >> y;
		--x, --y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	dp = vector<vector<int>>(n, vector<int>(N));
	dfs(0, -1);
	cout << dp[0][0] << endl;

	return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

int n, k;
vector<vector<int>> g;
vector<vector<ll>> dp;
vector<ll> a;
void dfs(int cur, int pre) {
	dp[cur][0] = a[cur];
	for (auto next : g[cur]) {
		if (next == pre) continue;
		dfs(next, cur);
		if (dp[next][k] == -1) continue;
		dp[cur][0] += dp[next][k];
	}
	vector<ll> sum(n, 0);
	for (auto next : g[cur]) {
		if (next == pre) continue;
		for (int dep = 0; dep < n; dep++) {
			if (dp[next][dep] != -1) sum[dep] += dp[next][dep];
		}
	}
	for (int dep = 1; dep < n; dep++) {
		for (auto next : g[cur]) {
			if (next == pre) continue;
			if (dp[next][dep - 1] == -1) continue;
			ll tmp = dp[next][dep - 1] + sum[max(dep - 1, k - dep)];
			if (dp[next][max(dep - 1, k - dep)] != -1) tmp -= dp[next][max(dep - 1, k - dep)];
			chmax(dp[cur][dep], tmp);
		}
	}
	for (int dep = n - 2; dep >= 0; dep--) {
		chmax(dp[cur][dep], dp[cur][dep + 1]);
	}
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	scanf("%d %d", &n, &k);
	dp = vector<vector<ll>>(n, vector<ll>(n, -1));
	a.resize(n); for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
	g.resize(n);
	for (int i = 0; i < n - 1; i++) {
		int u, v; scanf("%d %d", &u, &v);
		u--; v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(0, -1);

	cout << dp[0][0] << endl;
	return 0;

}


#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

int n, k;
vector<vector<int>> g;
vector<vector<ll>> dp;
vector<ll> a;
void dfs(int cur, int pre) {
	dp[cur][0] = a[cur];
	for (auto next : g[cur]) {
		if (next == pre) continue;
		dfs(next, cur);
		if (dp[next][k] == -1) continue;
		dp[cur][0] += dp[next][k];
	}
	vector<ll> sum(n, 0);
	for (auto next : g[cur]) {
		if (next == pre) continue;
		for (int dep = 0; dep < n; dep++) {
			if (dp[next][dep] != -1) sum[dep] += dp[next][dep];
		}
	}
	for (int dep = 1; dep < n; dep++) {
		for (auto next : g[cur]) {
			if (next == pre) continue;
			if (dp[next][dep - 1] == -1) continue;
			ll tmp = dp[next][dep - 1] + sum[max(dep - 1, k - dep)];
			if (dp[next][max(dep - 1, k - dep)] != -1) tmp -= dp[next][max(dep - 1, k - dep)];
			chmax(dp[cur][dep], tmp);
		}
	}
	for (int dep = n - 2; dep >= 0; dep--) {
		chmax(dp[cur][dep], dp[cur][dep + 1]);
	}
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	scanf("%d %d", &n, &k);
	dp = vector<vector<ll>>(n, vector<ll>(n, -1));
	a.resize(n); for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
	g.resize(n);
	if (k > n - 1) {
		cout << *max_element(a.begin(), a.end()) << endl;
		return 0;
	}
	for (int i = 0; i < n - 1; i++) {
		int u, v; scanf("%d %d", &u, &v);
		u--; v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(0, -1);

	cout << dp[0][0] << endl;
	return 0;

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define mod 1000000007
#define lli long long
#define fi first
#define se second
#define pb emplace_back
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORR(x,v) for(auto x : v)
#define sz(a) (int)(a.size())
#define MAXC 205
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
// %

vector< int > ed[MAXC];
int dp[MAXC][MAXC], W[MAXC], n, k;

int solve(int u, int dep, int p=-1){
    if(dp[u][dep]!=-1){
        return dp[u][dep];
    }
    if(dep >= n){
        return dp[u][dep] = 0;
    }
    if(!dep){
        dp[u][dep] = W[u];
        FORR(v, ed[u]){
            if(v!=p){
                dp[u][dep] += solve(v, k, u);
            }
        }
    }
    else{
        int sum = 0;
        dp[u][dep] = 0;
        FORR(v, ed[u]){
            if(v!=p){
                sum += solve(v, max(k-dep, dep-1) , u);
            }
        }
        FORR(v, ed[u]){
            if(v!=p){
                dp[u][dep] = max(dp[u][dep], sum - solve(v, max(k-dep, dep-1), u) + solve(v, dep-1, u));
            }
        }
    }
    return dp[u][dep] = max(dp[u][dep], solve(u, dep+1, p));
}

int main(int argc, char **argv)
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    memset(dp, 0xff, sizeof dp);
    FOR(i, n){
        cin >> W[i+1];
    }
    FOR(i, n-1){
        int a, b;
        cin >> a >> b;
        ed[a].pb(b);
        ed[b].pb(a);
    }
    cout<< solve(1, 0) << "\n";
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define mod 1000000007
#define lli long long
#define fi first
#define se second
#define pb emplace_back
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORR(x,v) for(auto x : v)
#define sz(a) (int)(a.size())
#define MAXC 205
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
// %

vector< int > ed[MAXC];
int dp[MAXC][MAXC], W[MAXC], n, k;
int H[MAXC];

int solve(int u, int dep, int p=-1){
    if(dp[u][dep]!=-1){
        return dp[u][dep];
    }
    if(dep >= n){
        return dp[u][dep] = 0;
    }
    if(!dep){
        dp[u][dep] = W[u];
        FORR(v, ed[u]){
            if(v!=p){
                dp[u][dep] += solve(v, k, u);
            }
        }
    }
    else{
        int sum = 0;
        FORR(v, ed[u]){
            if(v!=p){
                sum += solve(v, max(k-dep, dep-1) , u);
            }
        }
        FORR(v, ed[u]){
            if(v!=p){
                dp[u][dep] = max(dp[u][dep], sum - solve(v, max(k-dep, dep-1), u) + solve(v, dep-1, u));
            }
        }
    }
    return dp[u][dep] = max(dp[u][dep], solve(u, dep+1, p));
}

int main(int argc, char **argv)
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    memset(dp, 0xff, sizeof dp);
    FOR(i, n){
        cin >> W[i+1];
    }
    FOR(i, n-1){
        int a, b;
        cin >> a >> b;
        ed[a].pb(b);
        ed[b].pb(a);
    }
    cout<< solve(1, 0) << "\n";
}


#include <bits/stdc++.h>
using namespace std;

template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
    return os << "(" << P.first << "," << P.second << ")"; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
    os << "[ "; for(auto v : V) os << v << " "; return os << "]"; }
template<class T> ostream& operator<<(ostream &os, set<T> S){
    os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}"; }
template<class T> ostream& operator<<(ostream &os, multiset<T> S){
    os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}"; }
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {
    os << "{ "; for(auto m:M) os<<"("<<m.F<<":"<<m.S<<") "; return os<<"}"; }
    #define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');
	cout.write(names, comma - names) << " : " << arg1 << " |"; __f(comma + 1, args...); }
#define rng(x) x.begin(), x.end()
#define pb push_back
#define F first
#define S second
#define el '\n'

typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<bool> vb;

/*-----------------------------Code begins----------------------------------*/

const int N = 202;
vi w(N), adj[N];
vvi DP(N, vi(N));
int n, k;

void dfs(int node = 0, int par = -1){
	vi sum(N);
	for(int child : adj[node]){
		if(child == par) continue;
		dfs(child, node);
		for (int i = 0; i < N; ++i){
			sum[i] += DP[child][i];
		}
	}
	DP[node][0] = w[node] + sum[k];
	for(int child : adj[node]){
		if(child == par) continue;
		for (int x = 0; x <= (k-1)/2; ++x){
			int y = k + x - 1;
			DP[node][x+1] = max(DP[node][x+1], DP[child][x] + sum[y] - DP[child][y]);
		}
		for (int x = (k-1)/2 + 1; x < N-1; ++x){
			DP[node][x+1] = sum[x];
		}
	}
	for (int i = N-2; i >= 0; --i){
		DP[node][i] = max(DP[node][i], DP[node][i+1]);
	}
}

void solve(){
	cin>>n>>k;
	for (int i = 0; i < n; ++i){
		cin>>w[i];
	}
	for (int i = 0; i < n-1; ++i){
		int u, v; cin>>u>>v;
		u--; v--;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs();
	cout<<DP[0][0]<<el;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	int T=1, tc = 1;
	// cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
    return os << "(" << P.first << "," << P.second << ")"; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
    os << "[ "; for(auto v : V) os << v << " "; return os << "]"; }
template<class T> ostream& operator<<(ostream &os, set<T> S){
    os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}"; }
template<class T> ostream& operator<<(ostream &os, multiset<T> S){
    os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}"; }
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {
    os << "{ "; for(auto m:M) os<<"("<<m.F<<":"<<m.S<<") "; return os<<"}"; }
    #define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');
	cout.write(names, comma - names) << " : " << arg1 << " |"; __f(comma + 1, args...); }
#define rng(x) x.begin(), x.end()
#define pb push_back
#define F first
#define S second
#define el '\n'

typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<bool> vb;

/*-----------------------------Code begins----------------------------------*/

const int N = 202;
vi w(N), adj[N];
vvi DP(N, vi(N));
int n, k;

void dfs(int node = 0, int par = -1){
	vi sum(N);
	for(int child : adj[node]){
		if(child == par) continue;
		dfs(child, node);
		for (int i = 0; i < N; ++i){
			sum[i] += DP[child][i];
		}
	}
	DP[node][0] = w[node] + sum[k];
	for(int child : adj[node]){
		if(child == par) continue;
		for (int d = 1; d <= k; ++d){
			int y = max(k+1-d - 1, d-1);
			DP[node][d] = max(DP[node][d], DP[child][d-1] + sum[y] - DP[child][y]);
		}
	}
	for (int i = N-2; i >= 0; --i){
		DP[node][i] = max(DP[node][i], DP[node][i+1]);
	}
}

void solve(){
	cin>>n>>k;
	for (int i = 0; i < n; ++i){
		cin>>w[i];
	}
	for (int i = 0; i < n-1; ++i){
		int u, v; cin>>u>>v;
		u--; v--;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs();
	cout<<DP[0][0]<<el;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	int T=1, tc = 1;
	// cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
    return os << "(" << P.first << "," << P.second << ")"; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
    os << "[ "; for(auto v : V) os << v << " "; return os << "]"; }
template<class T> ostream& operator<<(ostream &os, set<T> S){
    os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}"; }
template<class T> ostream& operator<<(ostream &os, multiset<T> S){
    os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}"; }
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {
    os << "{ "; for(auto m:M) os<<"("<<m.F<<":"<<m.S<<") "; return os<<"}"; }
    #define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');
	cout.write(names, comma - names) << " : " << arg1 << " |"; __f(comma + 1, args...); }
#define rng(x) x.begin(), x.end()
#define pb push_back
#define F first
#define S second
#define el '\n'

typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<bool> vb;

/*-----------------------------Code begins----------------------------------*/

const int N = 7;
vi w(N), adj[N];
vvi DP(N, vi(N));
int n, k;

void dfs(int node = 0, int par = -1){
	vi sum(N);
	for(int child : adj[node]){
		if(child == par) continue;
		dfs(child, node);
		for (int i = 0; i < N; ++i){
			sum[i] += DP[child][i];
		}
	}
	DP[node][0] = w[node] + sum[k];
	for(int child : adj[node]){
		if(child == par) continue;
		for (int x = 0; x <= (k-1)/2; ++x){
			int y = k + x - 1;
			DP[node][x+1] = max(DP[node][x + 1], DP[child][x] + sum[y] - DP[child][y]);
		}
		for (int x = (k-1)/2 + 1; x < k; ++x){
			DP[node][x+1] = sum[x];
		}
	}
	for (int i = N-2; i >= 0; --i){
		DP[node][i] = max(DP[node][i], DP[node][i+1]);
	}
}

void solve(){
	cin>>n>>k;
	for (int i = 0; i < n; ++i){
		cin>>w[i];
	}
	for (int i = 0; i < n-1; ++i){
		int u, v; cin>>u>>v;
		u--; v--;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs();
	cout<<DP[0][0]<<el;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	int T=1, tc = 1;
	// cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
    return os << "(" << P.first << "," << P.second << ")"; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
    os << "[ "; for(auto v : V) os << v << " "; return os << "]"; }
template<class T> ostream& operator<<(ostream &os, set<T> S){
    os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}"; }
template<class T> ostream& operator<<(ostream &os, multiset<T> S){
    os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}"; }
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {
    os << "{ "; for(auto m:M) os<<"("<<m.F<<":"<<m.S<<") "; return os<<"}"; }
    #define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');
	cout.write(names, comma - names) << " : " << arg1 << " |"; __f(comma + 1, args...); }
#define rng(x) x.begin(), x.end()
#define pb push_back
#define F first
#define S second
#define el '\n'

typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<bool> vb;

/*-----------------------------Code begins----------------------------------*/

const int N = 202;
vi w(N), adj[N];
vvi DP(N, vi(N));
int n, k;

void dfs(int node = 0, int par = -1){
	vi sum(N);
	for(int child : adj[node]){
		if(child == par) continue;
		dfs(child, node);
		for (int i = 0; i < N; ++i){
			sum[i] += DP[child][i];
		}
	}
	DP[node][0] = w[node] + sum[k];
	for(int child : adj[node]){
		if(child == par) continue;
		for (int x = 0; x <= (k-1)/2; ++x){
			int y = k + x - 1;
			DP[node][x+1] = max(DP[node][x + 1], DP[child][x] + sum[y] - DP[child][y]);
		}
		for (int x = (k-1)/2 + 1; x < k; ++x){
			DP[node][x+1] = sum[x];
		}
	}
	for (int i = N-2; i >= 0; --i){
		DP[node][i] = max(DP[node][i], DP[node][i+1]);
	}
}

void solve(){
	cin>>n>>k;
	for (int i = 0; i < n; ++i){
		cin>>w[i];
	}
	for (int i = 0; i < n-1; ++i){
		int u, v; cin>>u>>v;
		u--; v--;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs();
	cout<<DP[0][0]<<el;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	int T=1, tc = 1;
	// cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
    return os << "(" << P.first << "," << P.second << ")"; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
    os << "[ "; for(auto v : V) os << v << " "; return os << "]"; }
template<class T> ostream& operator<<(ostream &os, set<T> S){
    os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}"; }
template<class T> ostream& operator<<(ostream &os, multiset<T> S){
    os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}"; }
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {
    os << "{ "; for(auto m:M) os<<"("<<m.F<<":"<<m.S<<") "; return os<<"}"; }
    #define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');
	cout.write(names, comma - names) << " : " << arg1 << " |"; __f(comma + 1, args...); }
#define rng(x) x.begin(), x.end()
#define pb push_back
#define F first
#define S second
#define el '\n'

typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<bool> vb;

/*-----------------------------Code begins----------------------------------*/

const int N = 202;
vi w(N), adj[N];
vvi DP(N, vi(N));
int n, k;

void dfs(int node = 0, int par = -1){
	vi sum(N);
	for(int child : adj[node]){
		if(child == par) continue;
		dfs(child, node);
		for (int i = 0; i < N; ++i){
			sum[i] += DP[child][i];
		}
	}
	DP[node][0] = w[node] + sum[k];
	for(int child : adj[node]){
		if(child == par) continue;
		for (int x = 0; x <= (k-1)/2; ++x){
			int y = k-1 - x;
			DP[node][x+1] = max(DP[node][x+1], DP[child][x] + sum[y] - DP[child][y]);
		}
		for (int x = (k-1)/2 + 1; x < N-1; ++x){
			DP[node][x+1] = sum[x];
		}
	}
	for (int i = N-2; i >= 0; --i){
		DP[node][i] = max(DP[node][i], DP[node][i+1]);
	}
}

void solve(){
	cin>>n>>k;
	for (int i = 0; i < n; ++i){
		cin>>w[i];
	}
	for (int i = 0; i < n-1; ++i){
		int u, v; cin>>u>>v;
		u--; v--;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs();
	cout<<DP[0][0]<<el;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	int T=1, tc = 1;
	// cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<iomanip>
#define BUG puts("no bug")
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define ll long long
#define ull unsigned long long
#define REP(a,b,c) for(int a=b;a<=c;a++)
#define PER(a,b,c) for(int a=b;a>=c;a--)
#define lb(a) (a&(-a))
#define pp (p<<1)
#define ppp (p<<1|1)
#define pi pair<int,int>
#define pii pair<pair<int,int>,int>
using namespace std;
const double Pi=acos(-1.0);
const int MAXN=4e2+5;
int n,m,t;
int wei[MAXN];
int total=0;
int du[MAXN],vis[MAXN];
int deep[MAXN],res[MAXN];
int head[MAXN],nxt[MAXN],len[MAXN],to[MAXN];
void add(ll a,ll b,ll c=1){
    nxt[++total]=head[a];
    to[total]=b;
    len[total]=c;
    head[a]=total;
}

struct NODE {
    int index,dep;
};

int cmp(int a,int b){
    return deep[a]>deep[b];
}

void dfs(int u,int f){
    deep[u]=deep[f]+1;
    for(int i=head[u];i;i=nxt[i]){
        int v=to[i];
        if(v!=f){
            dfs(v,u);
        }
    }
}

int bfs(int x){
    if(wei[x]<=0)return 0;
    memset(vis,0, sizeof(vis));
    queue<NODE>qu;
    qu.push({x,0});
    while(!qu.empty()){
        NODE u=qu.front();
        vis[u.index]=1;
        qu.pop();
        for(int i=head[u.index];i;i=nxt[i]){
            int v=to[i];
            if(!vis[v]&&u.dep<m){
                qu.push({v,u.dep+1});
                wei[v]-=wei[x];
            }
        }
    }
    return wei[x];
}

int main(){
    cin>>n>>m;
    REP(i,1,n)cin>>wei[i],res[i]=i;
    REP(i,1,n-1){
        int a,b;
        cin>>a>>b;
        add(a,b);add(b,a);
        du[a]++,du[b]++;
    }
    dfs(1,0);
    int ans=0;
    sort(res+1,res+1+n,cmp);
    REP(i,1,n)ans+=bfs(res[i]);
    cout<<ans;
}


#include<iostream>
#include<vector>
#include<map>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<iomanip>
#define BUG puts("no bug")
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define ll long long
#define ull unsigned long long
#define REP(a,b,c) for(int a=b;a<=c;a++)
#define PER(a,b,c) for(int a=b;a>=c;a--)
#define lb(a) (a&(-a))
#define pp (p<<1)
#define ppp (p<<1|1)
#define pi pair<int,int>
#define pii pair<pair<int,int>,int>
using namespace std;
const double Pi=acos(-1.0);
const int MAXN=4e2+5;
int n,m,t;
int wei[MAXN];
int total=0;
int du[MAXN],vis[MAXN];
int head[MAXN],nxt[MAXN],len[MAXN],to[MAXN];
void add(ll a,ll b,ll c=1){
    nxt[++total]=head[a];
    to[total]=b;
    len[total]=c;
    head[a]=total;
}

struct NODE {
    int index,dep;
};

int bfs(int x){
    if(wei[x]<=0)return 0;
    memset(vis,0, sizeof(vis));
    queue<NODE>qu;
    qu.push({x,0});
    while(!qu.empty()){
        NODE u=qu.front();
        vis[u.index]=1;
        qu.pop();
        for(int i=head[u.index];i;i=nxt[i]){
            int v=to[i];
            if(!vis[v]&&u.dep<m){
                qu.push({v,u.dep+1});
                wei[v]-=wei[x];
            }
        }
    }
    return wei[x];
}

int main(){
    cin>>n>>m;
    REP(i,1,n)cin>>wei[i];
    REP(i,1,n-1){
        int a,b;
        cin>>a>>b;
        add(a,b);add(b,a);
        du[a]++,du[b]++;
    }
    int ans=0;
    queue<int>qu;
    REP(i,1,n)if(du[i]==1)qu.push(i);
    while(!qu.empty()){
        ans+=bfs(qu.front());
//        cout<<qu.front()<<endl;
        for(int i=head[qu.front()];i;i=nxt[i]){
            int v=to[i];
            du[v]--;
//            cout<<du[v]<<endl;
            if(du[v]==1)qu.push(v);
        }
        qu.pop();
    }
    cout<<ans;
}


#include<iostream>
#include<vector>
#include<map>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<iomanip>
#define BUG puts("no bug")
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define ll long long
#define ull unsigned long long
#define REP(a,b,c) for(int a=b;a<=c;a++)
#define PER(a,b,c) for(int a=b;a>=c;a--)
#define lb(a) (a&(-a))
#define pp (p<<1)
#define ppp (p<<1|1)
#define pi pair<int,int>
#define pii pair<pair<int,int>,int>
using namespace std;
const double Pi=acos(-1.0);
const int MAXN=2e2+5;
int n,m,t;
int wei[MAXN];
int total=0;
int du[MAXN],vis[MAXN];
int head[MAXN],nxt[MAXN],len[MAXN],to[MAXN];
void add(ll a,ll b,ll c=1){
    nxt[++total]=head[a];
    to[total]=b;
    len[total]=c;
    head[a]=total;
}

struct NODE {
    int index,dep;
};

int bfs(int x){
    if(wei[x]<=0)return 0;
    memset(vis,0, sizeof(vis));
    queue<NODE>qu;
    qu.push({x,0});
    while(!qu.empty()){
        NODE u=qu.front();
        vis[u.index]=1;
        qu.pop();
        for(int i=head[u.index];i;i=nxt[i]){
            int v=to[i];
            if(!vis[v]&&u.dep<m){
                qu.push({v,u.dep+1});
                wei[v]-=wei[x];
            }
        }
    }
    return wei[x];
}

int main(){
    cin>>n>>m;
    REP(i,1,n)cin>>wei[i];
    REP(i,1,n-1){
        int a,b;
        cin>>a>>b;
        add(a,b);add(b,a);
        du[a]++,du[b]++;
    }
    int ans=0;
    queue<int>qu;
    REP(i,1,n)if(du[i]==1)qu.push(i);
    while(!qu.empty()){
        ans+=bfs(qu.front());
//        cout<<qu.front()<<endl;
        for(int i=head[qu.front()];i;i=nxt[i]){
            int v=to[i];
            du[v]--;
//            cout<<du[v]<<endl;
            if(du[v]==1)qu.push(v);
        }
        qu.pop();
    }
    cout<<ans;
}


#include<iostream>
#include<vector>
#include<map>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<iomanip>
#define BUG puts("no bug")
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define ll long long
#define ull unsigned long long
#define REP(a,b,c) for(int a=b;a<=c;a++)
#define PER(a,b,c) for(int a=b;a>=c;a--)
#define lb(a) (a&(-a))
#define pp (p<<1)
#define ppp (p<<1|1)
#define pi pair<int,int>
#define pii pair<pair<int,int>,int>
using namespace std;
const double Pi=acos(-1.0);
const int MAXN=4e2+5;
int n,m,t;
int wei[MAXN];
int total=0;
int du[MAXN],vis[MAXN];
int head[MAXN],nxt[MAXN],len[MAXN],to[MAXN];
void add(ll a,ll b,ll c=1){
    nxt[++total]=head[a];
    to[total]=b;
    len[total]=c;
    head[a]=total;
}

struct NODE {
    int index,dep;
};

int bfs(int x){
    if(wei[x]<=0)return 0;
    memset(vis,0, sizeof(vis));
    queue<NODE>qu;
    qu.push({x,0});
    int tmp=wei[x];
    while(!qu.empty()){
        NODE u=qu.front();
        vis[u.index]=1;
        wei[u.index]-=tmp;
        qu.pop();
        if(u.dep!=m)
        for(int i=head[u.index];i;i=nxt[i]){
            int v=to[i];
            if(!vis[v]){
                qu.push({v,u.dep+1});
//                wei[v]-=wei[x];
            }
        }
    }
    return tmp;
}

int main(){
    cin>>n>>m;
    REP(i,1,n)cin>>wei[i];
    REP(i,1,n-1){
        int a,b;
        cin>>a>>b;
        add(a,b);add(b,a);
        du[a]++,du[b]++;
    }
    int ans=0;
    queue<int>qu;
    REP(i,1,n)if(du[i]==1)qu.push(i);
    while(!qu.empty()){
        ans+=bfs(qu.front());
//        cout<<qu.front()<<endl;
        for(int i=head[qu.front()];i;i=nxt[i]){
            int v=to[i];
            du[v]--;
//            cout<<du[v]<<endl;
            if(du[v]==1)qu.push(v);
        }
        qu.pop();
    }
    cout<<ans;
}


#include <bits/stdc++.h>

#define FILE
#define fr first
#define se second

using namespace std;

const long long N = 2e2 + 7;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;

int n;
int k;
int x;
int y;
int a[N];
int d[N][N];
vector<int> g[N];
void dfs(int v, int p){
    d[v][0] = a[v];
    for(auto to: g[v]){
        if(to == p){
            continue;
        }
        dfs(to, v);
    }
    for(int dep = 0; dep < N; dep ++){
        if(dep == 0){
            for(auto to: g[v]){
                if(to == p){
                    continue;
                }
                d[v][dep] += d[to][max(0, k - dep - 1)];
            }
        }
        else{
            for(auto to: g[v]){
                if(to == p){
                    continue;
                }
                int cur = d[to][dep - 1];
                for(auto other: g[v]){
                    if(other == to || other == p){
                        continue;
                    }
                    cur += d[other][max(dep - 1, k - dep - 1)];
                }
                d[v][dep] = max(d[v][dep], cur);
            }
        }
    }
    for(int dep = N - 1; dep >= 0; dep --){
        d[v][dep - 1] = max(d[v][dep - 1], d[v][dep]);
    }
}
int main()
{
    #ifdef FILEs
        freopen("input.txt", "r", stdin);
        /// freopen("output.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);

    cin >> n >> k; k ++;
    for(int i = 1; i <= n; i ++){
        cin >> a[i];
    }
    for(int i = 1; i < n; i ++){
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1, 0);
    cout << d[1][0] << "\n";
}


#include <bits/stdc++.h>

#define FILE
#define fr first
#define se second

using namespace std;

const long long N = 2e2 + 7;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;

int n;
int k;
int x;
int y;
int a[N];
int d[N][N];
vector<int> g[N];
void dfs(int v, int p){
    d[v][0] = a[v];
    for(auto to: g[v]){
        if(to == p){
            continue;
        }
        dfs(to, v);
    }
    for(int dep = 0; dep < N; dep ++){
        if(dep == 0){
            for(auto to: g[v]){
                if(to == p){
                    continue;
                }
                d[v][dep] += d[to][max(0, k - dep - 1)];
            }
        }
        else{
            for(auto to: g[v]){
                if(to == p){
                    continue;
                }
                int cur = d[to][dep - 1];
                for(auto other: g[v]){
                    if(other == to || other == p){
                        continue;
                    }
                    cur += d[other][max(dep - 1, k - dep - 1)];
                }
                d[v][dep] = max(d[v][dep], cur);
            }
        }
    }
    for(int dep = N - 1; dep > 0; dep --){
        d[v][dep - 1] = max(d[v][dep - 1], d[v][dep]);
    }
}
int main()
{
    #ifdef FILEs
        freopen("input.txt", "r", stdin);
        /// freopen("output.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);

    cin >> n >> k; k ++;
    for(int i = 1; i <= n; i ++){
        cin >> a[i];
    }
    for(int i = 1; i < n; i ++){
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1, 0);
    cout << d[1][0] << "\n";
}


#include <bits/stdc++.h>

#define FILE
#define fr first
#define se second

using namespace std;

const long long M = 21;
const long long N = 2e5 + 7;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;

int n;
int k;
int x;
int y;
int t;
int c[N];
int a[N];
int in[N];
int out[N];
int d[N][M];
int depth[N];
vector<int> g[N];
void dfs(int v, int p){
    d[v][0] = p;
    in[v] = ++ t;
    depth[v] = depth[p] + 1;
    for(int i = 1; i < M; i ++){
        d[v][i] = d[d[v][i - 1]][i - 1];
    }
    for(auto to: g[v]){
        if(to == p){
            continue;
        }
        dfs(to, v);
    }
    out[v] = t;
}
int can(int a, int b){
    return (in[a] <= in[b] && out[a] >= out[b]);
}
int lca(int a, int b){
    if(can(a, b)){
        return a;
    }
    if(can(b, a)){
        return b;
    }
    for(int i = M - 1; i >= 0; i --){
        if(!can(d[a][i], b)){
            a = d[a][i];
        }
    }
    return d[a][0];
}
int dist(int a, int b){
    return depth[a] + depth[b] - 2 * depth[lca(a, b)];
}
int main()
{
    #ifdef FILEs
        freopen("input.txt", "r", stdin);
        /// freopen("output.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);

    cin >> n >> k;
    for(int i = 1; i <= n; i ++){
        cin >> a[i];
    }
    iota(c + 1, c + n + 1, 1);
    sort(c + 1, c + n + 1, [](int x, int y){
        return (c[a[x]] > c[a[y]]);
    });
    for(int i = 1; i < n; i ++){
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    int res = 0;
    for(int i = 1; i <= n; i ++){
        set<int> cur = {c[i]};
        dfs(i, i);
        int good = 1;
        while(good == 1){
            good = 0;
            int idx = 0;
            for(int j = 1; j <= n && idx == 0; j ++){
                if(cur.count(c[j]) == 0){
                    int good1 = 1;
                    for(auto x: cur){
                        good1 &= (dist(x, c[j]) > k);
                    }
                    if(good1 == 1){
                        idx = c[j];
                        break;
                    }
                }
            }
            if(idx > 0){
                good = 1;
                cur.insert(idx);
            }
        }
        int ans = 0;
        for(auto x: cur){
            ans += a[x];
        }
        t = 0;
        res = max(res, ans);
        memset(d, 0, sizeof d);
        memset(in, 0, sizeof in);
        memset(out, 0, sizeof out);
        memset(depth, 0, sizeof depth);
    }
    cout << res << "\n";
}


#include <bits/stdc++.h>
using namespace std;
int a[201], p[201], s[201][201], k;
vector<int> g[201];

int sol(int n, int d){
	if(s[n][d]) return s[n][d];
	for(int i=0;i<g[n].size();++i){
		if(g[n][i] != p[n])
			p[g[n][i]] = n;
	}
	int r=0,tmp;
	if(d == 0){
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				for(int dis = k-1;dis>-1;--dis){
					tmp = sol(g[n][i], dis);
					for(int j=0;j<g[n].size();++j){
						if(i!=j && g[n][j] != p[n]) tmp+=sol(g[n][j],max(k-1-dis,dis));
					}
					r = max(r,tmp);
				}
			}
		}

		tmp = a[n];
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				tmp += sol(g[n][i],k);
			}
		}
		r = max(r,tmp);
	}
	else{
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				for(int dis = k-1;dis>-1;--dis){
					tmp = sol(g[n][i], max(d-1,dis));
					for(int j=0;j<g[n].size();++j){
						if(i!=j && g[n][j] != p[n]) tmp+=sol(g[n][j],max(k-1-dis,d-1));
					}
					r = max(r,tmp);
				}
			}
		}
	}

	s[n][d] = r;
	return s[n][d];
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n>>k;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	cout<<sol(1,0);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[201], p[201], s[201][201], k;
vector<int> g[201];

int sol(int n, int d){
	if(s[n][d]!=-1) return s[n][d];

	for(int i=0;i<g[n].size();++i){
		if(g[n][i] != p[n])
			p[g[n][i]] = n;
	}

	int r=0,tmp;
	if(d == 0){
		r = a[n];
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				r += sol(g[n][i],k);
			}
		}
	}
	else{
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				tmp = sol(g[n][i], d-1);
				for(int j=0;j<g[n].size();++j){
					if(i!=j && g[n][j] != p[n]) tmp+=sol(g[n][j],max(k-d,d-1));
				}
				r = max(r,tmp);
			}
		}
	}

	s[n][d] = r;
	s[n][d] = max(s[n][d], sol(n,min(k,d+1)));
	return s[n][d];
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n>>k;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}

	for(int i=1;i<n;++i){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	for(int i=1;i<=n;++i){
		for(int j=0;j<=k;++j){
			s[i][j] = -1;
		}
	}
	cout<<sol(1,0);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[201], p[201], s[201][201], k;
vector<int> g[201];

int sol(int n, int d){
	if(s[n][d]) return s[n][d];
	for(int i=0;i<g[n].size();++i){
		if(g[n][i] != p[n])
			p[g[n][i]] = n;
	}
	int r=0,tmp;
	if(d == 0){
		r = a[n];
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				r += sol(g[n][i],k);
			}
		}
	}
	else{
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				tmp = sol(g[n][i], d-1);
				for(int j=0;j<g[n].size();++j){
					if(i!=j && g[n][j] != p[n]) tmp+=sol(g[n][j],max(k-d,d-1));
				}
				r = max(r,tmp);
			}
		}
	}

	s[n][d] = r;
	for(int i=k-1;i>d;--i){
		s[n][i-1] = max(s[n][i-1],sol(n,i));
	}
	return s[n][d];
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n>>k;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	cout<<sol(1,0);
    return 0;
}