#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 2050
#define M 1000000
#define ll long long
int li[N],n,m;ll ans0,TL[N],TS[N],SS[N],SL[N],P[N],cnt[N];
double p[N],Exp[N],ans1;
inline int Read()
 {
    int x=0;char y;
    do y=getchar(); while (y<'0'||y>'9');
    do x=x*10+y-'0',y=getchar(); while (y>='0'&&y<='9');
    return x;
 }
inline bool cmp(int x,int y)
 {return P[x]*TL[x]*(M-P[y])<P[y]*TL[y]*(M-P[x]);}
inline void Update(int x,ll y,double z)
 {if (y>cnt[x]||(y==cnt[x]&&z<Exp[x])) cnt[x]=y,Exp[x]=z;}
int main()
 {
    n=Read();m=Read();
    for (int i=1;i<=n;i++)
      SS[i]=Read(),SL[i]=Read(),TS[i]=Read(),TL[i]=Read(),
      scanf("%lf",&p[i]),P[i]=p[i]*M+0.5,li[i]=i;
    sort(li+1,li+n+1,cmp);
    for (int i=1;i<=n;i++)
     for (int j=m;j>=0;j--)
      {
         if (j+TS[li[i]]<=m)
           Update(j+TS[li[i]],cnt[j]+SS[li[i]]*M,TS[li[i]]+Exp[j]);
         if (j+TS[li[i]]+TL[li[i]]<=m)
           Update(j+TS[li[i]]+TL[li[i]],cnt[j]+SS[li[i]]*M+SL[li[i]]*(M-P[li[i]]),
             TS[li[i]]+p[li[i]]*Exp[j]+(1-p[li[i]])*(j+TL[li[i]]));
      }
    ans0=cnt[m];ans1=Exp[m];
    for (int i=m-1;i>=0;i--)
     if (ans0==cnt[i]&&Exp[i]<ans1) ans1=Exp[i];
    printf("%.10lf %.10lf\n",(double)(ans0)/M,ans1);
    return 0;
 }


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <cmath>
#include <cstdio>
#include <cctype>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cwchar>
#include <cwctype>
#include <exception>
#include <locale>
#include <numeric>
#include <new>
#include <stdexcept>
#include <limits>
#include <valarray>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <list>
#include <utility>
#include <bitset>
#include <algorithm>
#include <functional>
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF=1e9+7;
const double EPS=1e-9;
struct task{
	double ps,pl,prb;
	int ts,tl;
	bool operator <(const task& oth)const{
		return tl*oth.prb-tl*prb*oth.prb<oth.tl*prb-oth.tl*oth.prb*prb;
	}
}pro[1005];
double sc[1005][1565],t[1005][1565];
void upd(int i,int j,double csc,double ct){
	if(csc-sc[i][j]>EPS||csc-sc[i][j]>-EPS&&t[i][j]>ct){
		sc[i][j]=csc;
		t[i][j]=ct;
	}
}
int main(){
	int n,lim,nj;
	scanf("%d%d",&n,&lim);
	rep(k,n){
		scanf("%lf%lf%d%d%lf",&pro[k].ps,&pro[k].pl,&pro[k].ts,&pro[k].tl,&pro[k].prb);
		pro[k].prb=1.0-pro[k].prb;
	}
	sort(pro,pro+n);
	rep(i,n)for(int j=0;j<=lim;j++){
		upd(i+1,j,sc[i][j],t[i][j]);
		if((nj=j+pro[i].ts)>lim)continue;
		upd(i+1,nj,sc[i][j]+pro[i].ps,t[i][j]+pro[i].ts);
		if((nj=j+pro[i].ts+pro[i].tl)>lim)continue;
		upd(i+1,nj,sc[i][j]+pro[i].ps+pro[i].pl*pro[i].prb,(t[i][j]+pro[i].ts)*(1.0-pro[i].prb)+nj*pro[i].prb);
	}
	for(int k=0;k<=lim;k++)upd(0,0,sc[n][k],t[n][k]);
	printf("%lf %lf\n",sc[0][0],t[0][0]);
	return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <cmath>
#include <cstdio>
#include <cctype>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cwchar>
#include <cwctype>
#include <exception>
#include <locale>
#include <numeric>
#include <new>
#include <stdexcept>
#include <limits>
#include <valarray>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <list>
#include <utility>
#include <bitset>
#include <algorithm>
#include <functional>
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF=1e9+7;
const long double EPS=1e-9;
struct task{
	long double ps,pl,prb;
	int ts,tl;
	bool operator <(const task& oth)const{
		return tl*oth.prb-tl*prb*oth.prb<oth.tl*prb-oth.tl*oth.prb*prb;
	}
}pro[1005];
long double sc[1005][1565],t[1005][1565];
void upd(int i,int j,long double csc,long double ct){
	if(csc-sc[i][j]>EPS||csc-sc[i][j]>-EPS&&t[i][j]>ct){
		sc[i][j]=csc;
		t[i][j]=ct;
	}
}
int main(){
	ios::sync_with_stdio(false);
	int n,lim,nj;
	cin>>n>>lim;
	rep(k,n){
		cin>>pro[k].ps>>pro[k].pl>>pro[k].ts>>pro[k].tl>>pro[k].prb;
		pro[k].prb=1.0-pro[k].prb;
	}
	sort(pro,pro+n);
	rep(i,n)for(int j=0;j<=lim;j++){
		upd(i+1,j,sc[i][j],t[i][j]);
		if((nj=j+pro[i].ts)>lim)continue;
		upd(i+1,nj,sc[i][j]+pro[i].ps,t[i][j]+pro[i].ts);
		if((nj=j+pro[i].ts+pro[i].tl)>lim)continue;
		upd(i+1,nj,sc[i][j]+pro[i].ps+pro[i].pl*pro[i].prb,(t[i][j]+pro[i].ts)*(1.0-pro[i].prb)+nj*pro[i].prb);
	}
	for(int k=0;k<=lim;k++)upd(0,0,sc[n][k],t[n][k]);
	cout<<fixed<<setprecision(10)<<sc[0][0]<<' '<<t[0][0];
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef long double dcm;
#define initIO(pn) freopen(pn ".in", "r", stdin), freopen(pn ".out", "w", stdout)
#define arrset(p, v) memset(p, v, sizeof(p))
#define sqr(x) ((x) * (x))
#define rep(i, a, b) for (int i = a; b; i++)
#define rer(i, a, b) for (int i = a; b; i--)
#define trv(i, x, he, nx) for (int i = he[x]; i != -1; i = nx[i])
#define CT(T) const T&
const int MAXN = 1010;
const int MAXT = 1570;

struct dpNode {
	dcm s, t;
	dpNode() { s = t = 0; }
	dpNode(dcm _s, dcm _t) { s = _s, t = _t; }
};

struct item {
	int s1, s2, t1, t2;
	dcm p, k;

	void input() {
		double t;
		scanf("%d %d %d %d %lf", &s1, &s2, &t1, &t2, &t);
		p = t, k = t2 * p / (1 - p);
	}
};

bool itemCmp(CT(item) a, CT(item) b) {
	return a.k < b.k;
}

int n, T;
item a[MAXN];
dpNode f[MAXN][MAXT];

void lucky() {
	#ifndef ONLINE_JUDGE
	initIO("277D");
	#endif
	scanf("%d %d", &n, &T);
	rep (i, 1, i <= n) { a[i].input(); }
}

bool upd(dcm& a, dcm b) {
	return (a < b)? a = b, 1: 0;
}

void upd(dpNode& a, dcm s, dcm t) {
	if (upd(a.s, s))
		a.t = t;
	else if (a.s == s)
		a.t = min(a.t, t);
}

int main() {
	lucky();
	sort(a + 1, a + n + 1, itemCmp);
	f[0][0] = dpNode(0, 0);
	rep (i, 0, i < n) {
		rep (j, 0, j <= T) {
			int s1 = a[i + 1].s1, s2 = a[i + 1].s2, t1 = a[i + 1].t1, t2 = a[i + 1].t2;
			dcm p = a[i + 1].p;
			upd(f[i + 1][j], f[i][j].s, f[i][j].t);
			if (j + t1 <= T)
				upd(f[i + 1][j + t1], f[i][j].s + s1, f[i][j].t + t1);
			if (j + t1 + t2 <= T)
				upd(f[i + 1][j + t1 + t2], f[i][j].s + s1 + s2 * (1.0 - p), t1 + p * f[i][j].t + (1.0 - p) * (j + t2));
		}
	}
	dpNode ans;
	rep (i, 0, i <= T) {
		upd(ans, f[n][i].s, f[n][i].t);
	}
	printf("%.15lf %.15lf\n", double(ans.s), double(ans.t));
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef double dcm;
#define initIO(pn) freopen(pn ".in", "r", stdin), freopen(pn ".out", "w", stdout)
#define arrset(p, v) memset(p, v, sizeof(p))
#define sqr(x) ((x) * (x))
#define rep(i, a, b) for (int i = a; b; i++)
#define rer(i, a, b) for (int i = a; b; i--)
#define trv(i, x, he, nx) for (int i = he[x]; i != -1; i = nx[i])
#define CT(T) const T&
const int MAXN = 1010;
const int MAXT = 1570;

struct dpNode {
	dcm s, t;
	dpNode() { s = t = 0; }
	dpNode(dcm _s, dcm _t) { s = _s, t = _t; }
};

struct item {
	int s1, s2, t1, t2;
	dcm p, k;

	void input() {
		scanf("%d %d %d %d %lf", &s1, &s2, &t1, &t2, &p);
		k = t2 * p / (1 - p);
	}
};

bool itemCmp(CT(item) a, CT(item) b) {
	return a.k < b.k;
}

int n, T;
item a[MAXN];
dpNode f[MAXN][MAXT];

void lucky() {
	#ifndef ONLINE_JUDGE
	initIO("277D");
	#endif
	scanf("%d %d", &n, &T);
	rep (i, 1, i <= n) { a[i].input(); }
}

bool upd(dcm& a, dcm b) {
	return (a < b)? a = b, 1: 0;
}

void upd(dpNode& a, dcm s, dcm t) {
	if (upd(a.s, s))
		a.t = t;
	else if (a.s == s)
		a.t = min(a.t, t);
}

int main() {
	lucky();
	sort(a + 1, a + n + 1, itemCmp);
	f[0][0] = dpNode(0, 0);
	rep (i, 0, i < n) {
		rep (j, 0, j <= T) {
			int s1 = a[i + 1].s1, s2 = a[i + 1].s2, t1 = a[i + 1].t1, t2 = a[i + 1].t2;
			dcm p = a[i + 1].p;
			upd(f[i + 1][j], f[i][j].s, f[i][j].t);
			if (j + t1 <= T)
				upd(f[i + 1][j + t1], f[i][j].s + s1, j + t1);
			if (j + t1 + t2 <= T)
				upd(f[i + 1][j + t1 + t2], f[i][j].s + s1 + s2 * (1.0 - p), t1 + p * f[i][j].t + (1.0 - p) * (j + t2));
		}
	}
	dpNode ans;
	rep (i, 0, i <= T) {
		upd(ans, f[n][i].s, f[n][i].t);
	}
	printf("%.10lf %.10lf\n", ans.s, ans.t);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef double dcm;
#define initIO(pn) freopen(pn ".in", "r", stdin), freopen(pn ".out", "w", stdout)
#define arrset(p, v) memset(p, v, sizeof(p))
#define sqr(x) ((x) * (x))
#define rep(i, a, b) for (int i = a; b; i++)
#define rer(i, a, b) for (int i = a; b; i--)
#define trv(i, x, he, nx) for (int i = he[x]; i != -1; i = nx[i])
#define CT(T) const T&
const int MAXN = 1010;
const int MAXT = 1570;

struct dpNode {
	dcm s, t;
	dpNode() { s = t = 0; }
	dpNode(dcm _s, dcm _t) { s = _s, t = _t; }
};

struct item {
	int s1, s2, t1, t2;
	dcm p, k;

	void input() {
		scanf("%d %d %d %d %lf", &s1, &s2, &t1, &t2, &p);
		k = t2 * p / (1 - p);
	}
};

bool itemCmp(CT(item) a, CT(item) b) {
	return a.k < b.k;
}

int n, T;
item a[MAXN];
dpNode f[MAXN][MAXT];

void lucky() {
	#ifndef ONLINE_JUDGE
	initIO("277D");
	#endif
	scanf("%d %d", &n, &T);
	rep (i, 1, i <= n) { a[i].input(); }
}

bool upd(dcm& a, dcm b) {
	return (a < b)? a = b, 1: 0;
}

void upd(dpNode& a, dcm s, dcm t) {
	if (upd(a.s, s))
		a.t = t;
	else if (a.s == s)
		a.t = min(a.t, t);
}

int main() {
	lucky();
	sort(a + 1, a + n + 1, itemCmp);
	f[0][0] = dpNode(0, 0);
	rep (i, 0, i < n) {
		rep (j, 0, j <= T) {
			int s1 = a[i + 1].s1, s2 = a[i + 1].s2, t1 = a[i + 1].t1, t2 = a[i + 1].t2;
			dcm p = a[i + 1].p;
			upd(f[i + 1][j], f[i][j].s, f[i][j].t);
			if (j + t1 <= T)
				upd(f[i + 1][j + t1], f[i][j].s + s1, f[i][j].t + t1);
			if (j + t1 + t2 <= T)
				upd(f[i + 1][j + t1 + t2], f[i][j].s + s1 + s2 * (1.0 - p), t1 + p * f[i][j].t + (1.0 - p) * (j + t2));
		}
	}
	dpNode ans;
	rep (i, 0, i <= T) {
		upd(ans, f[n][i].s, f[n][i].t);
	}
	printf("%.10lf %.10lf\n", ans.s, ans.t);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=2005;
double p[maxn],g[maxn];
int n,m,i,j,s[maxn];
LL sp[maxn],lp[maxn],st[maxn],lt[maxn],pr[maxn],f[maxn],b=1000000;
bool cmp(int x,int y){ return (lt[x]+lt[y])*b*(b-pr[y])+lt[x]*(b-pr[x])*pr[y]<(lt[x]+lt[y])*b*(b-pr[x])+lt[y]*(b-pr[y]); }
void upd(int t,LL x,double y)
{
	if(f[t]<x)f[t]=x,g[t]=y;
	else if(f[t]==x&&g[t]>y)g[t]=y;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)
	{
		scanf("%I64d %I64d %I64d %I64d %lf",sp+i,lp+i,st+i,lt+i,p+i);
		pr[i]=p[i]*b+0.5;s[i]=i;
	}
	sort(s+1,s+n+1,cmp);
	for(i=1;i<=n;i++)
	{
		for(j=m;j>=0;j--)
		{
			if(j+st[s[i]]<=m)
			upd(j+st[s[i]],f[j]+sp[s[i]]*b,g[j]+st[s[i]]);
			if(j+st[s[i]]+lt[s[i]]<=m)
			upd(j+st[s[i]]+lt[s[i]],f[j]+sp[s[i]]*b+lp[s[i]]*(b-pr[s[i]]),st[s[i]]+p[s[i]]*g[j]+(1-p[s[i]])*(lt[s[i]]+j));
		}
	}
	for(i=0;i<m;i++)upd(m,f[i],g[i]);
	printf("%.10f %.10f\n",(double)f[m]/b,g[m]);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=2005;
double p[maxn],g[maxn];
int n,m,i,j,s[maxn];
LL sp[maxn],lp[maxn],st[maxn],lt[maxn],pr[maxn],f[maxn],b=1000000;
bool cmp(int x,int y){ return (lt[x]+lt[y])*b*(b-pr[y])+lt[x]*(b-pr[x])*pr[y]<(lt[x]+lt[y])*b*(b-pr[x])+lt[y]*(b-pr[y]); }
void upd(int t,LL x,double y)
{
	if(f[t]<x)f[t]=x,g[t]=y;
	else if(f[t]==x&&g[t]>y)g[t]=y;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)
	{
		scanf("%I64d %I64d %I64d %I64d %lf",sp+i,lp+i,st+i,lt+i,p+i);
		pr[i]=p[i]*b;s[i]=i;
	}
	sort(s+1,s+n+1,cmp);
	for(i=1;i<=n;i++)
	{
		for(j=m;j>=0;j--)
		{
			if(j+st[s[i]]<=m)
			upd(j+st[s[i]],f[j]+sp[s[i]]*b,g[j]+st[s[i]]);
			if(j+st[s[i]]+lt[s[i]]<=m)
			upd(j+st[s[i]]+lt[s[i]],f[j]+sp[s[i]]*b+lp[s[i]]*(b-pr[s[i]]),st[s[i]]+p[s[i]]*g[j]+(1-p[s[i]])*(lt[s[i]]+j));
		}
	}
	for(i=0;i<m;i++)upd(m,f[i],g[i]);
	printf("%.10f %.10f\n",1.*f[i]/b,g[i]);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;
typedef long long LL;
const int maxn=2005;
double p[maxn],g[maxn];
int n,m,i,j,s[maxn];
LL sp[maxn],lp[maxn],st[maxn],lt[maxn],pr[maxn],f[maxn],b=1000000;
bool cmp(int x,int y){ return (lt[x]+lt[y])*b*(b-pr[y])+lt[x]*(b-pr[x])*pr[y]<(lt[x]+lt[y])*b*(b-pr[x])+lt[y]*pr[x]*(b-pr[y]); }
void upd(int t,LL x,double y)
{
	if(f[t]<x)f[t]=x,g[t]=y;
	else if(f[t]==x&&g[t]>y)g[t]=y;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)
	{
		scanf("%I64d %I64d %I64d %I64d %lf",sp+i,lp+i,st+i,lt+i,p+i);
		pr[i]=p[i]*b+0.5;s[i]=i;
	}
	sort(s+1,s+n+1,cmp);
	for(i=1;i<=n;i++)
	{
		for(j=m;j>=0;j--)
		{
			if(j+st[s[i]]<=m)
			upd(j+st[s[i]],f[j]+sp[s[i]]*b,g[j]+st[s[i]]);
			if(j+st[s[i]]+lt[s[i]]<=m)
			upd(j+st[s[i]]+lt[s[i]],f[j]+sp[s[i]]*b+lp[s[i]]*(b-pr[s[i]]),st[s[i]]+p[s[i]]*g[j]+(1.-p[s[i]])*(lt[s[i]]+j));
		}
	}
	for(i=0;i<m;i++)upd(m,f[i],g[i]);
	printf("%.10f %.10f\n",(double)f[m]/b,g[m]);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;
const int kMaxN = 1000, kMaxT = 1560;
int N, T;
ll f[kMaxN + 1][kMaxT + 1];
double g[kMaxN + 1][kMaxT + 1];

struct Problem {
    int scS, scL, tiS, tiL;
    ll exp_scS, exp_scL;
    double fail, key;
    bool operator < (const Problem &t) const {
        return (key < t.key);
    }
} problems[kMaxN + 1];

inline void upt(int n, int t, ll ff, double gg) {
    if (n <= N && t <= T && (ff > f[n][t] || (ff == f[n][t] && gg < g[n][t]))) {
        f[n][t] = ff;
        g[n][t] = gg;
    }
}

int main() {
    scanf("%d%d", &N, &T);
    for (int i = 1; i <= N; ++ i) {
        Problem &t = problems[i];
        scanf("%d%d%d%d%lf", &t.scS, &t.scL, &t.tiS, &t.tiL, &t.fail);
        int p = (int)(t.fail * 1000000 + 0.5);
        t.exp_scS = (ll)t.scS * 1000000;
        t.exp_scL = (ll)t.scL * (1000000 - p);
        if (p)
            t.key = t.fail / (1.0 - t.fail) * t.tiL;
        else
            t.key = 1E18;
    }
    sort(problems + 1, problems + N + 1);
    memset(f, -1, sizeof(f));
    f[0][0] = 0;
    for (int i = 0; i < N; ++ i)
        for (int j = 0; j <= T; ++ j)
            if (f[i][j] >= 0) {
                Problem &t = problems[i + 1];
                upt(i + 1, j, f[i][j], g[i][j]);
                upt(i + 1, j + t.tiS, f[i][j] + t.exp_scS, g[i][j] + t.tiS);
                upt(i + 1, j + t.tiS + t.tiL, f[i][j] + t.exp_scS + t.exp_scL, t.fail * (g[i][j] + t.tiS) + (1.0 - t.fail) * (j + t.tiS + t.tiL));
            }
    ll ff = 0;
    double gg = 0.0;
    for (int j = 0; j <= T; ++ j)
        if (f[N][j] > ff || (f[N][j] == ff && g[N][j] < gg)) {
            ff = f[N][j];
            gg = g[N][j];
        }
    printf("%.12lf %.12lf\n", ff * 1.0 / 1000000.0, gg);
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;
const int kMaxN = 1000, kMaxT = 1560;
int N, T;
ll f[kMaxN + 1][kMaxT + 1];
double g[kMaxN + 1][kMaxT + 1];

struct Problem {
    int scS, scL, tiS, tiL;
    ll exp_scS, exp_scL;
    double fail, key;
    bool operator < (const Problem &t) const {
        return (key < t.key);
    }
} problems[kMaxN + 1];

inline void upt(int n, int t, ll ff, double gg) {
    if (n <= N && t <= T && (ff > f[n][t] || (ff == f[n][t] && gg < g[n][t]))) {
        f[n][t] = ff;
        g[n][t] = gg;
    }
}

int main() {
    scanf("%d%d", &N, &T);
    for (int i = 1; i <= N; ++ i) {
        Problem &t = problems[i];
        scanf("%d%d%d%d%lf", &t.scS, &t.scL, &t.tiS, &t.tiL, &t.fail);
        int p = (int)(t.fail * 1000000 + 0.5);
        t.exp_scS = (ll)t.scS * 1000000;
        t.exp_scL = (ll)t.scL * (1000000 - p);
        if (p != 1000000)
            t.key = t.fail / (1.0 - t.fail) * t.tiL;
        else
            t.key = 1E15;
    }
    sort(problems + 1, problems + N + 1);
    memset(f, -1, sizeof(f));
    f[0][0] = 0;
    for (int i = 0; i < N; ++ i)
        for (int j = 0; j <= T; ++ j)
            if (f[i][j] >= 0) {
                Problem &t = problems[i + 1];
                upt(i + 1, j, f[i][j], g[i][j]);
                upt(i + 1, j + t.tiS, f[i][j] + t.exp_scS, g[i][j] + t.tiS);
                upt(i + 1, j + t.tiS + t.tiL, f[i][j] + t.exp_scS + t.exp_scL, t.fail * (g[i][j] + t.tiS) + (1.0 - t.fail) * (j + t.tiS + t.tiL));
            }
    ll ff = 0;
    double gg = 0.0;
    for (int j = 0; j <= T; ++ j)
        if (f[N][j] > ff || (f[N][j] == ff && g[N][j] < gg)) {
            ff = f[N][j];
            gg = g[N][j];
        }
    printf("%.12lf %.12lf\n", ff * 1.0 / 1000000.0, gg);
    return 0;
}


import sys

n = int(sys.stdin.readline())

print(n)


import sys

def appr(s):
        if len(s)<10 :
            print (s)
        else :
            print(str(s[0])+str(len(s)-2)+str(s[len(s)-1]))

n = int(sys.stdin.readline())
for x in xrange(0, n-1):
    s = (sys.stdin.readline()).strip()
    appr(s)


def appr(s):
        if len(s)<10 :
            print (s)
        else :
            print(str(s[0])+str(len(s)-2)+str(s[len(s)-1]))

n = int(raw_input())
for x in xrange(n):
    s = raw_input()
    print(s)


import sys

def appr(s):
        if len(s)<10 :
            print (s)
        else :
            print(str(s[0])+str(len(s)-2)+str(s[len(s)-1]))

n = int(sys.stdin.readline())
for x in xrange(0, n):
    s = (sys.stdin.readline()).strip()
    print(s)


import sys

def appr(s):
        if len(s)<10 :
            print (s)
        else :
            print(str(s[0])+str(len(s)-2)+str(s[len(s)-1]))

n = int(sys.stdin.readline())
for x in xrange(0, n):
    s = (sys.stdin.readline()).strip()
    print s


import sys

def appr(s):
        if len(s)<10 :
            print (s)
        else :
            print(str(s[0])+str(len(s)-2)+str(s[len(s)-1]))

n = int(sys.stdin.readline())
for x in xrange(0, n):
    s = (sys.stdin.readline()).strip()
    appr(s)


n=int(input())
for i in range(0,n):
    k=input()
    t=len(k)-2
    if(len(k)>10):
        s=k[0]+"%t"+k[n-1]
        print(s)
    else:
        print(k)


n=int(input())
for i in range(0,n):
    k=input()
    t=len(k)-2
    if(len(k)>10):
        print(k[0],end='')
        print(t',end='')
        print(k[n-1])
    else:
        print(k)


n=int(input())
for i in range(0,n):
    k=input()
    if(len(k)>10):
        s=k[0]+"len(k)-2"+k[n-1]
        print(s)
    else:
        print(k)


n=int(input())
for i in range(0,n):
    k=input()
    t=len(k)-2
    if(len(k)>10):
        s=k[0]+"t"+k[n-1]
        print(s)
    else
        print(k)


n=int(input())
for i in range(0,n):
    k=input()
    t=len(k)-2
    if(len(k)>10):
        print(k[0],end='')
        print(t'end='')
        print(k[n-1])
    else:
        print(k)


n=int(input())
for i in range(0,n):
    k=input()
    t=len(k)-2
    if(len(k)>10):
        s=k[0]+""+k[n-1]
        print(s)
    else:
        print(k)


n=int(input())
for i in range(0,n):
    k=input()
    t=len(k)-2
    if(len(k)>10):
        print(k[0])
        print("%d",t)
        print(k[n-1])
    else:
        print(k)


n=int(input())
for i in range(0,n):
    k=input()
    t=len(k)-2
    if(len(k)>10):
        s=k[0]+"%d",t+k[n-1]
        print(s)
    else:
        print(k)


n=int(input())
for i in range(0,n):
    k=input()
    t=len(k)-2
    if(len(k)>10):
        print(k[0],end='')
        print(t,end='')
        print(k[len(k)-1])
    else:
        print(k)


n=int(input())
for i in range(0,n):
    k=input()
    t=len(k)-2
    if(len(k)>10):
        print(k[0]+"%d",t+k[n-1])
    else:
        print(k)


n=int(input())
for i in range(0,n):
    k=input()
    if(len(k)>10):
        s=k[0]+"len(k)-2"+k[n-1]
        print(s)
    else
        print(k)


n = int(input())

if 1 <= n <= 100:
    for r in range(0, n):
        w = input().lower()
        if len(w) > 10:
            print(f"{w[0]}{len(w) - 2}{w[-1]}")
        elif len(w) <= 10:
            print(w)


n = input()

if n.isalpha() and 10 <= len(n) <= 100:
    print(f"{n[0]}{len(n) - 2}{n[-1]}")
elif n.isalpha() and len(n) < 10:
    print(n)


while True:
    n = input()

    if n.isalpha() and 10 <= len(n) <= 100:
        print(f"{n[0]}{len(n) - 2}{n[-1]}")
    elif n.isalpha() and len(n) < 10:
        print(n)


n = int(input())

if 1 <= n <= 100:
    for r in range(0, n):
        w = input().lower()
        if 10 < len(w):
            print(f"{w[0]}{len(w) - 2}{w[-1]}")
        elif len(w) < 10:
            print(w)


if 1 <= n <= 100:
    for r in range(0, n):
        w = input().lower()
        if len(w) > 11:
            print(f"{w[0]}{len(w) - 2}{w[-1]}")
        elif len(w) < 10:
            print(w)


n = int(input())

if 1 <= n <= 100:
    for r in range(0, n):
        w = input().lower()
        if len(w) > 11:
            print(f"{w[0]}{len(w) - 2}{w[-1]}")
        elif len(w) < 10:
            print(w)


n = input()

if 10 <= len(n) <= 100 and int(n) is False:
    print(f"{n[0]}{len(n) - 2}{n[-1]}")
elif len(n) < 10 and int(n) is False:
    print(n)


n = int(input())

for r in range(0, n):
    w = input().lower()
    if 10 <= len(w) <= 100:
        print(f"{w[0]}{len(w) - 2}{w[-1]}")
    elif len(w) < 10:
        print(w)


#include < iostream >

using namespace std;

int main(){int n; cin>>n
while(n--){string s;cin>>s;
if(s.length()>10){cout<<s[0]<<s.length()-2<<s[s.length()-1]<<endl;}
else{cout<<s<endl;}}
return 0;}


input=input()
input=input.splitlines()
n=int(input[0])
for i in range(1,len(input)+1):
    a=''
    w=input[i]
    if len(w)<10:
        print(w)
    else:
        a=w[0]+str(len(w))+w[-1]
        print(a)


import sys
inputs = sys.stdin.read().split()
for v in inputs[1:]:
    if len(v) > 10:
        print('{}{}{}'.format(v[0], str(len(v)-2), v[len(v)-1]))
    else:
        print(v)


#include < iostream >

using namespace std;

int main(){int n;string s; cin>>n;
while(n--){cin>>s;
if(s.length()>10){cout<<s[0]<<s.length()-2<<s[s.length()-1]<<endl;}
else{cout<<s<endl;}}
return 0;}


input=input()
input=input.splitlines()
n=int(input[0])
for i in range(1:len(input)+1):
    a=''
    w=input[i]
    if len(w)<10:
        print(w)
    else:
        a=w[0]+str(len(w))+w[-1]
        print(a)


import sys
input=sys.stdin.read().split()
for w in input[1:]:
 if len(w)>10:
  print("{}{}{}".format(w[0],str(len(w)-2),w[-1])
 else:
  print(w)


import sys
input=sys.stdin.read().split()
for w in input[1:]:
    if len(w)>10:
        print("{}{}{}".format(w[0]+str(len(w)-2)+w[-1])
    else:
        print(w)


input=input()
input=input.splitlines()
n=int(input[0])
for i in range(1,len(input)):
    a=''
    w=input[i]
    if len(w)<10:
        print(w)
    else:
        a=w[0]+str(len(w))+w[-1]
        print(a)


input=input()
input=input.splitlines()
n=int(input[0])
for i in range(1:len(input)):
    a=''
    w=input[i]
    if len(w)<10:
        print(w)
    else:
        a=w[0]+str(len(w))+w[-1]
        print(a)


import sys
input=sys.stdin.read().split()
for w in input[1:]:
 if len(w)>10:
  print("{}{}{}".format(w[0],str(len(w)-2),w[len(w)-1])
 else:
  print(w)


n=input()
for i in range(n):
    a=''
    w=input()
    a=w[0]+str(len(w))+w[-1]
    print(a)


#include < iostream >

using namespace std;

int main(){int n; cin>>n;
while(n--){cin>>s;
if(s.length()>10){cout<<s[0]<<s.length()-2<<s[s.length()-1]<<endl;}
else{cout<<s<endl;}}
return 0;}


input=input()
input=input.splitlines()
n=int(input[0])
for i in range(1,len(input)):
    a=''
    w=input[i]
    if len(w)<10:
        print(w)
    else:
        a=w[0]+str(len(w))+w[-1]+'\n'
        print(a)


n=int(input())
s=[]
for i in range(n):
    ele=input()
    s.append(ele)
for i in s:
    if(len(s)>10):
        print(s[0]+str(len(s)-2)+s[-1] if len(s)>10)
    else:
        print(s)


n=int(input())
s=raw_input()
for i in range(len(n)):
    if (len(s)<10):
        print (%s)
    else :
        print(s[0]+str(len(s)-2)+s[-1]):


n=input()
r=int(n)
for i in range(r):
    s=input()
    if(len(s)> 10):
        print(s[0]+str(len(s)-2)+s[-1])
    else:
        print(s)


n=int(input())
for i in range(len(n)):
    s=input()
    if(len(s)>10):
        print(s[0]+str(len(s)-2)+s[-1] )
    else:
        print(s)


n=int(input())
for i in range(len(n)):
    s=input()
    if(len(s)>10):
        print(s[0]+str(len(s)-2)+s[-1])
    else:
        print(s)


n=int(input())
s=[]
for i in range(n):
    ele=input()
    s.append(ele)
for i in s:
    if(len(s)>10):
        print(s[0]+str(len(s)-2)+s[-1] )
    else:
        print(s)


n=int(input())
s=raw_input()
for i in range(len(n)):
    if (len(s)<10):
        print (s)
    else :
        print(s[0]+str(len(s)-2)+s[-1])


n=int(input())
for i in range(len(n)):
    s=input[]
    if(len(s)>10):
        print(s[0]+str(len(s)-2)+s[-1])
    else:
        print(s)


n=int(input())
s=raw_input()
for i in (len(n)):
    if(len(s)>10):
        print(s[0]+str(len(s)-2)+s[-1] )
    else:
        print(s)


for i in range(input()):
    s= raw_input()
    if len(s)>10:
        print (s[0]+str(len(s)-2)+s[-1])
    else:
        print(s)


n=int(input())
s=raw_input()
for i in range(len(n)):
    if (len(s)<10):
        print (s)
    else :
        print(s[0]+str(len(s)-2)+s[-1]):


for i in range(input()):
    s= raw_input()
    if len(s)>10
    print s[0]+str(len(s)-2)+s[-1]
    else:
        print(s)


n=int(input())
for i in range(n):
    s=input()
    if(len(s)>10):
        print(s[0]+str(len(s)-2)+s[-1])
    else:
        print(s)


n=int(input())
s=raw_input()
for i in (len(n)):
    if(len(s)>10):
        print(s[0]+str(len(s)-2)+s[-1] if len(s)>10)
    else:
        print(s)


for _ in range(input()):
    s= raw_input()
    print s[0]+str(len(s)-2)+s[-1] if len(s)>10 else s


for _ in range(input()):
    s = input()
    if s > 10:
        print(s[0] +str(len(s)-2) +s[-1])
    else:
        return s


def main(s):
	for _ in range(input())
	s = input()
	if s > 10:
		s_new = s[0] +str(len(s)-2) +s[-1]
		print(s_new)
	else:
		return s

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))


def main(s):
	for _ in range(input()):
		s = input()
		if s > 10:
			s_new = s[0] +str(len(s)-2) +s[-1]
			print(s_new)
		else:
			return s

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))


def main(s):
	for _ in range(input()):
		s = input()
		if len(s) > 10:
			print(s[0] +str(len(s)-2) +s[-1])
		else:
			s

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))


for _ in range(input()):
    s= input()
    if len(s)>10:
        print(s[0]+str(len(s)-2)+s[-1])
    else:
        s


def main(s):
	for _ in range(input()):
		s = input()
		if s > 10:
			print(s[0] +str(len(s)-2) +s[-1])
		else:
			s

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))


def main(s):
	n = int(input())
	for i in range(n):
		s = input()
		if len(s) > 10:
			print(s[0] + str(len(s)-2) + s[-1])
		else:
			print(s)

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))


for _ in range(input()):
    s = input()
    if s > 10:
        s_new = s[0] +str(len(s)-2) +s[-1]
        print(s_new)
    else:
        s


def main(s):
	n = int(input())
	for i in range(n):
		s = input()
	if len(s) > 10:
		print(s[0] + str(len(s)-2) + s[-1])
	else:
		print(s)


for _ in range(input()):
    s= input()
    print s[0]+str(len(s)-2)+s[-1] if len(s)>10 else s


#!/usr/bin/env python
counter = 0
integer = int(input())

for x in range(integer):
  text = str(input())
  text = list(text)
  length = len(text)

  if len(text) < 10:
    print(''.join(map(str, text)))
  else:
    for i in text[1:-1]:
      counter += 1
      first_char = text[0]
      last_char = text[-1]
    print(first_char + str(counter) + last_char)


#!/usr/bin/env python

while True:
    counter = 0
    flag = False
    text = str(input())

    try:
        int(text)
        flag = True
    except ValueError:
        pass

    if flag == True:
        continue

    text = list(text)
    length = len(text)

    if len(text) < 10:
        print(''.join(map(str, text)))
    else:
        for i in text[1:-1]:
            counter += 1

        first_char = text[0]
        last_char = text[-1]
        print(first_char + str(counter) + last_char)


integer = int(input())

for x in range(integer):
  counter = 0
  text = str(input())
  text = list(text)
  length = len(text)

  if len(text) <= 10:
    print(''.join(map(str, text)))
  else:
    for i in text[1:-1]:
      counter += 1
      first_char = text[0]
      last_char = text[-1]
    print(first_char + str(counter) + last_char)


#!/usr/bin/env python
counter = 0
flag = False
text = str(input())

try:
    int(text)
    flag = True
except ValueError:
    pass

if flag == True:
    print("")
    quit()

text = list(text)
length = len(text)

if len(text) < 10:
    print(''.join(map(str, text)))
else:
    for i in text[1:-1]:
        counter += 1

    first_char = text[0]
    last_char = text[-1]
    print(first_char + str(counter) + last_char)


#!/usr/bin/env python
counter = 0
integer = int(input())

for x in range(integer):
  text = str(input())

  text = list(text)
  length = len(text)

  if len(text) < 10:
    print(''.join(map(str, text)))
  else:
    for i in text[1:-1]:
      counter += 1
      first_char = text[0]
      last_char = text[-1]
      print(first_char + str(counter) + last_char)


#!/usr/bin/env python
counter = 0
string = input()

if isinstance(string, int)):
    sys.exit(0)

string = list(string)
length = len(string)

if len(string) < 10:
    print(''.join(map(str, string)))
else:
    for i in string[1:-1]:
        counter += 1
    first_char = string[0]
    last_char = string[-1]
    print(first_char + str(counter) + last_char)


#!/usr/bin/env python
integer = int(input())

for x in range(integer):
  counter = 0
  text = str(input())
  text = list(text)
  length = len(text)

  if len(text) < 10:
    print(''.join(map(str, text)))
  else:
    for i in text[1:-1]:
      counter += 1
      first_char = text[0]
      last_char = text[-1]
    print(first_char + str(counter) + last_char)


#!/usr/bin/env python
import sys

counter = 0
string = input()
if string.isdigit():
    sys.exit(0)
string = list(string)
length = len(string)

if len(string) < 10:
    print(''.join(map(str, string)))
else:
    for i in string[1:-1]:
        counter += 1
    first_char = string[0]
    last_char = string[-1]
    print(first_char + str(counter) + last_char)


#!/usr/bin/env python
counter = 0
flag = False
text = str(input())

try:
    int(text)
    flag = True
except ValueError:
    pass

if flag == True:
    quit()

text = list(text)
length = len(text)

if len(text) < 10:
    print(''.join(map(str, text)))
else:
    for i in text[1:-1]:
        counter += 1
    first_char = text[0]
    last_char = text[-1]
    print(first_char + str(counter) + last_char)


#!/usr/bin/env python
counter = 0
string = input()

if isinstance(string, int):
    sys.exit(0)

string = list(string)
length = len(string)

if len(string) < 10:
    print(''.join(map(str, string)))
else:
    for i in string[1:-1]:
        counter += 1
    first_char = string[0]
    last_char = string[-1]
    print(first_char + str(counter) + last_char)


#!/usr/bin/env python
counter = 0
string = input()
if string.isdigit():
    quit()

string = list(string)
length = len(string)

if len(string) < 10:
    print(''.join(map(str, string)))
else:
    for i in string[1:-1]:
        counter += 1
    first_char = string[0]
    last_char = string[-1]
    print(first_char + str(counter) + last_char)


#!/usr/bin/env python
counter = 0
string = input()
string = list(string)
length = len(string)

if len(string) < 10:
    print(''.join(map(str, string)))
else:
    for i in string[1:-1]:
        counter += 1
    first_char = string[0]
    last_char = string[-1]
    print(first_char + str(counter) + last_char)


    n = int(input())
    for i in range(n):
        word = input()
        length = len(word)
        if length<=10:
        	print(word)
        else:
            subnum = str(length-2)
            abbr = word[0]+subnum+word[-1]
            print(short)


m=int(input())
for i in range(n):
    n=input()
    if len(n)>10:
        subnum=str(len(n)-2)
        abbr=word[0]+subnum+word[-1]
        print(abbr)
    else:
        print(word)


n = int(input())
for i in range(n):
    word = input()
    length = len(word)
    if length <=10:
    	print(word)
    else:
        num = str(length-2)
        short = word[0]+num+word[-1]
        print(short)


n = int(input())
for i in range(n):
    word = input()
    length = len(word)
    if length >10:
        num = str(length-2)
        short = word[0]+num+word[-1]
        print(short)
    else:
        print(word)


m=int(input())
for n in range(m):
    n=input()
    if len(n)<=10:
	    print(n)
    else:
	    subnum=len(n)-2
	    abbr=n[0]+subnum+n[-1]
	    print(abbr)


n = int(input())
for i in range(n):
    word = input()
    if len(word)<=10:
    	print(word)
    else:
        subnum = str(len(word)-2)
        abbr = word[0]+subnum+word[-1]
        print(abbr)


m=int(input())
for i in range(m):
    n=input()
    if len(n)>10:
        subnum=str(len(n)-2)
        abbr=word[0]+subnum+word[-1]
        print(abbr)
    else:
        print(word)


n = int(input())
for i in range(n):
    word = input()
    length = len(word)
    if length<=10:
    	print(word)
    else:
        num = str(length-2)
        short = word[0]+num+word[-1]
        print(short)


n = int(input())
for i in range(n):
    word = input()
    length = len(word)
    if length<=10:
    	print(word)
    else:
        subnum = str(length-2)
        abbr = word[0]+subnum+word[-1]
        print(abbr)


n=input()
if len(n)<=10:
	print(n)
else:
	subnum=len(n)-2
	abbr=n[0]+subnum+n[-1]
	print(abbr)


m=int(input())
for i in range(m):
    n=input()
    length=len(n)
    if length>10:
        subnum=str(length-2)
        abbr=word[0]+subnum+word[-1]
        print(abbr)
    else:
        print(word)


n = int(input())
for i in range(n):
    word = input()
    length = len(word)
    if length<=10:
        print(word)
    else:
        subnum = str(length-2)
        abbr = word[0]+subnum+word[-1]
        print(short)


n = int(input())
for i in range(n):
    word = input()
    length = len(word)
    if length<=10:
        print(word)
    else:
        subnum = str(length-2)
        abbr = word[0]+subnum+word[-1]
        print(abbr)


n=int(input())
for i in range(n):
    word=input()
    length=len(word)
    if length<=10:
        print(word)
    else:
        subnum=str(length-2)
        abbr=word[0]+subnum+word[-1]
        print(abbr)


n=int(input())
for i in range(n):
    word=input()
    if len(word)<=10:
        print(word)
    else:
        subnum=str(len(word)-2)
        abbr=word[0]+subnum+word[-1]
        print(abbr)


n = str(input())
lenOfString = len(n)

if lenOfString > 10:
    print(n[0]+lenOfString+n[lenOfString-1]
else:
    print(n)


n = str(input())
lenOfString = len(n)

if lenOfString > 10:
    print(n[0]+lenOfString+n[lenOfString-1])
else:
    print(n)


n = input()
for i in range (1,int(n)+1):
    j = str(input())
    if (len(j)) > 10:
        print(j[0] + (len(j) - 2 + j[-1]))
    else:
        print(j)


n = input()

for i in range (len(n)):
    if i != 0:
        if (len(n[i])) > 10:
            print(n[i][0] + ((len(n)) - 2) + n[i][-1])
        elif (len(n[i])) <= 10:
            print(n[i])


n = (input())
for i in range n:
    if type(i) != int
        if (len(i)) > 10:
            print(i[0] + len(i) + i[len(i)-1])
        elif (len(i)) <= 10:
            print(i)


n = input()
if type(n) != int:
    lenOfString = len(n)

if lenOfString > 10:
    print(n[0]+lenOfString+n[lenOfString-1]
elif lenOfString <= 10:
    print(n)


n = input()

for i in range(1, (int[n] + 1):
    newN = str(input())
    if (len(newN)) > 10:
        print(newN[0] + (len(newN))-2 + newN[-1])
    else:
        print(newN)


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

typedef long long ll;

inline void read(int& x){
    static char c;
    while(!isdigit(c=getchar()));x=c^48;
    while(isdigit(c=getchar()))x=(x*10)+(c^48);
}

int T;

ll n;

int m;

int cnt[105];

int _2Div[105],tot;

inline void Divide(ll num){
    tot=0;
    while(num) _2Div[tot++]=num&1,num>>=1;
}

inline void Work(){
    read(T);
    while(T--){
        memset(cnt,0,sizeof 0);
        scanf("%lld",&n);read(m);
        Divide(n);--tot;
        int ans=0,mx=0;
        ll sum=0;
        for(REG int i=1;i<=m;++i){
            int tmp;read(tmp);sum+=tmp;
            tmp=(int)log2(tmp);mx=max(mx,tmp);
            ++cnt[tmp];
        }
        if(sum<n){puts("-1");continue;}
        for(REG int i=0;i<=tot;++i){
            if(i) cnt[i]+=(cnt[i-1]>>1);
            if(_2Div[i])
                if(cnt[i]){--cnt[i];continue;}
                else{
                    int flag=0;
                    for(REG int j=i+1;j<=max(mx,tot);++j)
                        if(cnt[j]){flag=j;break;}
                    --cnt[flag];
                    ans+=flag-i;
                    i=flag;
                }
        }
        printf("%d\n",ans);
    }
    getchar(),getchar();
}

int main(){Work();}


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

typedef long long ll;

inline void read(int& x){
    static char c;
    while(!isdigit(c=getchar()));x=c^48;
    while(isdigit(c=getchar()))x=(x*10)+(c^48);
}

int T;

ll n;

int m;

int cnt[105];

int _2Div[105],tot;

inline void Divide(ll num){
    tot=0;
    while(num) _2Div[tot++]=num&1,num>>=1;
}

inline void Work(){
    read(T);
    while(T--){
        memset(cnt,0,sizeof 0);
        scanf("%lld",&n);read(m);
        Divide(n);--tot;
        int ans=0,mx=0;
        ll sum=0;
        for(REG int i=1;i<=m;++i){
            int tmp;read(tmp);sum+=tmp;
            tmp=(int)log2(tmp);mx=max(mx,tmp);
            ++cnt[tmp];
        }
        if(sum<n){puts("-1");continue;}
        for(REG int i=0;i<=tot;++i){
            if(i) cnt[i]+=(cnt[i-1]>>1);
            if(_2Div[i])
                if(cnt[i]){--cnt[i];continue;}
                else{
                    int flag=0;
                    for(REG int j=i+1;j<=max(mx,tot);++j)
                        if(cnt[j]){flag=j;break;}
                    --cnt[flag];
                    ans+=flag-i;
                    for(REG int j=i;j<flag;++j){
                        ++cnt[j];
                        if(j) cnt[j]+=(cnt[j-1]>>1);
                    }
                }
        }
        printf("%d\n",ans);
    }
    getchar(),getchar();
}

int main(){Work();}


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

typedef long long ll;

inline void read(int& x){
    static char c;
    while(!isdigit(c=getchar()));x=c^48;
    while(isdigit(c=getchar()))x=(x*10)+(c^48);
}

int T;

ll n;

int m;

int cnt[105];

int _2Div[105],tot;

inline void Divide(ll num){
    tot=0;
    while(num) _2Div[tot++]=num&1,num>>=1;
}

inline void Work(){
    read(T);
    while(T--){
        memset(cnt,0,sizeof 0);
        scanf("%lld",&n);read(m);
        Divide(n);--tot;
        int ans=0,mx=0;
        ll sum=0;
        for(REG int i=1;i<=m;++i){
            int tmp;read(tmp);sum+=tmp;
            tmp=(int)log2(tmp);mx=max(mx,tmp);
            ++cnt[tmp];
        }
        if(sum<n){puts("-1");continue;}
        for(REG int i=0;i<=tot;++i){
            if(i) cnt[i]+=(cnt[i-1]>>1);
            if(_2Div[i])
                if(cnt[i]){--cnt[i];continue;}
                else{
                    int flag=0;
                    for(REG int j=i+1;j<=max(mx,tot);++j)
                        if(cnt[j]){flag=j;break;}
                    --cnt[flag];
                    ans+=flag-i;
                    for(REG int j=i;j<flag;++j) ++cnt[j];
                }
        }
        printf("%d\n",ans);
    }
    getchar(),getchar();
}

int main(){Work();}


#include <iostream>
#include <memory.h>
#include <cstdio>
#define int long long
using namespace std;
const int N = 100001;
int t, n, m, p, a[N], ans, sum, pow_2[31], cnt;
signed main()
{
	scanf("%lld", &t);
	while(t--)
	{
		ans = sum = 0;
		memset(pow_2, 0, sizeof pow_2);
		scanf("%lld%lld", &n, &m);
		for(int i = 1; i <= m; i++)
			scanf("%lld", &a[i]), sum += a[i];
		if(sum < n)
		{
			cout << "-1\n";
			continue;
		}
		for(int i = 1; i <= m; i++)
		{
			p = 0;
			while(a[i] > 1) p++, a[i] /= 2;
			pow_2[p]++;
		}
		for(int i = 0; i <= 30; i++)
		{
			if(n / (1 << i) % 2 == 1) cnt--;
			if(cnt < 0)
			{
				int now = i;
				while(!pow_2[now]) pow_2[now] = 1, now++;
				pow_2[now]--;
				ans += now - i;
			}
			cnt = (cnt > 0 ? cnt : 0) + pow_2[i] >> 1;
		}
		cout << ans << endl;
	}
	return 0;
}


#include <iostream>
#include <memory.h>
#include <cstdio>
#define int long long
using namespace std;
const int N = 100001;
int t, n, m, p, a[N], ans, sum, pow_2[31], cnt;
signed main()
{
	scanf("%lld", &t);
	while(t--)
	{
		ans = sum = 0;
		memset(pow_2, 0, sizeof pow_2);
		scanf("%lld%lld", &n, &m);
		for(int i = 1; i <= m; i++)
			scanf("%lld", &a[i]), sum += a[i];
		if(sum < n)
		{
			cout << "-1\n";
			continue;
		}
		for(int i = 1; i <= m; i++)
		{
			p = 0;
			while(a[i] > 1) p++, a[i] /= 2;
			pow_2[p]++;
		}
		cnt = 0;
		for(int i = 0; i <= 30; i++)
		{
			if(n >> i & 1) cnt--;
			if(cnt < 0)
			{
				int now = i;
				while(!pow_2[now]) pow_2[now] = 1, now++;
				pow_2[now]--;
				ans += now - i;
				cnt = 0;
			}
			cnt = cnt + pow_2[i] >> 1;
		}
		cout << ans << endl;
	}
	return 0;
}


#include <iostream>
#include <memory.h>
#include <cstdio>
#define int long long
using namespace std;
const int N = 100001;
int t, n, m, p, a[N], ans, sum, pow_2[31], cnt;
signed main()
{
	scanf("%lld", &t);
	while(t--)
	{
		ans = sum = 0;
		memset(pow_2, 0, sizeof pow_2);
		scanf("%lld%lld", &n, &m);
		for(int i = 1; i <= m; i++)
			scanf("%lld", &a[i]), sum += a[i];
		if(sum < n)
		{
			cout << "-1\n";
			continue;
		}
		for(int i = 1; i <= m; i++)
		{
			p = 0;
			while(a[i] > 1) p++, a[i] /= 2;
			pow_2[p]++;
		}
		cnt = 0;
		for(int i = 0; i <= 30; i++)
		{
			if(n >> i & 1) cnt--;
			if(cnt < 0)
			{
				int now = i;
				while(!pow_2[now]) pow_2[now] = 1, now++;
				pow_2[now]--;
				ans += now - i;
				cnt = 0;
			}
			cnt += pow_2[i] / 2;
		}
		cout << ans << endl;
	}
	return 0;
}


#include <iostream>
#include <memory.h>
#include <cstdio>
#define int long long
using namespace std;
const int N = 100001;
int t, n, m, p, a[N], ans, sum, pow_2[31], cnt;
signed main()
{
	scanf("%lld", &t);
	while(t--)
	{
		ans = sum = 0;
		memset(pow_2, 0, sizeof pow_2);
		scanf("%lld%lld", &n, &m);
		for(int i = 1; i <= m; i++)
			scanf("%lld", &a[i]), sum += a[i];
		if(sum < n)
		{
			cout << "-1\n";
			continue;
		}
		for(int i = 1; i <= m; i++)
		{
			p = 0;
			while(a[i] > 1) p++, a[i] /= 2;
			pow_2[p]++;
		}
		cnt = 0;
		for(int i = 0; i <= 30; i++)
		{
			if(n / (1 << i) % 2 == 1) cnt--;
			if(cnt < 0)
			{
				int now = i;
				while(!pow_2[now]) pow_2[now] = 1, now++;
				pow_2[now]--;
				ans += now - i;
			}
			cnt = (cnt > 0 ? cnt : 0) + pow_2[i] / 2;
		}
		cout << ans << endl;
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define ll long long
ll a[100005];
int main(int argc, char** argv) {
	int t;
	cin>>t;
	while(t--){
		ll n;

		int m;
		scanf("%lld%d",&n,&m);
		int ans=0;
		for(int i=1;i<=m;i++)scanf("%lld",&a[i]);
		sort(a+1,a+m+1);
		priority_queue<ll>q;
		for(int i=m;i>=1;i--)
		{
			if(n>=a[i])
			{
				n-=a[i];
			}
			else
			{
			q.push(-a[i]);
			}
		}

			while(!q.empty()&&n)
			{
				ll u=-q.top();
				q.pop();
				while(u>n)
				{
					u/=2;
					ans++;
				}
				if(u<=n)
				n-=u;
			}

		if(n)
		{
			printf("-1\n");
		}
		else printf("%d\n",ans);
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define ll long long
ll a[100005];
int main(int argc, char** argv) {
	int t;
	cin>>t;
	while(t--){
		ll n;

		int m;
		scanf("%lld%d",&n,&m);
		int ans=0;
		for(int i=1;i<=m;i++)scanf("%lld",&a[i]);
		priority_queue<int>q;
		for(int i=1;i<=m;i++)
		{
			if(n>=a[i])
			{
				n-=a[i];
			}
			else
			{
			q.push(-a[i]);
			}
		}

			while(!q.empty()&&n)
			{
				int u=-q.top();
				q.pop();
				while(u>n)
				{
					u/=2;
					ans++;
				}
				if(u<=n)
				n-=u;
			}

		if(n)
		{
			printf("-1\n");
		}
		else printf("%d\n",ans);
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define ll long long
ll a[100005];
ll sum[100005];
int main(int argc, char** argv) {
	int t;
	cin>>t;
	while(t--){
		ll n;

		int m;
		scanf("%lld%d",&n,&m);
		int ans=0;
		for(int i=1;i<=m;i++)scanf("%lld",&a[i]);

		sum[0]=0;
		sort(a+1,a+m+1);
		for(int i=1;i<=m;i++)
		{
			sum[i]=sum[i-1]+a[i];
		}
		if(sum[m]<n)
		{
			printf("-1\n");
			continue;
		}
		/*24 5
8 8 32 32 32*/

		ll minn=a[m];
		priority_queue<ll>q;
		for(int i=m;i>=1;i--)
		{
			if(n>=a[i])
			{
				n-=a[i];
			}
			else if(sum[i-1]<n)
			{
				minn=min(minn,a[i]);
				while(sum[i-1]<n)
				{
					while(n<minn)
					{
					minn/=2;
					ans++;
					}
					n-=minn;
				}
			}
			else
			{
				minn=min(minn,a[i]);
			}
		}

			printf("%d\n",ans);
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define ll long long
ll a[100005];
ll sum[100005];
int main(int argc, char** argv) {
	int t;
	cin>>t;
	while(t--){
		ll n;

		int m;
		scanf("%lld%d",&n,&m);
		int ans=0;
		for(int i=1;i<=m;i++)scanf("%lld",&a[i]);

		sum[0]=0;
		sort(a+1,a+m+1);
		for(int i=1;i<=m;i++)
		{
			sum[i]=sum[i-1]+a[i];
		}
		if(sum[m]<n)
		{
			printf("-1\n");
			continue;
		}
		/*24 5
8 8 32 32 32*/

		ll minn=a[m];
		priority_queue<ll>q;
		for(int i=m;i>=1;i--)
		{
			if(n>=a[i])
			{
				n-=a[i];
			}
			else if(sum[i-1]<n)
			{
				while(sum[i-1]<n)
				{
					while(n<minn)
					{
					minn/=2;
					ans++;
					}
					n-=minn;
				}
			}
			else
			{
				minn=min(minn,a[i]);
			}
		}

			printf("%d\n",ans);
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define ll long long
ll a[100005];
int main(int argc, char** argv) {
	int t;
	cin>>t;
	while(t--){
		ll n;

		int m;
		scanf("%lld%d",&n,&m);
		int ans=0;
		for(int i=1;i<=m;i++)scanf("%lld",&a[i]);
		priority_queue<ll>q;
		for(int i=1;i<=m;i++)
		{
			if(n>=a[i])
			{
				n-=a[i];
			}
			else
			{
			q.push(-a[i]);
			}
		}

			while(!q.empty()&&n)
			{
				ll u=-q.top();
				q.pop();
				while(u>n)
				{
					u/=2;
					ans++;
				}
				if(u<=n)
				n-=u;
			}

		if(n)
		{
			printf("-1\n");
		}
		else printf("%d\n",ans);
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define ll long long
ll a[100005];
int main(int argc, char** argv) {
	int t;
	cin>>t;
	while(t--){
		ll n;

		int m;
		scanf("%lld%d",&n,&m);
		int ans=0;
		for(int i=1;i<=m;i++)scanf("%lld",&a[i]);
		ll sum=0;
		for(int i=1;i<=m;i++)
		{
			sum+=a[i];
		}
		if(sum<n)
		{
			printf("-1\n");
			continue;
		}
		sort(a+1,a+m+1);
		ll minn=1e9;
		priority_queue<ll>q;
		for(int i=m;i>=1;i--)
		{
			if(n>=a[i])
			{
				n-=a[i];
			}
			else
			{
			minn=min(minn,a[i]);
			}
		}

			while(n)
			{
				while(minn>n)
				{
					minn/=2;
					ans++;
				}
				n-=minn;
			}

		if(n)
		{
			printf("-1\n");
		}
		else printf("%d\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

string getDig(long long int n){
    string s="";
    while(n>0){
        s.push_back((char)(n%2+48));
        n=n/2;
    }
    return s;
}

long long int getMinDiv(long long int x, long long int m){
    string digX=getDig(x);
    string digM=getDig(m);
    long long int j=0;
    long long int c=0;
    for(long long int i=0;i<digX.length();i++){
        if(digX[i]=='1'){
            long long int ctr=0;
            while(j<digM.length()){
                if(digM[j]=='1')
                    break;
                j++;
                ctr++;
            }
            c=c+ctr;
        }
        j++;
    }
    return c-1;
}

int main(){
    long long int t,n,m,x;
    cin>>t;
    for(long long int i=0;i<t;i++){
        cin>>m>>n;
        long long int s=0;
        for(long long int j=0;j<n;j++){
            cin>>x;
            s=s+x;
        }

        if(s<m){
            cout<<"-1\n";
        }else{
            cout<<getMinDiv(x,m)<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    long long int t,m,n,x;
    cin>>t;
    for(long long int i=0;i<t;i++){
        cin>>m>>n;
        long long int s=0;
        long long int arr[n];
        for(long long int j=0;j<n;j++){
            cin>>arr[j];
        }
        sort(arr,arr+n);
        long long int cnt=0,ctr=0;
        vector<pair<long long int,long long int> > notPossible;
        for(long long int j=0;j<n;j++){
            if(s<arr[j]){
                notPossible.push_back(make_pair(s+1,arr[j]-1));
            }
            s=s+arr[j];
            if(s>=m){
                cnt=1;
                for(long long int k=0;k<notPossible.size();k++){
                    if(m>=notPossible[k].first && m<=notPossible[k].second){
                        long long int v=notPossible[k].second+1;
                        while(v>0){
                            if(v+notPossible[k].first-1 <=m){
                                break;
                            }
                            ctr++;
                            v=v/2;
                        }
                        break;
                    }
                }
                break;
            }
        }
        if(cnt==0){
            cout<<"-1\n";
        }else{
            cout<<ctr<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
string getDig(long long int n){
    string s="";
    while(n>0){
        s.push_back((char)(n%2+48));
        n=n/2;
    }
    return s;
}

long long int getDivs(long long int m, long long int * bin, long long int binSize){
    string digM=getDig(m);
    long long int ctr=0;
    for(long long int i=0;i<digM.length();i++){
        if(digM[i]=='1'){
            long long int j=i,req=1,cnt=0;
            while(1){
                if(bin[j]>=req){
                    break;
                }
                if(j==0){
                    cnt=1;
                    break;
                }
                req=2*(req-bin[j]);
                j=j/2;
            }
            if(cnt==0){
                j=i;
                req=1;
                while(1){
                    if(bin[j]>=req){
                        bin[j]=bin[j]-req;
                        break;
                    }
                    req=2*(req-bin[j]);
                    bin[j]=0;
                    j=j/2;
                }
            }else{
                j=i+1;
                while(j<binSize){
                    if(bin[j]>0){
                        bin[j]--;
                        j--;
                        ctr++;
                        while(j>i){
                            bin[j]++;
                            j--;
                            ctr++;
                        }
                        break;
                    }
                    j++;
                }
            }
        }
    }
    return ctr;
}

long long int binC(long long int x){
    long long int ctr=0;
    while(x>0){
        x=x/2;
        ctr++;
    }

    return ctr-1;
}

int main(){
    long long int t,m,n,x;
    cin>>t;
    for(long long int i=0;i<t;i++){
        cin>>m>>n;
        long long int s=0;
        long long int bin[50]={0};
        long long int binSize=0;
        for(long long int j=0;j<n;j++){
            cin>>x;
            s=s+x;
            long long int binCount = binC(x);
            binSize=max(binSize,binCount);
            bin[binCount]++;
        }
        if(s<m){
            cout<<"-1\n";
        }else{
            cout<<getDivs(m,bin,binSize+1)<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
string getDig(long long int n){
    string s="";
    while(n>0){
        s.push_back((char)(n%2+48));
        n=n/2;
    }
    return s;
}

long long int getDivs(long long int m, long long int * bin, long long int binSize){
    string digM=getDig(m);
    long long int ctr=0;
    for(long long int i=0;i<digM.length();i++){
        if(digM[i]=='1'){
            long long int j=i,req=1,cnt=0;
            while(1){
                if(bin[j]>=req){
                    break;
                }
                if(j==0){
                    cnt=1;
                    break;
                }
                req=2*(req-bin[j]);
                j=j/2;
            }
            if(cnt==0){
                j=i;
                req=1;
                while(1){
                    if(bin[j]>=req){
                        bin[j]=bin[j]-req;
                        break;
                    }
                    req=2*(req-bin[j]);
                    bin[j]=0;
                    j=j/2;
                }
            }else{
                j=i+1;
                long long int cnt2=0;
                while(j<binSize){
                    if(bin[j]>0){
                        bin[j]--;
                        j--;
                        ctr++;
                        while(j>i){
                            bin[j]++;
                            j--;
                            ctr++;
                        }
                        bin[j]++;
                        cnt2=1;
                        break;
                    }
                    j++;
                }
                if(cnt2==0)
                    return -1;
            }
        }
    }
    return ctr;
}

long long int binC(long long int x){
    long long int ctr=0;
    while(x>0){
        x=x/2;
        ctr++;
    }

    return ctr-1;
}

int main(){
    long long int t,m,n,x;
    cin>>t;
    for(long long int i=0;i<t;i++){
        cin>>m>>n;
        long long int s=0;
        long long int bin[100]={0};
        long long int binSize=0;
        for(long long int j=0;j<n;j++){
            cin>>x;
            s=s+x;
            long long int binCount = binC(x);

            bin[binCount]++;
        }
        if(s<m){
            cout<<"-1\n";
        }else{
            binSize = getDig(s).length()+1;
            cout<<getDivs(m,bin,binSize)<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

string getDig(long long int n){
    string s="";
    while(n>0){
        s.push_back((char)(n%2+48));
        n=n/2;
    }
    return s;
}

long long int getMinDiv(long long int x, long long int m){
    string digX=getDig(x);
    string digM=getDig(m);
    long long int j=0;
    long long int c=0;
    //cout<<digX<<" \n";
    //cout<<digM<<"\n";
    for(long long int i=0;i<digM.length();i++){
        if(digM[i]=='1'){
            while(j<digX.length()){
                if(digX[j]=='1'){
                    c=c+j-i;
                    break;
                }
                j++;
            }
            //c=c+ctr;
        }
        j++;
    }
    return c;
}

int main(){
    long long int t,n,m,x;
    cin>>t;
    for(long long int i=0;i<t;i++){
        cin>>m>>n;
        long long int s=0;
        for(long long int j=0;j<n;j++){
            cin>>x;
            s=s+x;
        }

        if(s<m){
            cout<<"-1\n";
        }else{
            cout<<getMinDiv(s,m)<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
string getDig(long long int n){
    string s="";
    while(n>0){
        s.push_back((char)(n%2+48));
        n=n/2;
    }
    return s;
}

long long int getDivs(long long int m, long long int * bin, long long int binSize){
    string digM=getDig(m);
    long long int ctr=0;
    for(long long int i=0;i<digM.length();i++){
        if(digM[i]=='1'){
            long long int j=i,req=1,cnt=0;
            while(1){
                if(bin[j]>=req){
                    break;
                }
                if(j==0){
                    cnt=1;
                    break;
                }
                req=2*(req-bin[j]);
                j=j/2;
            }
            if(cnt==0){
                j=i;
                req=1;
                while(1){
                    if(bin[j]>=req){
                        bin[j]=bin[j]-req;
                        break;
                    }
                    req=2*(req-bin[j]);
                    bin[j]=0;
                    j=j/2;
                }
            }else{
                j=i+1;
                long long int cnt2=0;
                while(j<binSize){
                    if(bin[j]>0){
                        bin[j]--;
                        j--;
                        ctr++;
                        while(j>i){
                            bin[j]++;
                            j--;
                            ctr++;
                        }
                        bin[j]++;
                        cnt2=1;
                        break;
                    }
                    j++;
                }
                if(cnt2==0)
                    return -1;
            }
        }
    }
    return ctr;
}

long long int binC(long long int x){
    long long int ctr=0;
    while(x>0){
        x=x/2;
        ctr++;
    }

    return ctr-1;
}

int main(){
    long long int t,m,n,x;
    cin>>t;
    for(long long int i=0;i<t;i++){
        cin>>m>>n;
        long long int s=0;
        long long int bin[50]={0};
        long long int binSize=0;
        for(long long int j=0;j<n;j++){
            cin>>x;
            s=s+x;
            long long int binCount = binC(x);
            binSize=max(binSize,binCount);
            bin[binCount]++;
        }
        if(s<m){
            cout<<"-1\n";
        }else{
            cout<<getDivs(m,bin,binSize+1)<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
string getDig(long long int n){
    string s="";
    while(n>0){
        s.push_back((char)(n%2+48));
        n=n/2;
    }
    return s;
}

long long int getDivs(long long int m, long long int * bin, long long int binSize){
    string digM=getDig(m);
    long long int ctr=0;
    for(long long int i=0;i<digM.length();i++){
        if(digM[i]=='1'){
            long long int j=i,req=1,cnt=0;
            while(1){
                if(bin[j]>=req){
                    break;
                }
                if(j==0){
                    cnt=1;
                    break;
                }
                req=2*(req-bin[j]);
                j--;
            }
            if(cnt==0){
                j=i;
                req=1;
                while(1){
                    if(bin[j]>=req){
                        bin[j]=bin[j]-req;
                        break;
                    }
                    req=2*(req-bin[j]);
                    bin[j]=0;
                    j=j/2;
                }
            }else{
                j=i+1;
                long long int cnt2=0;
                while(j<binSize){
                    if(bin[j]>0){
                        bin[j]--;
                        j--;
                        ctr++;
                        while(j>i){
                            bin[j]++;
                            j--;
                            ctr++;
                        }
                        bin[j]++;
                        cnt2=1;
                        break;
                    }
                    j++;
                }
                if(cnt2==0){
                    //cout<<i<<"----\n";
                    return -1;
                }
            }
        }
    }
    return ctr;
}

long long int binC(long long int x){
    long long int ctr=0;
    while(x>0){
        x=x/2;
        ctr++;
    }

    return ctr-1;
}

int main(){
    long long int t,m,n,x;
    cin>>t;
    for(long long int i=0;i<t;i++){
        cin>>m>>n;
        long long int s=0;
        long long int bin[100]={0};
        long long int binSize=0;
        for(long long int j=0;j<n;j++){
            cin>>x;
            s=s+x;
            long long int binCount = binC(x);

            bin[binCount]++;
        }
        if(s<m){
            cout<<"-1\n";
        }else{
            binSize = getDig(s).length()+1;
            cout<<getDivs(m,bin,binSize)<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
string getDig(long long int n){
    string s="";
    while(n>0){
        s.push_back((char)(n%2+48));
        n=n/2;
    }
    return s;
}

long long int getDivs(long long int m, long long int * bin, long long int binSize){
    string digM=getDig(m);
    long long int ctr=0;
    for(long long int i=0;i<digM.length();i++){
        if(digM[i]=='1'){
            long long int j=i,req=1,cnt=0;
            while(1){
                if(bin[j]>=req){
                    break;
                }
                if(j==0){
                    cnt=1;
                    break;
                }
                req=2*(req-bin[j]);
                j--;
            }
            if(cnt==0){
                j=i;
                req=1;
                while(1){
                    if(bin[j]>=req){
                        bin[j]=bin[j]-req;
                        break;
                    }
                    req=2*(req-bin[j]);
                    bin[j]=0;
                    j--;
                }
            }else{
                j=i+1;
                long long int cnt2=0;
                while(j<binSize){
                    if(bin[j]>0){
                        bin[j]--;
                        j--;
                        ctr++;
                        while(j>i){
                            bin[j]++;
                            j--;
                            ctr++;
                        }
                        bin[j]++;
                        cnt2=1;
                        break;
                    }
                    j++;
                }
                if(cnt2==0){
                    //cout<<i<<"----\n";
                    return -1;
                }
            }
        }
    }
    return ctr;
}

long long int binC(long long int x){
    long long int ctr=0;
    while(x>0){
        x=x/2;
        ctr++;
    }

    return ctr-1;
}

int main(){
    long long int t,m,n,x;
    cin>>t;
    for(long long int i=0;i<t;i++){
        cin>>m>>n;
        long long int s=0;
        long long int bin[100]={0};
        long long int binSize=0;
        for(long long int j=0;j<n;j++){
            cin>>x;
            s=s+x;
            long long int binCount = binC(x);

            bin[binCount]++;
        }
        if(s<m){
            cout<<"-1\n";
        }else{
            binSize = getDig(s).length()+1;
            cout<<getDivs(m,bin,binSize)<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
string getDig(long long int n){
    string s="";
    while(n>0){
        s.push_back((char)(n%2+48));
        n=n/2;
    }
    return s;
}

long long int getDivs(long long int m, long long int * bin, long long int binSize){
    string digM=getDig(m);
    long long int ctr=0;
    for(long long int i=0;i<digM.length();i++){
        if(digM[i]=='1'){
            long long int j=i,req=1,cnt=0;
            while(1){
                if(bin[j]>=req){
                    break;
                }
                if(j==0){
                    cnt=1;
                    break;
                }
                req=2*(req-bin[j]);
                j=j/2;
            }
            if(cnt==0){
                j=i;
                req=1;
                while(1){
                    if(bin[j]>=req){
                        bin[j]=bin[j]-req;
                        break;
                    }
                    req=2*(req-bin[j]);
                    bin[j]=0;
                    j=j/2;
                }
            }else{
                j=i;
                while(j<binSize){
                    if(bin[j]>0){
                        bin[j]--;
                        j--;
                        ctr++;
                        while(j>i){
                            bin[j]++;
                            j--;
                            ctr++;
                        }
                        break;
                    }
                    j++;
                }
            }
        }
    }
    return ctr;
}

long long int binC(long long int x){
    long long int ctr=0;
    while(x>0){
        x=x/2;
        ctr++;
    }

    return ctr-1;
}

int main(){
    long long int t,m,n,x;
    cin>>t;
    for(long long int i=0;i<t;i++){
        cin>>m>>n;
        long long int s=0;
        long long int bin[50]={0};
        long long int binSize=0;
        for(long long int j=0;j<n;j++){
            cin>>x;
            s=s+x;
            long long int binCount = binC(x);
            binSize=max(binSize,binCount);
            bin[binCount]++;
        }
        if(s<m){
            cout<<"-1\n";
        }else{
            cout<<getDivs(m,bin,binSize+1)<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
string getDig(long long int n){
    string s="";
    while(n>0){
        s.push_back((char)(n%2+48));
        n=n/2;
    }
    return s;
}

long long int getDivs(long long int m, long long int * bin, long long int binSize){
    string digM=getDig(m);
    long long int ctr=0;
    for(long long int i=0;i<digM.length();i++){
        if(digM[i]=='1'){
            long long int j=i,req=1,cnt=0;
            while(1){
                if(bin[j]>=req){
                    break;
                }
                if(j==0){
                    cnt=1;
                    break;
                }
                req=2*(req-bin[j]);
                j=j/2;
            }
            if(cnt==0){
                j=i;
                req=1;
                while(1){
                    if(bin[j]>=req){
                        bin[j]=bin[j]-req;
                        break;
                    }
                    req=2*(req-bin[j]);
                    bin[j]=0;
                    j=j/2;
                }
            }else{
                j=i+1;
                while(j<binSize){
                    if(bin[j]>0){
                        bin[j]--;
                        j--;
                        ctr++;
                        while(j>i){
                            bin[j]++;
                            j--;
                            ctr++;
                        }
                        bin[j]++;
                        break;
                    }
                    j++;
                }
            }
        }
    }
    return ctr;
}

long long int binC(long long int x){
    long long int ctr=0;
    while(x>0){
        x=x/2;
        ctr++;
    }

    return ctr-1;
}

int main(){
    long long int t,m,n,x;
    cin>>t;
    for(long long int i=0;i<t;i++){
        cin>>m>>n;
        long long int s=0;
        long long int bin[50]={0};
        long long int binSize=0;
        for(long long int j=0;j<n;j++){
            cin>>x;
            s=s+x;
            long long int binCount = binC(x);
            binSize=max(binSize,binCount);
            bin[binCount]++;
        }
        if(s<m){
            cout<<"-1\n";
        }else{
            cout<<getDivs(m,bin,binSize+1)<<"\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define INF 2147483647
#define mod 1000000007
#define lowbit(n) n&-n
#define mem(x) memset(x,0,sizeof(x))
#define fo(i,a,b) for(int i=a,_e=b;i<=_e;++i)
#define fd(i,a,b) for(int i=b,_e=a;i>=_e;--i)
#define ff(i,a,b) for(int i=a,_e=b;i<_e;++i)
#define Cout(a,l,r) fo(i,l,r) cout<<a[i]<<' ';cout<<endl
using namespace std;
const int maxn=32;
typedef long long ll;
typedef long double ld;
ll n,m;

ll pre[maxn],c[maxn],a[200010],vis[maxn];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);

	int T;cin>>T;
	while(T--){
		cin>>n>>m;
		ll sum=0;
		fo(i,1,m){
			cin>>a[i];
			int cnt=0;
			while(a[i]>1) a[i]>>=1,cnt++,sum+=a[i];
			c[cnt]++;
		}
		if(sum<n){
			cout<<"-1"<<endl;
			continue;
		}
		ll i,j;
		for(i=1,j=0;i<=n;i<<=1,j++){
			if( i&n ){
				vis[j]=1;
			}
		}
		j--;

		pre[0]=c[0];
		fo(k,1,32){
			pre[k]=pre[k-1]+c[k]*(1ll<<k);
			//cout<<pre[k]<<endl;
		}
		int ans=0;
		ll now=0;
		fo(k,0,j){
			if(vis[k]==1){
				now+=(1ll<<k);
				if(pre[k]<now){
					int t=k;
					k++;
					while(!c[k]) k++;
					//cout<<k<<' '<<t<<endl;
					ans+=(k-t);
				}
			}
		}
		cout<<ans<<endl;
		mem(pre);mem(vis);mem(c);
		fo(i,0,m) a[i]=0;
	}

	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define INF 2147483647
#define mod 1000000007
#define lowbit(n) n&-n
#define mem(x) memset(x,0,sizeof(x))
#define fo(i,a,b) for(int i=a,_e=b;i<=_e;++i)
#define fd(i,a,b) for(int i=b,_e=a;i>=_e;--i)
#define ff(i,a,b) for(int i=a,_e=b;i<_e;++i)
#define Cout(a,l,r) fo(i,l,r) cout<<a[i]<<' ';cout<<endl
using namespace std;
const int maxn=65;
typedef long long ll;
typedef long double ld;
ll n,m;

ll pre[maxn],c[maxn],a[200010],vis[maxn];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);

	int T;cin>>T;
	while(T--){
		cin>>n>>m;
		ll sum=0;
		fo(i,1,m){
			cin>>a[i];
			sum+=a[i];
			int cnt=0;
			while(a[i]>1) a[i]>>=1,cnt++;
			c[cnt]++;
		}
		if(sum<n){
			cout<<"-1"<<endl;
			mem(pre);mem(vis);mem(c);
			continue;
		}
		ll i,j;
		for(i=1,j=0;i<=n;i<<=1,j++){
			if( (i&n)!=0 ){
				vis[j]=1;
			}
		}
		j--;
		fd(i,0,j){
			//cout<<vis[i];
		}
		//cout<<endl;

		pre[0]=c[0];
		fo(k,1,maxn-1){
			pre[k]=pre[k-1]+c[k]*(1ll<<k);
			//cout<<pre[k]<<endl;
		}
		int ans=0;
		ll now=0;
		fo(k,0,j){
			if(vis[k]==1){
				now+=(1ll<<k);
				if(pre[k]<now){
					int t=k;
					k++;
					while(!c[k]) k++;
					//cout<<k<<' '<<t<<endl;
					ans+=(k-t);
					k--;
				}
			}
		}
		cout<<ans<<endl;
		mem(pre);mem(vis);mem(c);
		fo(i,0,m) a[i]=0;
	}

	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define INF 2147483647
#define mod 1000000007
#define lowbit(n) n&-n
#define mem(x) memset(x,0,sizeof(x))
#define fo(i,a,b) for(int i=a,_e=b;i<=_e;++i)
#define fd(i,a,b) for(int i=b,_e=a;i>=_e;--i)
#define ff(i,a,b) for(int i=a,_e=b;i<_e;++i)
#define Cout(a,l,r) fo(i,l,r) cout<<a[i]<<' ';cout<<endl
using namespace std;
const int maxn=33;
typedef long long ll;
typedef long double ld;
ll n,m;

ll pre[maxn],c[maxn],a[200010],vis[maxn];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);

	int T;cin>>T;
	while(T--){
		cin>>n>>m;
		ll sum=0;
		fo(i,1,m){
			cin>>a[i];
			sum+=a[i];
			int cnt=0;
			while(a[i]>1) a[i]>>=1,cnt++;
			c[cnt]++;
		}
		if(sum<n){
			cout<<"-1"<<endl;
			continue;
		}
		ll i,j;
		for(i=1,j=0;i<=n;i<<=1,j++){
			if( i&n ){
				vis[j]=1;
			}
		}
		j--;

		pre[0]=c[0];
		fo(k,1,32){
			pre[k]=pre[k-1]+c[k]*(1ll<<k);
			//cout<<pre[k]<<endl;
		}
		int ans=0;
		ll now=0;
		fo(k,0,j){
			if(vis[k]==1){
				now+=(1ll<<k);
				if(pre[k]<now){
					int t=k;
					k++;
					while(!c[k]) k++;
					//cout<<k<<' '<<t<<endl;
					ans+=(k-t);
				}
			}
		}
		cout<<ans<<endl;
		mem(pre);mem(vis);mem(c);
		fo(i,0,m) a[i]=0;
	}

	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define INF 2147483647
#define mod 1000000007
#define lowbit(n) n&-n
#define mem(x) memset(x,0,sizeof(x))
#define fo(i,a,b) for(int i=a,_e=b;i<=_e;++i)
#define fd(i,a,b) for(int i=b,_e=a;i>=_e;--i)
#define ff(i,a,b) for(int i=a,_e=b;i<_e;++i)
#define Cout(a,l,r) fo(i,l,r) cout<<a[i]<<' ';cout<<endl
using namespace std;
const int maxn=33;
typedef long long ll;
typedef long double ld;
ll n,m;

ll pre[maxn],c[maxn],a[200010],vis[maxn];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);

	int T;cin>>T;
	while(T--){
		cin>>n>>m;
		ll sum=0;
		fo(i,1,m){
			cin>>a[i];
			int cnt=0;
			while(a[i]>1) a[i]>>=1,cnt++,sum+=a[i];
			c[cnt]++;
		}
		if(sum<n){
			cout<<"-1"<<endl;
			continue;
		}
		ll i,j;
		for(i=1,j=0;i<=n;i<<=1,j++){
			if( i&n ){
				vis[j]=1;
			}
		}
		j--;

		pre[0]=c[0];
		fo(k,1,32){
			pre[k]=pre[k-1]+c[k]*(1ll<<k);
			//cout<<pre[k]<<endl;
		}
		int ans=0;
		ll now=0;
		fo(k,0,j){
			if(vis[k]==1){
				now+=(1ll<<k);
				if(pre[k]<now){
					int t=k;
					k++;
					while(!c[k]) k++;
					//cout<<k<<' '<<t<<endl;
					ans+=(k-t);
				}
			}
		}
		cout<<ans<<endl;
		mem(pre);mem(vis);mem(c);
		fo(i,0,m) a[i]=0;
	}

	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define INF 2147483647
#define mod 1000000007
#define lowbit(n) n&-n
#define mem(x) memset(x,0,sizeof(x))
#define fo(i,a,b) for(int i=a,_e=b;i<=_e;++i)
#define fd(i,a,b) for(int i=b,_e=a;i>=_e;--i)
#define ff(i,a,b) for(int i=a,_e=b;i<_e;++i)
#define Cout(a,l,r) fo(i,l,r) cout<<a[i]<<' ';cout<<endl
using namespace std;
const int maxn=33;
typedef long long ll;
typedef long double ld;
ll n,m;

ll pre[maxn],c[maxn],a[200010],vis[maxn];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);

	int T;cin>>T;
	while(T--){
		cin>>n>>m;
		ll sum=0;
		fo(i,1,m){
			cin>>a[i];
			sum+=a[i];
			int cnt=0;
			while(a[i]>1) a[i]>>=1,cnt++;
			c[cnt]++;
		}
		if(sum<n){
			cout<<"-1"<<endl;
			mem(pre);mem(vis);mem(c);
			continue;
		}
		ll i,j;
		for(i=1,j=0;i<=n;i<<=1,j++){
			if( (i&n)!=0 ){
				vis[j]=1;
			}
		}
		j--;
		fd(i,0,j){
			//cout<<vis[i];
		}
		//cout<<endl;

		pre[0]=c[0];
		fo(k,1,32){
			pre[k]=pre[k-1]+c[k]*(1ll<<k);
			//cout<<pre[k]<<endl;
		}
		int ans=0;
		ll now=0;
		fo(k,0,j){
			if(vis[k]==1){
				now+=(1ll<<k);
				if(pre[k]<now){
					int t=k;
					k++;
					while(!c[k]) k++;
					//cout<<k<<' '<<t<<endl;
					ans+=(k-t);
					k--;
				}
			}
		}
		cout<<ans<<endl;
		mem(pre);mem(vis);mem(c);
		fo(i,0,m) a[i]=0;
	}

	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define INF 2147483647
#define mod 1000000007
#define lowbit(n) n&-n
#define mem(x) memset(x,0,sizeof(x))
#define fo(i,a,b) for(int i=a,_e=b;i<=_e;++i)
#define fd(i,a,b) for(int i=b,_e=a;i>=_e;--i)
#define ff(i,a,b) for(int i=a,_e=b;i<_e;++i)
#define Cout(a,l,r) fo(i,l,r) cout<<a[i]<<' ';cout<<endl
using namespace std;
const int maxn=33;
typedef long long ll;
typedef long double ld;
ll n,m;

ll pre[maxn],c[maxn],a[200010],vis[maxn];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);

	int T;cin>>T;
	while(T--){
		cin>>n>>m;
		ll sum=0;
		fo(i,1,m){
			cin>>a[i];
			sum+=a[i];
			int cnt=0;
			while(a[i]>1) a[i]>>=1,cnt++;
			c[cnt]++;
		}
		if(sum<n){
			cout<<"-1"<<endl;
			mem(pre);mem(vis);mem(c);
			continue;
		}
		ll i,j;
		for(i=1,j=0;i<=n;i<<=1,j++){
			if( i&n ){
				vis[j]=1;
			}
		}
		j--;

		pre[0]=c[0];
		fo(k,1,32){
			pre[k]=pre[k-1]+c[k]*(1ll<<k);
			//cout<<pre[k]<<endl;
		}
		int ans=0;
		ll now=0;
		fo(k,0,j){
			if(vis[k]==1){
				now+=(1ll<<k);
				if(pre[k]<now){
					int t=k;
					k++;
					while(!c[k]) k++;
					//cout<<k<<' '<<t<<endl;
					ans+=(k-t);
				}
			}
		}
		cout<<ans<<endl;
		mem(pre);mem(vis);mem(c);
		fo(i,0,m) a[i]=0;
	}

	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;

int main()
{

    int t;
    cin>>t;
    while(t--)
    {
        ll a,n;
        cin>>a>>n;
        int arr[61]={0};
        ll temp;
        ll prev=0;
        for(int i=0;i<n;i++){
            cin>>temp;
            prev+=temp;
            int x=0;
            while(temp){
                x++;
                temp=temp>>1;
            }
            arr[x-1]++;
        }
        if(prev<a){
            cout<<"-1\n";
            continue;
        }
        temp=0;
        prev=0;
        ll res=0;
        while(a){
            if(a%2){
                if(arr[temp]){
                    arr[temp]--;
                }else if(prev>=(1<<temp)){
                    prev-=1<<temp;
                }else{
                    arr[temp]++;
                    int i;
                    for(i=temp+1;i<61&&arr[i]==0;i++){
                        arr[i]++;
                        res++;
                    }
                    if(i==61){
                        res=-1;
                        break;
                    }else{
                        res++;
                        arr[i]--;
                    }
                }
            }
            if(temp<32)
            prev+=(1<<temp)*arr[temp];
            a>>=1;
            temp++;
        }
        cout<<res<<endl;

    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{

    int t;
    cin>>t;
    while(t--)
    {
        ll a,n;
        cin>>a>>n;
        int arr[30]={0};
        ll temp;
        ll prev=0;
        for(int i=0;i<n;i++){
            cin>>temp;
            prev+=temp;
            int x=0;
            while(temp){
                x++;
                temp=temp>>1;
            }
            arr[x-1]++;
        }
        if(prev<a){
            cout<<"-1\n";
            continue;
        }
        temp=0;
        prev=0;
        ll res=0;
        while(a){
            if(a%2){
                if(temp<30&&arr[temp]){
                    arr[temp]--;
                }else if(prev>=(1<<temp)){
                    prev-=1<<temp;
                }else{
                    if(temp>=30){
                        res=-1;
                        break;

                    }
                    arr[temp]++;
                    int i;
                    for(i=temp+1;i<30&&arr[i]==0;i++){
                        arr[i]++;
                        res++;
                    }
                    if(i==30){
                        res=-1;
                        break;
                    }else{
                        res++;
                        arr[i]--;
                    }
                }
            }
            prev+=(1<<temp)*arr[temp];
            a>>=1;
            temp++;
        }
        cout<<res<<endl;

    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{

    int t;
    cin>>t;
    while(t--)
    {
        ll a,n;
        cin>>a>>n;
        int arr[61]={0};
        ll temp;
        ll prev=0;
        for(int i=0;i<n;i++){
            cin>>temp;
            prev+=temp;
            int x=0;
            while(temp){
                x++;
                temp=temp>>1;
            }
            arr[x-1]++;
        }
        if(prev<a){
            cout<<"-1\n";
            continue;
        }
        temp=0;
        prev=0;
        ll res=0;
        while(a){
            if(a%2){
                if(arr[temp]){
                    arr[temp]--;
                }else if(prev>=(1<<temp)){
                    prev-=1<<temp;
                }else{
                    arr[temp]++;
                    int i;
                    for(i=temp+1;i<61&&arr[i]==0;i++){
                        arr[i]++;
                        res++;
                    }
                    if(i==61){
                        res=-1;
                        break;
                    }else{
                        res++;
                        arr[i]--;
                    }
                }
            }
            if(temp<32)
            prev+=(1<<temp)*arr[temp];
            a>>=1;
            temp++;
        }
        cout<<res<<endl;

    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;

int main()
{

    int t;
    cin>>t;
    while(t--)
    {
        ll a,n;
        cin>>a>>n;
        int arr[61]={0};
        ll temp;
        ll prev=0;
        for(int i=0;i<n;i++){
            cin>>temp;
            prev+=temp;
            int x=0;
            while(temp){
                x++;
                temp=temp>>1;
            }
            arr[x-1]++;
        }
        if(prev<a){
            cout<<"-1\n";
            continue;
        }
        temp=0;
        prev=0;
        ll res=0;
        while(a){
            if(a%2){
                if(arr[temp]){
                    arr[temp]--;
                }else if(prev>=(1<<temp)){
                    prev-=1<<temp;
                }else{
                    arr[temp]++;
                    int i;
                    for(i=temp+1;i<61&&arr[i]==0;i++){
                        arr[i]++;
                        res++;
                    }
                    if(i==61){
                        res=-1;
                        break;
                    }else{
                        res++;
                        arr[i]--;
                    }
                }
            }
            if(temp<32)
            prev+=arr[temp]*(1<<temp);
            a>>=1;
            temp++;
        }
        cout<<res<<endl;

    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{

    int t;
    cin>>t;
    while(t--)
    {
        ll a,n;
        cin>>a>>n;
        int arr[30]={0};
        ll temp;
        ll prev=0;
        for(int i=0;i<n;i++){
            cin>>temp;
            prev+=temp;
            int x=0;
            while(temp){
                x++;
                temp=temp>>1;
            }
            arr[x-1]++;
        }
        if(prev<a){
            cout<<"-1\n";
            continue;
        }
        temp=0;
        prev=0;
        ll res=0;
        while(a){
            if(a%2){
                if(arr[temp]){
                    arr[temp]--;
                }else if(prev>=(1<<temp)){
                    prev-=1<<temp;
                }else{
                    arr[temp]++;
                    int i;
                    for(i=temp+1;i<30&&arr[i]==0;i++){
                        arr[i]++;
                        res++;
                    }
                    if(i==30){
                        res=-1;
                        break;
                    }else{
                        res++;
                        arr[i]--;
                    }
                }
            }
            prev+=(1<<temp)*arr[temp];
            a>>=1;
            temp++;
        }
        cout<<res<<endl;

    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{

    int t;
    cin>>t;
    while(t--)
    {
        ll a,n;
        cin>>a>>n;
        int arr[30]={0};
        ll temp;
        for(int i=0;i<n;i++){
            cin>>temp;
            int x=0;
            while(temp){
                x++;
                temp=temp>>1;
            }
            arr[x-1]++;
        }
        temp=0;
        ll prev=0;
        ll res=0;
        while(a){
            if(a%2){
                if(arr[temp]){
                    arr[temp]--;
                }else if(prev>=(1<<temp)){
                    prev-=1<<temp;
                }else{
                    arr[temp]++;
                    int i;
                    for(i=temp+1;i<30&&arr[i]==0;i++){
                        arr[i]++;
                        res++;
                    }
                    if(i==30){
                        res=-1;
                        break;
                    }else{
                        res++;
                        arr[i]--;
                    }
                }
            }
            prev+=(1<<temp)*arr[temp];
            a>>=1;
            temp++;
        }
        cout<<res<<endl;

    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;

int main()
{

    int t;
    cin>>t;
    while(t--)
    {
        ll a,n;
        cin>>a>>n;
        ll arr[61]={0};
        ll temp;
        ll prev=0;
        for(int i=0;i<n;i++){
            cin>>temp;
            prev+=temp;
            int x=0;
            while(temp){
                x++;
                temp=temp>>1;
            }
            arr[x-1]++;
        }
        if(prev<a){
            cout<<"-1\n";
            continue;
        }
        temp=0;
        prev=0;
        ll res=0;
        while(a){
            if(a%2){
                if(arr[temp]){
                    arr[temp]--;
                }else if(prev>=(1<<temp)){
                    prev-=1<<temp;
                }else{
                    arr[temp]++;
                    int i;
                    for(i=temp+1;i<61&&arr[i]==0;i++){
                        arr[i]++;
                        res++;
                    }
                    if(i==61){
                        res=-1;
                        break;
                    }else{
                        res++;
                        arr[i]--;
                    }
                }
            }
            if(temp<32)
            prev+=arr[temp]*(1<<temp);
            a>>=1;
            temp++;
        }
        cout<<res<<endl;

    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{

    int t;
    cin>>t;
    while(t--)
    {
        ll a,n;
        cin>>a>>n;
        int arr[61]={0};
        ll temp;
        ll prev=0;
        for(int i=0;i<n;i++){
            cin>>temp;
            prev+=temp;
            int x=0;
            while(temp){
                x++;
                temp=temp>>1;
            }
            arr[x-1]++;
        }
        if(prev<a){
            cout<<"-1\n";
            continue;
        }
        temp=0;
        prev=0;
        ll res=0;
        while(a){
            if(a%2){
                if(arr[temp]){
                    arr[temp]--;
                }else if(prev>=(1<<temp)){
                    prev-=1<<temp;
                }else{
                    arr[temp]++;
                    int i;
                    for(i=temp+1;i<61&&arr[i]==0;i++){
                        arr[i]++;
                        res++;
                    }
                    if(i==61){
                        res=-1;
                        break;
                    }else{
                        res++;
                        arr[i]--;
                    }
                }
            }
            prev+=(1<<temp)*arr[temp];
            a>>=1;
            temp++;
        }
        cout<<res<<endl;

    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{

    int t;
    cin>>t;
    while(t--)
    {
        ll a,n;
        cin>>a>>n;
        int arr[61]={0};
        ll temp;
        ll prev=0;
        for(int i=0;i<n;i++){
            cin>>temp;
            prev+=temp;
            int x=0;
            while(temp){
                x++;
                temp=temp>>1;
            }
            arr[x-1]++;
        }
        if(prev<a){
            cout<<"-1\n";
            continue;
        }
        temp=0;
        prev=0;
        ll res=0;
        while(a){
            if(a%2){
                if(arr[temp]){
                    arr[temp]--;
                }else if(prev>=(1<<temp)){
                    prev-=1<<temp;
                }else{
                    arr[temp]++;
                    int i;
                    for(i=temp+1;i<61&&arr[i]==0;i++){
                        arr[i]++;
                        res++;
                    }
                    if(i==61){
                        res=-1;
                        break;
                    }else{
                        res++;
                        arr[i]--;
                    }
                }
            if(temp<32)
            prev+=(1<<temp)*arr[temp];
            a>>=1;
            temp++;
        }
        cout<<res<<endl;

    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll  long long
using namespace std;

int main()
{

    int t;
    cin>>t;
    while(t--)
    {
        ll a,n;
        cin>>a>>n;
        ll arr[61]= {0};
        ll temp;
        ll prev=0;
        for(int i=0; i<n; i++)
        {
            cin>>temp;
            prev+=temp;
            int x=0;
            while(temp)
            {
                x++;
                temp=temp>>1;
            }
            arr[x-1]++;
        }
        if(prev<a)
        {
            cout<<"-1\n";
            continue;
        }
        temp=0;
        prev=0;
        ll res=0;
        while(a)
        {
            if(a%2)
            {
                if(arr[temp])
                {
                    arr[temp]--;
                }
                else if(prev>=(1<<temp))
                {
                    prev-=1<<temp;
                }
                else
                {
                    arr[temp]++;
                    int i;
                    for(i=temp+1; i<61&&arr[i]==0; i++)
                    {
                        arr[i]++;
                        res++;
                    }
                    if(i==61)
                    {
                        res=-1;
                        break;
                    }
                    else
                    {
                        res++;
                        arr[i]--;
                    }
                }
            }
            if(temp<32)
                prev+=arr[temp]*(1<<temp);
            a/=2;
            temp++;
        }
        cout<<res<<endl;

    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;

int main()
{

    int t;
    cin>>t;
    while(t--)
    {
        ll a,n;
        cin>>a>>n;
        ll arr[61]= {0};
        ll temp;
        ll prev=0;
        for(int i=0; i<n; i++)
        {
            cin>>temp;
            prev+=temp;
            int x=0;
            while(temp)
            {
                x++;
                temp=temp>>1;
            }
            arr[x-1]++;
        }
        if(prev<a)
        {
            cout<<"-1\n";
            continue;
        }
        temp=0;
        prev=0;
        ll res=0;
        while(a)
        {
            if(a%2)
            {
                if(arr[temp])
                {
                    arr[temp]--;
                }
                else if(prev>=(1<<temp))
                {
                    prev-=1<<temp;
                }
                else
                {
                    arr[temp]++;
                    int i;
                    for(i=temp+1; i<61&&arr[i]==0; i++)
                    {
                        arr[i]++;
                        res++;
                    }
                    if(i==61)
                    {
                        res=-1;
                        break;
                    }
                    else
                    {
                        res++;
                        arr[i]--;
                    }
                }
            }
            if(temp<32)
                prev+=arr[temp]*(1<<temp);
            a/=2;
            temp++;
        }
        cout<<res<<endl;

    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
typedef long long ll;
ll sum[65],a[65];
ll t,n,m,ans;
int main()
{
	cin>>t;
	sum[0]=1;
	for(int i=1;i<=60;i++)
		sum[i]=sum[i-1]*2;
	while(t--)
	{
		cin>>n>>m;
		ans=0;
		ll num=0;
		memset(a,0,sizeof(a));
		for(int i=0;i<m;i++)
		{
			ll x;
			cin>>x;
			num+=x;
			for(int j=0;j<=60;j++)
			{
				if(sum[j]==x)
				{
					a[j]++;
					break;
				}

			}
		}
		if(num<n)
		{
			cout<<-1<<endl;
			continue;
		}
		for(int i=0;i<=60;i++)
		{
			if(n&((ll)1<<i))//二进制判断这一位是不是1
			{
				if(a[i]!=0)
				{
					a[i]--;
				}
				else
				{
					int k=i;
					while(!a[k])//往上找可以分成两份的一项
					{
						a[k]++;
						k++;
						ans++;
					}
					a[k]--;
				}
			}
			a[i+1]+=a[i]/2;//合并没有用到的低位项
		}
		cout<<ans<<endl;

	}
}


#include <iostream>
#include <algorithm>
#include <bitset>
#include <queue>
using namespace std;

#define lowbit(x) (x&(-x))
priority_queue<int,vector<int>,greater<int>> sheap;

int main(){
    int t;
    cin>>t;
    while(t--){
        int k,n,x,cnt=0,sum=0;
        cin>>k>>n;
        while(!sheap.empty())
        	sheap.pop();
        for(int i=0;i<n;i++)
            cin>>x,sheap.push(x),sum+=x;
        if(sum<k)
            cnt=-1;
        else
            while((!sheap.empty())&&k){
                int nw = sheap.top();
                sheap.pop();
                int nt;
                int lk = lowbit(k);
//                cout << nw <<" "<<lk<<endl;
                if(lk==nw)
                    k-=lk;
                else if(lk<nw)
                    sheap.push(nw/2),sheap.push(nw/2),cnt++;
                else if((!sheap.empty())&&lk>(nt=sheap.top()) && nt==nw){
//                	cout << nw <<" "<<nt<<" "<<k<<endl;
                    sheap.pop();
                    sheap.push(nw*2);
                }
            }
        cout<<cnt<<endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <bitset>
#include <queue>
using namespace std;

#define lowbit(x) (x&(-x))
priority_queue<int,vector<int>,greater<int>> sheap;

int main(){
    int t;
    cin>>t;
    while(t--){
        int k,n,x,cnt=0,sum=0;
        cin>>k>>n;
        for(int i=0;i<n;i++)
            cin>>x,sheap.push(x),sum+=x;
        if(sum<k)
            cnt=-1;
        else
            while(!sheap.empty()&&k){
                int nw = sheap.top();
                sheap.pop();
                int nt = sheap.top();
                int lk = lowbit(k);
                //cout << nw <<" "<<nt<<" "<<k<<endl;
                if(lk==nw)
                    k-=lk;
                else if(lk<nw)
                    sheap.push(nw/2),sheap.push(nw/2),cnt++;
                else if(lk>nt && nt==nw){
                    sheap.pop();
                    sheap.push(nw*2);
                }
            }
        cout<<cnt<<endl;
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
map<long long,long long>ma;
int T,m,a[500005];
long long n,sum,cnt[75],vis[75];
inline long long solve(long long  u){
	for(int i=0;i<=33;i++)if((u>>i)&1)vis[i]=1;
	long long re=0,flag=-1;
	for(int i=0;i<=33;i++){
		if(flag!=-1&&cnt[i]){
			cnt[i]--;
			re+=(i-flag);
			flag=-1;
		}
		if(vis[i]){
			if(cnt[i])cnt[i]--;
			else if(flag==-1)flag=i;
		}
		cnt[i+1]+=(cnt[i]>>1);
		cnt[i]&=1;
	}
	return re;
}
inline void read(int &);
int main(){
	read(T);
	for(int i=0;i<=33;i++)ma[1<<i]=i;
//	printf("%d\n",T);
	while(T--){
		memset(cnt,0,sizeof(cnt));
		memset(vis,0,sizeof(vis));
		sum=0;
		scanf("%lld%d",&n,&m);
		for(long long i=1;i<=m;i++){
			scanf("%d",&a[i]);
			cnt[ma[a[i]]]++;
			sum+=a[i];
		}
		sum-=n;
		//printf("02349857208230-");
		if(sum<0){
			printf("-1\n");
							continue;
		}
		else printf("%lld\n",solve(sum));
	}
	return 0;
}
inline void read(int &num){
	num=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')ch=getchar();
	while(ch>='0'&&ch<='9')num=(num<<1)+(num<<3)+(ch^48),ch=getchar();
}


#include<bits/stdc++.h>
#define int long long
#define ld long double
#define inf 0x7f7f7f7f
#define maxn 10000010
#define ri register int
#define il inline
#define mod 1000000009
using namespace std;
int a[maxn];
int cnt[maxn];
signed main()
{
	ios::sync_with_stdio(0);
	int t;
	cin >> t;
	while(t--)
	{
		int n,m;
		cin >> n >> m;
		int sum = 0;
		for(int i=1;i<=m;i++)
			cin >> a[i],sum+=a[i];
		if(sum<n)
		{
			cout << -1<<endl;
			continue;
		}
		for(int i=1;i<=m;i++)
		{
			int k=0;
			while(a[i])
				k++,a[i]>>=1;
			cnt[k-1]++;
		}
		int tmp = 0,ans = 0;
		for(int i=0;i<32;i++)
		{
			if(n>>i&1)
				tmp--;//剩的数不够用了
			if(tmp<0)//不够用了,需要分解
			{
				int j=i;
				for(;!cnt[j];j++)//找一个可分解的
					cnt[j] = 1;//大的分解给2个小的,一个用掉还剩一个
				cnt[j]--;//被分解的数量减一
				ans += j-i;//分解次数
				tmp = 0;//剩的数归零
			}
			tmp+= cnt[i] >> 1;//这一位剩的凑给下一位
		}
		cout << ans << endl;
	}
  	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#define ll long long
using namespace std;
const int maxn=100005;
int a[maxn];
int cnt[100];

ll work(int x)
{
    ll ans=0;
    for(int i=0;i<x;i++)
    {
        ans=(ans+cnt[i])/2;
    }
    return ans+cnt[x];
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll n,m,ans=0,sum=0;
        cin>>n>>m;
        for(int i=0;i<100;i++) cnt[i]=0;
        for(int i=0;i<maxn;i++) a[i]=0;
        for(int i=1;i<=m;i++)
        {
            cin>>a[i];
            sum+=a[i];
        }
        if(sum<n) cout<<"-1"<<endl;
        else
        {
            for(int i=1;i<=m;i++)
            {
                for(int j=0;j<=40;j++)
                {
                    if((a[i]>>j)&1)
                    {
                        cnt[j]++;
                        break;
                    }
                }
            }
            for(int i=0;i<=63;i++)
            {
                if(((n>>i)&1)==0) continue;
                if(work(i)) cnt[i]--;
                else
                {
                    for(int j=i+1;j<=63;j++)
                    {
                        if(cnt[j])
                        {
                            cnt[j]--;
                            for(int k=i;k<j;k++)
                            {
                                cnt[k]++;
                            }
                            ans+=j-i;
                            break;
                        }
                    }
                }
            }
            cout<<ans<<endl;
        }
    }
    //system("pause");
    return 0;
}