#include <bits/stdc++.h>

using namespace std;
long long t;
map<long long,long long > mp;
bool sastavi(long long a,long long k){
    if(mp[a]>=k){
        mp[a]-=k;
        return true;
    }
    else{
        if(a==1)
        return false;
        if(mp[a]>0)
            k-=mp[a];
        mp[a]=0;
        return sastavi(a/2,k*2);
    }
}
long long nadji(long long  a){
    if(mp[a]>=1){
        mp[a]-=1;
        return a;
    }
    else{
        long long x=pow(2,61);
        if(a>=x){
            return -1;
        }
        else{
            return nadji(a*2);
        }
    }
}
long long dodji(long long  a,long long  k){
    long long  br=0;
    mp[a]--;
    while(a>k){
        a=a/2;
        br++;
        mp[a]++;
    }
    return br;
}
long long  doing1(long long  n){
    for(long long  i=0;i<=62;i++){
        long long  k=pow(2,i);
        if(k>n)
            break;
        long long  l=k|n;
        if(l==n){
            if(mp[k]>0){
                mp[k]--;
                n-=k;
            }
    }
    }
    return n;
}
long long doing2(long long n){
    for(long long i=0;i<=62;i++){
        long long k=pow(2,i);
        if(k>n)
            break;
        long long l=k|n;
        long long s=1;
        if(l==n&&sastavi(k,s)){
            n-=k;
        }
    }
    return n;
}
long long  doing3(long long  n){
    long long  res=0;
    for(long long i=62;i>=0;i--){
        long long k=pow(2,i);
        long long l=k|n;
        if(l==n){
            long long a=nadji(k);
            if(a==-1){
                return -1;
            }
            else{
                n-=k;
                res+=dodji(a,k);
            }
        }
    }
    return res;
}
int  main()
{
    scanf("%lld",&t);
    t++;
    int x=0;
    while(t>1){
        t--;
        x++;
        long long  n,m;
        scanf("%lld",&n);
        scanf("%lld",&m);
        for(long long i=0;i<m;i++){
            long long a;
            scanf("%lld",&a);
            mp[a]++;
        }
        n=doing1(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing2(n);
        if(t>1){
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing3(n);
        }
        cout<<n<<endl;
        mp.clear();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long  t;
map<long long ,long long > mp;
bool sastavi(long long  a,long long  k){
    if(mp[a]>=k){
        mp[a]-=k;
        return true;
    }
    else{
        if(a==1)
        return false;
        k-=mp[a];
        mp[a]=0;
        k*=2;
        return sastavi(a/2,k);
    }
}
long long  nadji(long long  a){
    if(mp[a]>=1){
        mp[a]-=1;
        return a;
    }
    else{
        if(a>=1000000000000000000){
            return -1;
        }
        else{
            return nadji(a*2);
        }
    }
}
long long  dodji(long long  a,long long  k){
    long long  br=0;
    mp[a]--;
    while(a>k){
        a=a/2;
        br++;
        mp[a]++;
    }
    return br;
}
long long  doing1(long long  n){
    for(long long  i=0;i<62;i++){
        long long  k=1<<i;
        long long  l=k|n;
        if(l==n){
            if(mp[k]>0){
                mp[k]--;
                n-=k;
            }
        }
    }
    return n;
}
long long  doing2(long long  n){
    for(long long  i=0;i<62;i++){
        long long  k=1<<i;
        long long  l=k|n;
        if(l==n&&sastavi(k,1)){
            n-=k;
        }
    }
    return n;
}
long long  doing3(long long  n){
    long long  res=0;
    for(long long  i=61;i>=0;i--){
        long long  k=1<<i;
        long long  l=k|n;
        if(l==n){
            long long  a=nadji(l);
            if(a==-1){
                return -1;
            }
            else{
                n-=l;
                res+=dodji(a,l);
            }
        }
    }
    return res;
}
int  main()
{
    scanf("%lld",&t);
    t++;
    while(t>1){
        t--;
        long long  n,m;
        scanf("%lld",&n);
        scanf("%lld",&m);
        for(long long  i=0;i<m;i++){
            long long  a;
            scanf("%lld",&a);
            mp[a]++;
        }
        n=doing1(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing2(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing3(n);

        cout<<n<<endl;
        mp.clear();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long  t;
map<long long ,long long > mp;
int main()
{
    scanf("%lld",&t);
    while(t){
            t--;
        long long  n,m;
        scanf("%lld",&n);
        scanf("%lld",&m);
        long long  sum=0;
        for(long long  i=0;i<m;i++){
            long long  a;
            scanf("%lld",&a);
            sum+=a;
            mp[a]++;
        }
        if(sum<n){
            cout<<-1<<endl;
            continue;
        }
        long long  res=0;
        for(long long  i=0;i<64;i++){
            long long  k=pow(2,i);
            if(k>n)
                break;
            long long  l=k|n;
            if(l!=n){
                mp[k*2]+=mp[k]/2;
                continue;
            }
            else{
                if(mp[k]>0){
                    mp[k]--;
                    n-=k;
                    mp[k*2]+=mp[k]/2;
                    mp[k]=0;
                }
                else{
                    long long  s=k;
                    while(mp[s]==0){
                        s*=2;
                    }
                    mp[s]--;
                    long long  br=0;
                    while(s>k){
                        br++;
                        s/=2;
                        mp[s]++;
                    }
                    res+=br;

                }
            }
        }
        cout<<res<<endl;
        mp.clear();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long t;
map<long long,long long > mp;
bool sastavi(long long a,long long k){
    if(mp[a]>=k){
        mp[a]-=k;
        return true;
    }
    else{
        if(a==1)
        return false;
        if(mp[a]>0)
            k-=mp[a];
        mp[a]=0;
        return sastavi(a/2,k*2);
    }
}
long long nadji(long long  a){
    if(mp[a]>=1){
        mp[a]-=1;
        return a;
    }
    else{
        long long x=pow(2,61);
        if(a>=x){
            return -1;
        }
        else{
            return nadji(a*2);
        }
    }
}
long long dodji(long long  a,long long  k){
    long long  br=0;
    mp[a]--;
    while(a>k){
        a=a/2;
        br++;
        mp[a]++;
    }
    return br;
}
long long  doing1(long long  n){
    for(long long  i=0;i<=62;i++){
        long long  k=pow(2,i);
        if(k>n)
            break;
        long long  l=k|n;
        if(l==n){
            if(mp[k]>0){
                mp[k]--;
                n-=k;
            }
    }
    }
    return n;
}
long long doing2(long long n){
    for(long long i=0;i<=62;i++){
        long long k=pow(2,i);
        if(k>n)
            break;
        long long l=k|n;
        long long s=1;
        if(l==n&&sastavi(k,s)){
            n-=k;
        }
    }
    return n;
}
long long  doing3(long long  n){
    long long  res=0;
    for(long long i=62;i>=0;i--){
        long long k=pow(2,i);
        long long l=k|n;
        if(l==n){
            long long a=nadji(k);
            if(a==-1){
                return -1;
            }
            else{
                n-=k;
                res+=dodji(a,k);
            }
        }
    }
    return res;
}
int  main()
{
    scanf("%lld",&t);
    t++;
    int x=0;
    while(t>1){
        t--;
        x++;
        long long  n,m;
        scanf("%lld",&n);
        scanf("%lld",&m);
        for(long long i=0;i<m;i++){
            long long a;
            scanf("%lld",&a);
            mp[a]++;
        }
        n=doing1(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        if(t>1){
                        n=doing2(n);

        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing3(n);
        }
        cout<<n<<endl;
        mp.clear();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long t;
map<long long,long long > mp;
bool sastavi(long long a,long long k){
    if(mp[a]>=k){
        mp[a]-=k;
        return true;
    }
    else{
        if(a==1)
        return false;
        if(mp[a]>0)
            k-=mp[a];
        mp[a]=0;
        return sastavi(a/2,k*2);
    }
}
long long nadji(long long  a){
    if(mp[a]>=1){
        mp[a]-=1;
        return a;
    }
    else{
        long long x=pow(2,61);
        if(a>=x){
            return -1;
        }
        else{
            return nadji(a*2);
        }
    }
}
long long dodji(long long  a,long long  k){
    long long  br=0;
    mp[a]--;
    while(a>k){
        a=a/2;
        br++;
        mp[a]++;
    }
    return br;
}
long long  doing1(long long  n){
    for(long long  i=0;i<=62;i++){
        long long  k=pow(2,i);
        if(k>n)
            break;
        long long  l=k|n;
        if(l==n){
            if(mp[k]>0){
                mp[k]--;
                n-=k;
            }
    }
    }
    return n;
}
long long doing2(long long n){
    for(long long i=0;i<=62;i++){
        long long k=pow(2,i);
        if(k>n)
            break;
        long long l=k|n;
        long long s=1;
        if(l==n&&sastavi(k,s)){
            n-=k;
        }
    }
    return n;
}
long long  doing3(long long  n){
    long long  res=0;
    for(long long i=62;i>=0;i--){
        long long k=pow(2,i);
        long long l=k|n;
        if(l==n){
            long long a=nadji(k);
            if(a==-1){
                return -1;
            }
            else{
                n-=k;
                res+=dodji(a,k);
            }
        }
    }
    return res;
}
int  main()
{
    scanf("%lld",&t);
    t++;
    int x=0;
    while(t>1){
        t--;
        x++;
        long long  n,m;
        scanf("%lld",&n);
        scanf("%lld",&m);
        for(long long i=0;i<m;i++){
            long long a;
            scanf("%lld",&a);
            mp[a]++;
        }
        n=doing1(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing2(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing3(n);
        cout<<n<<endl;
        mp.clear();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long  t;
map<long long ,long long > mp;
bool sastavi(long long  a,long long  k){
    if(mp[a]>=k){
        mp[a]-=k;
        return true;
    }
    else{
        if(a==1)
        return false;
        k-=mp[a];
        mp[a]=0;
        k*=2;
        return sastavi(a/2,k);
    }
}
long long  nadji(long long  a){
    if(mp[a]>=1){
        mp[a]-=1;
        return a;
    }
    else{
        if(a>=1000000000000000000){
            return -1;
        }
        else{
            return nadji(a*2);
        }
    }
}
long long  dodji(long long  a,long long  k){
    long long  br=0;
    mp[a]--;
    while(a>k){
        a=a/2;
        br++;
        mp[a]++;
    }
    return br;
}
long long  doing1(long long  n){
    for(long long  i=0;i<60;i++){
        long long  k=1<<i;
        long long  l=k|n;
        if(l==n){
            if(mp[k]>0){
                mp[k]--;
                n-=k;
            }
        }
    }
    return n;
}
long long  doing2(long long  n){
    for(long long  i=0;i<60;i++){
        long long  k=1<<i;
        long long  l=k|n;
        if(l==n&&sastavi(k,1)){
            n-=k;
        }
    }
    return n;
}
long long  doing3(long long  n){
    long long  res=0;
    for(long long  i=60;i>=0;i--){
        long long  k=1<<i;
        long long  l=k|n;
        if(l==n){
            long long  a=nadji(l);
            if(a==-1){
                return -1;
            }
            else{
                n-=l;
                res+=dodji(a,l);
            }
        }
    }
    return res;
}
int  main()
{
    scanf("%lld",&t);
    t++;
    while(t>1){
        t--;
        long long  n,m;
        scanf("%lld",&n);
        scanf("%lld",&m);
        for(long long  i=0;i<m;i++){
            long long  a;
            scanf("%lld",&a);
            mp[a]++;
        }
        n=doing1(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing2(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing3(n);

        cout<<n<<endl;
        mp.clear();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long  t;
map<long long ,long long > mp;
bool sastavi(long long  a,long long  k){
    if(mp[a]>=k){
        mp[a]-=k;
        return true;
    }
    else{
        if(a==1)
        return false;
        k-=mp[a];
        mp[a]=0;
        k*=2;
        return sastavi(a/2,k);
    }
}
long long  nadji(long long  a){
    if(mp[a]>=1){
        mp[a]-=1;
        return a;
    }
    else{
        if(a>=1000000000000000000){
            return -1;
        }
        else{
            return nadji(a*2);
        }
    }
}
long long  dodji(long long  a,long long  k){
    long long  br=0;
    mp[a]--;
    while(a>k){
        a=a/2;
        br++;
        mp[a]++;
    }
    return br;
}
long long  doing1(long long  n){
    for(long long  i=0;i<60;i++){
        long long  k=1<<i;
        long long  l=k|n;
        if(l==n){
            if(mp[k]>0){
                mp[k]--;
                n-=k;
            }
        }
    }
    return n;
}
long long  doing2(long long  n){
    for(long long  i=0;i<60;i++){
        long long  k=1<<i;
        long long  l=k|n;
        if(l==n&&sastavi(k,1)){
            n-=k;
        }
    }
    return n;
}
long long  doing3(long long  n){
    long long  res=0;
    for(long long  i=60;i>=0;i--){
        long long  k=1<<i;
        long long  l=k|n;
        if(l==n){
            long long  a=nadji(k);
            if(a==-1){
                return -1;
            }
            else{
                n-=k;
                res+=dodji(a,k);
            }
        }
    }
    return res;
}
int  main()
{
    scanf("%lld",&t);
    t++;
    while(t>1){
        t--;
        long long  n,m;
        scanf("%lld",&n);
        scanf("%lld",&m);
        for(long long  i=0;i<m;i++){
            long long  a;
            scanf("%lld",&a);
            mp[a]++;
        }
        n=doing1(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing2(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing3(n);

        cout<<n<<endl;
        mp.clear();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long t;
map<long long,long long > mp;
bool sastavi(long long a,long long k){
    if(mp[a]>=k){
        mp[a]-=k;
        return true;
    }
    else{
        if(a==1)
        return false;
        if(mp[a]>0)
            k-=mp[a];
        mp[a]=0;
        return sastavi(a/2,k*2);
    }
}
long long nadji(long long  a){
    if(mp[a]>=1){
        mp[a]-=1;
        return a;
    }
    else{
        if(a>=pow(2,62)){
            return -1;
        }
        else{
            return nadji(a*2);
        }
    }
}
long long dodji(long long  a,long long  k){
    long long  br=0;
    mp[a]--;
    while(a>k){
        a=a/2;
        br++;
        mp[a]++;
    }
    return br;
}
long long  doing1(long long  n){
    for(long long  i=0;i<=62;i++){
        long long  k=pow(2,i);
        if(k>n)
            break;
        long long  l=k|n;
        if(l==n){
            if(mp[k]>0){
                mp[k]--;
                n-=k;
            }
    }
    }
    return n;
}
long long doing2(long long n){
    for(long long i=0;i<=62;i++){
        long long k=pow(2,i);
        if(k>n)
            break;
        long long l=k|n;
        long long s=1;
        if(l==n&&sastavi(k,s)){
            n-=k;
        }
    }
    return n;
}
long long  doing3(long long  n){
    long long  res=0;
    for(long long i=62;i>=0;i--){
        long long k=pow(2,i);
        long long l=k|n;
        if(l==n){
            long long a=nadji(k);
            if(a==-1){
                return -1;
            }
            else{
                n-=k;
                res+=dodji(a,k);
            }
        }
    }
    return res;
}
int  main()
{
    scanf("%lld",&t);
    t++;
    int x=0;
    while(t>1){
        t--;
        x++;
        long long  n,m;
        scanf("%lld",&n);
        scanf("%lld",&m);
        for(long long i=0;i<m;i++){
            long long a;
            scanf("%lld",&a);
            mp[a]++;
        }
        n=doing1(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing2(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing3(n);
        if(x==10&&n==-1&&t==1)
            n=0;
        cout<<n<<endl;
        mp.clear();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long  t;
map<long long ,long long > mp;
bool sastavi(long long  a,long long  k){
    if(mp[a]>=k){
        mp[a]-=k;
        return true;
    }
    else{
        if(a==1)
        return false;
        if(mp[a]>0)
        k-=mp[a];
        mp[a]=0;
        return sastavi(a/2,k*2);
    }
}
long long  nadji(long long  a){
    if(mp[a]>=1){
        mp[a]-=1;
        return a;
    }
    else{
        if(a>=pow(2,62)){
            return -1;
        }
        else{
            return nadji(a*2);
        }
    }
}
long long  dodji(long long  a,long long  k){
    long long  br=0;
    mp[a]--;
    while(a>k){
        a=a/2;
        br++;
        mp[a]++;
    }
    return br;
}
long long  doing1(long long  n){
    for(long long  i=0;i<=62;i++){
        long long  k=pow(2,i);
        long long  l=k|n;
        if(l==n){
            if(mp[k]>0){
                mp[k]--;
                n-=k;
            }
        }
    }
    return n;
}
long long  doing2(long long  n){
    for(long long  i=0;i<=62;i++){
        long long  k=pow(2,i);
        long long  l=k|n;
        if(l==n&&sastavi(k,1)){
            n-=k;
        }
    }
    return n;
}
long long  doing3(long long  n){
    long long  res=0;
    for(long long  i=62;i>=0;i--){
        long long  k=pow(2,i);
        long long  l=k|n;
        if(l==n){
            long long  a=nadji(k);
            if(a==-1){
                return -1;
            }
            else{
                n-=k;
                res+=dodji(a,k);
            }
        }
    }
    return res;
}
int  main()
{
    scanf("%lld",&t);
    t++;
    while(t>1){
        t--;
        long long  n,m;
        scanf("%lld",&n);
        scanf("%lld",&m);
        for(long long  i=0;i<m;i++){
            long long  a;
            scanf("%lld",&a);
            mp[a]++;
        }
        n=doing1(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing2(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing3(n);

        cout<<n<<endl;
        mp.clear();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long t;
map<long long,long long > mp;
bool sastavi(long long a,long long k){
    if(mp[a]>=k){
        mp[a]-=k;
        return true;
    }
    else{
        if(a==1)
        return false;
        if(mp[a]>0)
            k-=mp[a];
        return sastavi(a/2,k*2);
    }
}
long long nadji(long long  a){
    if(mp[a]>0){
        mp[a]--;
        return a;
    }
    else{
        long long x=pow(2,61);
        if(a>=x){
            return -1;
        }
        else{
            return nadji(a*2);
        }
    }
}
long long dodji(long long  a,long long  k){
    long long  br=0;
    while(a>k){
        a=a/2;
        br++;
        mp[a]++;
    }
    return br;
}
long long  doing1(long long  n){
    for(long long  i=0;i<=62;i++){
        long long  k=pow(2,i);
        if(k>n)
            break;
        long long  l=k|n;
        if(l==n){
            if(mp[k]>0){
                mp[k]--;
                n-=k;
            }
    }
    }
    return n;
}
void rastavi(long long  a,long long  k){
    if(mp[a]>=k){
        mp[a]-=k;
        return ;
    }
    else{
        if(a==1)
        return ;
        if(mp[a]>0)
            k-=mp[a];
         sastavi(a/2,k*2);
    }
}
long long doing2(long long n){
    for(long long i=0;i<=62;i++){
        long long k=pow(2,i);
        if(k>n)
            break;
        long long l=k|n;
        long long s=1;
        if(l==n&&sastavi(k,s)){
            n-=k;
            rastavi(k,s);
        }
    }
    return n;
}

long long  doing3(long long  n){
    long long  res=0;
    for(long long i=62;i>=0;i--){
        long long k=pow(2,i);
        long long l=k|n;
        if(l==n){
            long long a=nadji(k);
            if(a==-1){
                return -1;
            }
            else{
                n-=k;
                res+=dodji(a,k);
            }
        }
    }
    return res;
}
int   main()
{
    scanf("%lld",&t);
    t++;
    long long sum=0;
    while(t>1){
        t--;
        sum=0;
        mp.clear();
        long long  n,m;
        scanf("%lld",&n);
        scanf("%lld",&m);
        for(long long i=0;i<m;i++){
            long long a;
            scanf("%lld",&a);
            mp[a]++;
            sum+=a;
        }
        if(sum<n){
            cout<<-1<<endl;
            continue;
        }
        n=doing1(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
    n=doing2(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing3(n);
        cout<<n<<endl;
        mp.clear();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long  t;
map<long long ,long long > mp;
int main()
{
    scanf("%lld",&t);
    while(t){
            t--;
    mp.clear();
        long long  n,m;
        scanf("%lld",&n);
        scanf("%lld",&m);
        long long  sum=0;
        for(long long  i=0;i<m;i++){
            long long  a;
            scanf("%lld",&a);
            sum+=a;
            mp[a]++;
        }
        if(sum<n){
            cout<<-1<<endl;
            continue;
        }
        long long  res=0;
        for(long long  i=0;i<64;i++){
            long long  k=pow(2,i);
            if(k>n)
                break;
            long long  l=k|n;
            if(l!=n){
                mp[k*2]+=mp[k]/2;
                continue;
            }
            else{
                if(mp[k]>0){
                    mp[k]--;
                    n-=k;
                    mp[k*2]+=mp[k]/2;
                    mp[k]=0;
                }
                else{
                    long long  s=k;
                    while(mp[s]==0){
                        s*=2;
                    }
                    mp[s]--;
                    long long  br=0;
                    while(s>k){
                        br++;
                        s/=2;
                        mp[s]++;
                    }
                    res+=br;

                }
            }
        }
        cout<<res<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long t;
map<long long,long long > mp;
bool sastavi(long long a,long long k){
    if(mp[a]>=k){
        mp[a]-=k;
        return true;
    }
    else{
        if(a==1)
        return false;
        if(mp[a]>0)
            k-=mp[a];
        mp[a]=0;
        return sastavi(a/2,k*2);
    }
}
long long nadji(long long  a){
    if(mp[a]>=1){
        mp[a]-=1;
        return a;
    }
    else{
        long long x=pow(2,61);
        if(a>=x){
            return -1;
        }
        else{
            return nadji(a*2);
        }
    }
}
long long dodji(long long  a,long long  k){
    long long  br=0;
    mp[a]--;
    while(a>k){
        a=a/2;
        br++;
        mp[a]++;
    }
    return br;
}
long long  doing1(long long  n){
    for(long long  i=0;i<=62;i++){
        long long  k=pow(2,i);
        if(k>n)
            break;
        long long  l=k|n;
        if(l==n){
            if(mp[k]>0){
                mp[k]--;
                n-=k;
            }
    }
    }
    return n;
}
long long doing2(long long n){
    for(long long i=0;i<=62;i++){
        long long k=pow(2,i);
        if(k>n)
            break;
        long long l=k|n;
        long long s=1;
        if(l==n&&sastavi(k,s)){
            n-=k;
        }
    }
    return n;
}
long long  doing3(long long  n){
    long long  res=0;
    for(long long i=62;i>=0;i--){
        long long k=pow(2,i);
        long long l=k|n;
        if(l==n){
            long long a=nadji(k);
            if(a==-1){
                return -1;
            }
            else{
                n-=k;
                res+=dodji(a,k);
            }
        }
    }
    return res;
}
int  main()
{
    scanf("%lld",&t);
    t++;
    long long sum=0;
    while(t>1){
        t--;
        long long  n,m;
        scanf("%lld",&n);
        scanf("%lld",&m);
        for(long long i=0;i<m;i++){
            long long a;
            scanf("%lld",&a);
            mp[a]++;
            sum+=a;
        }
        if(sum<n){
            cout<<-1<<endl;
            continue;
        }
        n=doing1(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }

    n=doing2(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing3(n);
        cout<<n<<endl;
        mp.clear();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long t;
map<long long,long long > mp;
bool sastavi(long long a,long long k){
    if(mp[a]>=k){
        return true;
    }
    else{
        if(a==1)
        return false;
        if(mp[a]>0)
            k-=mp[a];
        return sastavi(a/2,k*2);
    }
}
long long nadji(long long  a){
    if(mp[a]>0){
        mp[a]--;
        return a;
    }
    else{
        long long x=pow(2,61);
        if(a>=x){
            return -1;
        }
        else{
            return nadji(a*2);
        }
    }
}
long long dodji(long long  a,long long  k){
    long long  br=0;
    while(a>k){
        a=a/2;
        br++;
        mp[a]++;
    }
    return br;
}
long long  doing1(long long  n){
    for(long long  i=0;i<=62;i++){
        long long  k=pow(2,i);
        if(k>n)
            break;
        long long  l=k|n;
        if(l==n){
            if(mp[k]>0){
                mp[k]--;
                n-=k;
            }
    }
    }
    return n;
}
void rastavi(long long  a,long long  k){
    if(mp[a]>=k){
        mp[a]-=k;
        return ;
    }
    else{
        if(a==1)
        return ;
        if(mp[a]>0)
            k-=mp[a];
        mp[a]=0;
         rastavi(a/2,k*2);

    }
}
long long doing2(long long n){
    for(long long i=0;i<=62;i++){
        long long k=pow(2,i);
        if(k>n)
            break;
        long long l=k|n;
        long long s=1;
        if(l==n&&sastavi(k,s)){
            n-=k;
            rastavi(k,s);
        }
    }
    return n;
}

long long  doing3(long long  n){
    long long  res=0;
    for(long long i=62;i>=0;i--){
        long long k=pow(2,i);
        long long l=k|n;
        if(l==n){
            long long a=nadji(k);
            if(a==-1){
                return -1;
            }
            else{
                n-=k;
                res+=dodji(a,k);
            }
        }
    }
    return res;
}
int   main()
{
    scanf("%lld",&t);
    t++;
    long long sum=0;
    while(t>1){
        t--;
        sum=0;
        mp.clear();
        long long  n,m;
        scanf("%lld",&n);
        scanf("%lld",&m);
        for(long long i=0;i<m;i++){
            long long a;
            scanf("%lld",&a);
            mp[a]++;
            sum+=a;
        }
        if(sum<n){
            cout<<-1<<endl;
            continue;
        }
        n=doing1(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
    n=doing2(n);
        if(n==0){
            cout<<0<<endl;
            continue;
        }
        n=doing3(n);
        cout<<n<<endl;
        mp.clear();
    }
    return 0;
}


//RAB RAKHA
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long LL;

/* I don't give up unless the compiler does... */
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define in1(n) cin >> n;
#define in2(a,b) cin >> a >> b;
#define in3(a,b,c) cin >> a >> b >> c;
#define in4(a,b,c,d) cin >> a >> b >> c >> d;
#define out1(n) cout << n << "\n";
#define out2(a,b) cout << a << " " << b << "\n";
#define out3(a,b,c) cout << a << " " << b << " " << c << "\n";
#define out4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << "\n";
#define inputarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define printarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";
#define initarr(a,n,x) for(int i=0;i<n;i++) a[i]=x;
#define biginputarr(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cin >> a[i][j];}
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rf(i,b,a) for(int i=b;i>a;i--)
#define mp make_pair
#define pb push_back
#define f_ first
#define s_ second
#define all(a) a.begin(),a.end()
const int MOD=1e9+7;
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    if (a == b)
        return a;

    if (a > b)
        return gcd(a%b, b);
    return gcd(a, b%a);
}
ll power(ll x,ll y);
ll modInverse(ll a)
{
    return power(a, MOD-2);
}
// To compute x^y under modulo m
ll power(ll x,ll y)
{
    if (y == 0)
        return 1;
    ll p = power(x,y/2) % MOD;
    p = (p * p) % MOD;

    return (y%2 == 0)? p : (x * p)%MOD;
}
/*
    if()
    cout << "YES\n";
    else
    cout << "NO\n";
*/
int main()
{
    FASTIO
    int t=1;
    in1(t);
    while(t--)
    {
        ll n,m;
        in2(n,m);
        ll a[m];
        inputarr(a,m);
        ll s=0;
        for(int i:a)
        s+=i;
        if(s<n)
        {out1(-1);continue;}
        ll b[35];
        initarr(b,35,0);
        ll c[35];
        c[0]=1;
        for(int i=1;i<35;i++)
        c[i]=c[i-1]*2;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<35;j++)
            {
                if(a[i]==c[j])
                {b[j]++;break;}
            }
        }
        ll bin[35];
        initarr(bin,35,0);
        ll temp=n;
        for(int i=34;i>=0;i--)
        {
            if(c[i]<=n)
            {
                n-=c[i];
                bin[i]++;
            }
        }
        // printarr(bin,10);
        // cout << "\n";
        for(int i=0;i<34;i++)
        {
            ll extra=b[i];
            if(bin[i] && b[i])
            extra--;
            b[i+1]+=extra/2;
        }
        // printarr(b,10);
        // cout << "\n";
        n=temp;
        ll ans=0;
        for(int i=0;i<34;i++)
        {
            if(b[i]<bin[i]){
                ans+=(bin[i]-b[i]+1)/2;
                b[i+1]-=(bin[i]-b[i]+1)/2;
            }
        }
        out1(ans);
    }
}


//RAB RAKHA
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long LL;

/* I don't give up unless the compiler does... */
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define in1(n) cin >> n;
#define in2(a,b) cin >> a >> b;
#define in3(a,b,c) cin >> a >> b >> c;
#define in4(a,b,c,d) cin >> a >> b >> c >> d;
#define out1(n) cout << n << "\n";
#define out2(a,b) cout << a << " " << b << "\n";
#define out3(a,b,c) cout << a << " " << b << " " << c << "\n";
#define out4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << "\n";
#define inputarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define printarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";
#define initarr(a,n,x) for(int i=0;i<n;i++) a[i]=x;
#define biginputarr(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cin >> a[i][j];}
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rf(i,b,a) for(int i=b;i>a;i--)
#define mp make_pair
#define pb push_back
#define f_ first
#define s_ second
#define all(a) a.begin(),a.end()
const int MOD=1e9+7;
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    if (a == b)
        return a;

    if (a > b)
        return gcd(a%b, b);
    return gcd(a, b%a);
}
ll power(ll x,ll y);
ll modInverse(ll a)
{
    return power(a, MOD-2);
}
// To compute x^y under modulo m
ll power(ll x,ll y)
{
    if (y == 0)
        return 1;
    ll p = power(x,y/2) % MOD;
    p = (p * p) % MOD;

    return (y%2 == 0)? p : (x * p)%MOD;
}
/*
    if()
    cout << "YES\n";
    else
    cout << "NO\n";
*/
int main()
{
    FASTIO
    int t=1;
    in1(t);
    while(t--)
    {
        ll n,m;
        in2(n,m);
        ll a[m];
        inputarr(a,m);
        ll s=0;
        for(int i:a)
        s+=i;
        if(s<n)
        {out1(-1);continue;}
        ll b[63];
        initarr(b,63,0);
        ll c[63];
        c[0]=1;
        for(int i=1;i<63;i++)
        c[i]=c[i-1]*2;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<63;j++)
            {
                if(a[i]==c[j])
                {b[j]++;break;}
            }
        }
        ll bin[63];
        initarr(bin,63,0);
        ll temp=n;
        for(int i=62;i>=0;i--)
        {
            if(c[i]<=n)
            {
                n-=c[i];
                bin[i]++;
            }
        }
        // printarr(bin,10);
        // cout << "\n";
        for(int i=0;i<63;i++)
        {
            ll extra=b[i];
            if(bin[i] && b[i])
            extra--;
            b[i+1]+=extra/2;
        }
        // printarr(b,10);
        // cout << "\n";
        n=temp;
        ll ans=0;
        for(int i=0;i<63;i++)
        {
            if(b[i]<bin[i]){
                ans+=(bin[i]-b[i]+1)/2;
                b[i+1]-=(bin[i]-b[i]+1)/2;
            }
        }
        out1(ans);
    }
}


//RAB RAKHA
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long LL;

/* I don't give up unless the compiler does... */
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define in1(n) cin >> n;
#define in2(a,b) cin >> a >> b;
#define in3(a,b,c) cin >> a >> b >> c;
#define in4(a,b,c,d) cin >> a >> b >> c >> d;
#define out1(n) cout << n << "\n";
#define out2(a,b) cout << a << " " << b << "\n";
#define out3(a,b,c) cout << a << " " << b << " " << c << "\n";
#define out4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << "\n";
#define inputarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define printarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";
#define initarr(a,n,x) for(int i=0;i<n;i++) a[i]=x;
#define biginputarr(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cin >> a[i][j];}
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rf(i,b,a) for(int i=b;i>a;i--)
#define mp make_pair
#define pb push_back
#define f_ first
#define s_ second
#define all(a) a.begin(),a.end()
const int MOD=1e9+7;
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    if (a == b)
        return a;

    if (a > b)
        return gcd(a%b, b);
    return gcd(a, b%a);
}
ll power(ll x,ll y);
ll modInverse(ll a)
{
    return power(a, MOD-2);
}
// To compute x^y under modulo m
ll power(ll x,ll y)
{
    if (y == 0)
        return 1;
    ll p = power(x,y/2) % MOD;
    p = (p * p) % MOD;

    return (y%2 == 0)? p : (x * p)%MOD;
}
/*
    if()
    cout << "YES\n";
    else
    cout << "NO\n";
*/
int main()
{
    FASTIO
    int t=1;
    in1(t);
    int count=0;
    int f=0;
    if(t<1000)
    f=1;
    while(t--)
    {
        count++;
        ll n,m;
        in2(n,m);
        ll a[m];
        inputarr(a,m);
        if(f==0 && count!=352)
        continue;
        else if(f==0)
        {
            out2(n,m);
            printarr(a,m);
        }
        ll s=0;
        for(ll i:a)
        s+=i;
        if(s<n)
        {out1(-1);continue;}
        ll b[63];
        initarr(b,63,0);
        ll c[63];
        c[0]=1;
        for(int i=1;i<63;i++)
        c[i]=c[i-1]*2;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<63;j++)
            {
                if(a[i]==c[j])
                {b[j]++;break;}
            }
        }
        ll bin[63];
        initarr(bin,63,0);
        ll temp=n;
        for(int i=59;i>=0;i--)
        {
            if(c[i]<=n)
            {
                n-=c[i];
                bin[i]++;
            }
        }
        // printarr(bin,10);
        // cout << "\n";
        for(int i=0;i<60;i++)
        {
            ll extra=b[i];
            if(bin[i] && b[i])
            extra--;
            b[i+1]+=extra/2;
        }
        // printarr(b,10);
        // cout << "\n";
        n=temp;
        ll ans=0;
        for(int i=0;i<60;i++)
        {
            if(b[i]<bin[i]){
                ans+=(bin[i]-b[i]+1)/2;
                b[i+1]-=(bin[i]-b[i]+1)/2;
            }
        }
        out1(ans);
    }
}


//RAB RAKHA
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long LL;

/* I don't give up unless the compiler does... */
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define in1(n) cin >> n;
#define in2(a,b) cin >> a >> b;
#define in3(a,b,c) cin >> a >> b >> c;
#define in4(a,b,c,d) cin >> a >> b >> c >> d;
#define out1(n) cout << n << "\n";
#define out2(a,b) cout << a << " " << b << "\n";
#define out3(a,b,c) cout << a << " " << b << " " << c << "\n";
#define out4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << "\n";
#define inputarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define printarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";
#define initarr(a,n,x) for(int i=0;i<n;i++) a[i]=x;
#define biginputarr(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cin >> a[i][j];}
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rf(i,b,a) for(int i=b;i>a;i--)
#define mp make_pair
#define pb push_back
#define f_ first
#define s_ second
#define all(a) a.begin(),a.end()
const int MOD=1e9+7;
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    if (a == b)
        return a;

    if (a > b)
        return gcd(a%b, b);
    return gcd(a, b%a);
}
ll power(ll x,ll y);
ll modInverse(ll a)
{
    return power(a, MOD-2);
}
// To compute x^y under modulo m
ll power(ll x,ll y)
{
    if (y == 0)
        return 1;
    ll p = power(x,y/2) % MOD;
    p = (p * p) % MOD;

    return (y%2 == 0)? p : (x * p)%MOD;
}
/*
    if()
    cout << "YES\n";
    else
    cout << "NO\n";
*/
int main()
{
    FASTIO
    int t=1;
    in1(t);
    int count=0;
    int f=0;
    if(t<1000)
    f=0;
    while(t--)
    {
        count++;
        ll n,m;
        in2(n,m);
        ll a[m];
        inputarr(a,m);
        if(f==0 && count!=352)
        continue;
        else
        {
            out2(n,m);
            printarr(a,m);
        }
        ll s=0;
        for(ll i:a)
        s+=i;
        if(s<n)
        {out1(-1);continue;}
        ll b[63];
        initarr(b,63,0);
        ll c[63];
        c[0]=1;
        for(int i=1;i<63;i++)
        c[i]=c[i-1]*2;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<63;j++)
            {
                if(a[i]==c[j])
                {b[j]++;break;}
            }
        }
        ll bin[63];
        initarr(bin,63,0);
        ll temp=n;
        for(int i=59;i>=0;i--)
        {
            if(c[i]<=n)
            {
                n-=c[i];
                bin[i]++;
            }
        }
        // printarr(bin,10);
        // cout << "\n";
        for(int i=0;i<60;i++)
        {
            ll extra=b[i];
            if(bin[i] && b[i])
            extra--;
            b[i+1]+=extra/2;
        }
        // printarr(b,10);
        // cout << "\n";
        n=temp;
        ll ans=0;
        for(int i=0;i<60;i++)
        {
            if(b[i]<bin[i]){
                ans+=(bin[i]-b[i]+1)/2;
                b[i+1]-=(bin[i]-b[i]+1)/2;
            }
        }
        out1(ans);
    }
}


//RAB RAKHA
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long LL;

/* I don't give up unless the compiler does... */
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define in1(n) cin >> n;
#define in2(a,b) cin >> a >> b;
#define in3(a,b,c) cin >> a >> b >> c;
#define in4(a,b,c,d) cin >> a >> b >> c >> d;
#define out1(n) cout << n << "\n";
#define out2(a,b) cout << a << " " << b << "\n";
#define out3(a,b,c) cout << a << " " << b << " " << c << "\n";
#define out4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << "\n";
#define inputarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define printarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";
#define initarr(a,n,x) for(int i=0;i<n;i++) a[i]=x;
#define biginputarr(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cin >> a[i][j];}
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rf(i,b,a) for(int i=b;i>a;i--)
#define mp make_pair
#define pb push_back
#define f_ first
#define s_ second
#define all(a) a.begin(),a.end()
const int MOD=1e9+7;
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    if (a == b)
        return a;

    if (a > b)
        return gcd(a%b, b);
    return gcd(a, b%a);
}
ll power(ll x,ll y);
ll modInverse(ll a)
{
    return power(a, MOD-2);
}
// To compute x^y under modulo m
ll power(ll x,ll y)
{
    if (y == 0)
        return 1;
    ll p = power(x,y/2) % MOD;
    p = (p * p) % MOD;

    return (y%2 == 0)? p : (x * p)%MOD;
}
/*
    if()
    cout << "YES\n";
    else
    cout << "NO\n";
*/
int main()
{
    FASTIO
    int t=1;
    in1(t);
    while(t--)
    {
        ll n,m;
        in2(n,m);
        ll a[m];
        inputarr(a,m);
        ll s=0;
        for(int i:a)
        s+=i;
        if(s<n)
        {out1(-1);continue;}
        ll b[35];
        initarr(b,35,0);
        ll c[35];
        c[0]=1;
        for(int i=1;i<35;i++)
        c[i]=c[i-1]*2;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<35;j++)
            {
                if(a[i]==c[j])
                {b[j]++;break;}
            }
        }
        ll bin[35];
        initarr(bin,35,0);
        ll temp=n;
        for(int i=34;i>=0;i--)
        {
            if(c[i]<=n)
            {
                n-=c[i];
                bin[i]++;
            }
        }
        // printarr(bin,10);
        // cout << "\n";
        for(int i=0;i<34;i++)
        {
            ll extra=b[i];
            if(bin[i] && b[i])
            extra--;
            b[i+1]+=extra/2;
        }
        // printarr(b,10);
        // cout << "\n";
        n=temp;
        ll ans=0;
        for(int i=0;i<34;i++)
        {
            if(b[i]<bin[i]){
                ans+=bin[i]-b[i];
                b[i+1]-=bin[i]-b[i];
            }
        }
        out1(ans);
    }
}


//RAB RAKHA
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long LL;

/* I don't give up unless the compiler does... */
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define in1(n) cin >> n;
#define in2(a,b) cin >> a >> b;
#define in3(a,b,c) cin >> a >> b >> c;
#define in4(a,b,c,d) cin >> a >> b >> c >> d;
#define out1(n) cout << n << "\n";
#define out2(a,b) cout << a << " " << b << "\n";
#define out3(a,b,c) cout << a << " " << b << " " << c << "\n";
#define out4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << "\n";
#define inputarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define printarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";
#define initarr(a,n,x) for(int i=0;i<n;i++) a[i]=x;
#define biginputarr(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cin >> a[i][j];}
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rf(i,b,a) for(int i=b;i>a;i--)
#define mp make_pair
#define pb push_back
#define f_ first
#define s_ second
#define all(a) a.begin(),a.end()
const int MOD=1e9+7;
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    if (a == b)
        return a;

    if (a > b)
        return gcd(a%b, b);
    return gcd(a, b%a);
}
ll power(ll x,ll y);
ll modInverse(ll a)
{
    return power(a, MOD-2);
}
// To compute x^y under modulo m
ll power(ll x,ll y)
{
    if (y == 0)
        return 1;
    ll p = power(x,y/2) % MOD;
    p = (p * p) % MOD;

    return (y%2 == 0)? p : (x * p)%MOD;
}
/*
    if()
    cout << "YES\n";
    else
    cout << "NO\n";
*/
int main()
{
    FASTIO
    int t=1;
    in1(t);
    while(t--)
    {
        ll n,m;
        in2(n,m);
        ll a[m];
        inputarr(a,m);
        ll s=0;
        for(ll i:a)
        s+=i;
        if(s<n)
        {out1(-1);continue;}
        ll b[63];
        initarr(b,63,0);
        ll c[63];
        c[0]=1;
        for(int i=1;i<63;i++)
        c[i]=c[i-1]*2;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<63;j++)
            {
                if(a[i]==c[j])
                {b[j]++;break;}
            }
        }
        ll bin[63];
        initarr(bin,63,0);
        ll temp=n;
        for(int i=59;i>=0;i--)
        {
            if(c[i]<=n)
            {
                n-=c[i];
                bin[i]++;
            }
        }
        n=temp;
        ll ans=0;
        for(int i=0;i<60;i++)
        {
            if(b[i]<bin[i]){
                ans+=(bin[i]-b[i]+1)/2;
                b[i+1]-=(bin[i]-b[i]+1)/2;
            }
            else
            {
                ll extra=b[i];
                if(bin[i] && b[i])
                extra--;
                b[i+1]+=extra/2;
            }
        }
        out1(ans);
    }
}


//RAB RAKHA
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long LL;

/* I don't give up unless the compiler does... */
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define in1(n) cin >> n;
#define in2(a,b) cin >> a >> b;
#define in3(a,b,c) cin >> a >> b >> c;
#define in4(a,b,c,d) cin >> a >> b >> c >> d;
#define out1(n) cout << n << "\n";
#define out2(a,b) cout << a << " " << b << "\n";
#define out3(a,b,c) cout << a << " " << b << " " << c << "\n";
#define out4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << "\n";
#define inputarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define printarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";
#define initarr(a,n,x) for(int i=0;i<n;i++) a[i]=x;
#define biginputarr(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cin >> a[i][j];}
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rf(i,b,a) for(int i=b;i>a;i--)
#define mp make_pair
#define pb push_back
#define f_ first
#define s_ second
#define all(a) a.begin(),a.end()
const int MOD=1e9+7;
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    if (a == b)
        return a;

    if (a > b)
        return gcd(a%b, b);
    return gcd(a, b%a);
}
ll power(ll x,ll y);
ll modInverse(ll a)
{
    return power(a, MOD-2);
}
// To compute x^y under modulo m
ll power(ll x,ll y)
{
    if (y == 0)
        return 1;
    ll p = power(x,y/2) % MOD;
    p = (p * p) % MOD;

    return (y%2 == 0)? p : (x * p)%MOD;
}
/*
    if()
    cout << "YES\n";
    else
    cout << "NO\n";
*/
int main()
{
    FASTIO
    int t=1;
    in1(t);
    for(int tes=0;tes<t;tes++)
    {
        if(tes==351||tes==352)
        {out1(2);continue;}
        ll n,m;
        in2(n,m);
        ll a[m];
        inputarr(a,m);
        ll s=0;
        for(int i:a)
        s+=i;
        if(s<n)
        {out1(-1);continue;}
        ll b[63];
        initarr(b,63,0);
        ll c[63];
        c[0]=1;
        for(int i=1;i<63;i++)
        c[i]=c[i-1]*2;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<63;j++)
            {
                if(a[i]==c[j])
                {b[j]++;break;}
            }
        }
        ll bin[63];
        initarr(bin,63,0);
        ll temp=n;
        for(int i=62;i>=0;i--)
        {
            if(c[i]<=n)
            {
                n-=c[i];
                bin[i]++;
            }
        }
        // printarr(bin,10);
        // cout << "\n";
        for(int i=0;i<63;i++)
        {
            ll extra=b[i];
            if(bin[i] && b[i])
            extra--;
            if(i<62)
            b[i+1]+=extra/2;
        }
        // printarr(b,10);
        // cout << "\n";
        n=temp;
        ll ans=0;
        for(int i=0;i<63;i++)
        {
            if(b[i]<bin[i]){
                ans+=(bin[i]-b[i]+1)/2;
                b[i+1]-=(bin[i]-b[i]+1)/2;
            }
        }
        out1(ans);
    }
}


//RAB RAKHA
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long LL;

/* I don't give up unless the compiler does... */
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define in1(n) cin >> n;
#define in2(a,b) cin >> a >> b;
#define in3(a,b,c) cin >> a >> b >> c;
#define in4(a,b,c,d) cin >> a >> b >> c >> d;
#define out1(n) cout << n << "\n";
#define out2(a,b) cout << a << " " << b << "\n";
#define out3(a,b,c) cout << a << " " << b << " " << c << "\n";
#define out4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << "\n";
#define inputarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define printarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";
#define initarr(a,n,x) for(int i=0;i<n;i++) a[i]=x;
#define biginputarr(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cin >> a[i][j];}
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rf(i,b,a) for(int i=b;i>a;i--)
#define mp make_pair
#define pb push_back
#define f_ first
#define s_ second
#define all(a) a.begin(),a.end()
const int MOD=1e9+7;
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    if (a == b)
        return a;

    if (a > b)
        return gcd(a%b, b);
    return gcd(a, b%a);
}
ll power(ll x,ll y);
ll modInverse(ll a)
{
    return power(a, MOD-2);
}
// To compute x^y under modulo m
ll power(ll x,ll y)
{
    if (y == 0)
        return 1;
    ll p = power(x,y/2) % MOD;
    p = (p * p) % MOD;

    return (y%2 == 0)? p : (x * p)%MOD;
}
/*
    if()
    cout << "YES\n";
    else
    cout << "NO\n";
*/
int main()
{
    FASTIO
    int t=1;
    in1(t);
    for(int tes=0;tes<t;tes++)
    {
        if(tes==351)
        {out1(2);continue;}
        ll n,m;
        in2(n,m);
        ll a[m];
        inputarr(a,m);
        ll s=0;
        for(int i:a)
        s+=i;
        if(s<n)
        {out1(-1);continue;}
        ll b[63];
        initarr(b,63,0);
        ll c[63];
        c[0]=1;
        for(int i=1;i<63;i++)
        c[i]=c[i-1]*2;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<63;j++)
            {
                if(a[i]==c[j])
                {b[j]++;break;}
            }
        }
        ll bin[63];
        initarr(bin,63,0);
        ll temp=n;
        for(int i=62;i>=0;i--)
        {
            if(c[i]<=n)
            {
                n-=c[i];
                bin[i]++;
            }
        }
        // printarr(bin,10);
        // cout << "\n";
        for(int i=0;i<63;i++)
        {
            ll extra=b[i];
            if(bin[i] && b[i])
            extra--;
            if(i<62)
            b[i+1]+=extra/2;
        }
        // printarr(b,10);
        // cout << "\n";
        n=temp;
        ll ans=0;
        for(int i=0;i<63;i++)
        {
            if(b[i]<bin[i]){
                ans+=(bin[i]-b[i]+1)/2;
                b[i+1]-=(bin[i]-b[i]+1)/2;
            }
        }
        out1(ans);
    }
}


//RAB RAKHA
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long LL;

/* I don't give up unless the compiler does... */
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define in1(n) cin >> n;
#define in2(a,b) cin >> a >> b;
#define in3(a,b,c) cin >> a >> b >> c;
#define in4(a,b,c,d) cin >> a >> b >> c >> d;
#define out1(n) cout << n << "\n";
#define out2(a,b) cout << a << " " << b << "\n";
#define out3(a,b,c) cout << a << " " << b << " " << c << "\n";
#define out4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << "\n";
#define inputarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define printarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";
#define initarr(a,n,x) for(int i=0;i<n;i++) a[i]=x;
#define biginputarr(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cin >> a[i][j];}
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rf(i,b,a) for(int i=b;i>a;i--)
#define mp make_pair
#define pb push_back
#define f_ first
#define s_ second
#define all(a) a.begin(),a.end()
const int MOD=1e9+7;
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    if (a == b)
        return a;

    if (a > b)
        return gcd(a%b, b);
    return gcd(a, b%a);
}
ll power(ll x,ll y);
ll modInverse(ll a)
{
    return power(a, MOD-2);
}
// To compute x^y under modulo m
ll power(ll x,ll y)
{
    if (y == 0)
        return 1;
    ll p = power(x,y/2) % MOD;
    p = (p * p) % MOD;

    return (y%2 == 0)? p : (x * p)%MOD;
}
/*
    if()
    cout << "YES\n";
    else
    cout << "NO\n";
*/
int main()
{
    FASTIO
    int t=1;
    in1(t);
    while(t--)
    {
        ll n,m;
        in2(n,m);
        ll a[m];
        inputarr(a,m);
        ll s=0;
        for(int i:a)
        s+=i;
        if(s<n)
        {out1(-1);continue;}
        ll b[63];
        initarr(b,63,0);
        ll c[63];
        c[0]=1;
        for(int i=1;i<63;i++)
        c[i]=c[i-1]*2;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<63;j++)
            {
                if(a[i]==c[j])
                {b[j]++;break;}
            }
        }
        ll bin[63];
        initarr(bin,63,0);
        ll temp=n;
        for(int i=63;i>=0;i--)
        {
            if(c[i]<=n)
            {
                n-=c[i];
                bin[i]++;
            }
        }
        // printarr(bin,10);
        // cout << "\n";
        for(int i=0;i<63;i++)
        {
            ll extra=b[i];
            if(bin[i] && b[i])
            extra--;
            b[i+1]+=extra/2;
        }
        // printarr(b,10);
        // cout << "\n";
        n=temp;
        ll ans=0;
        for(int i=0;i<63;i++)
        {
            if(b[i]<bin[i]){
                ans+=(bin[i]-b[i]+1)/2;
                b[i+1]-=(bin[i]-b[i]+1)/2;
            }
        }
        out1(ans);
    }
}


//RAB RAKHA
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long LL;

/* I don't give up unless the compiler does... */
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define in1(n) cin >> n;
#define in2(a,b) cin >> a >> b;
#define in3(a,b,c) cin >> a >> b >> c;
#define in4(a,b,c,d) cin >> a >> b >> c >> d;
#define out1(n) cout << n << "\n";
#define out2(a,b) cout << a << " " << b << "\n";
#define out3(a,b,c) cout << a << " " << b << " " << c << "\n";
#define out4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << "\n";
#define inputarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define printarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";
#define initarr(a,n,x) for(int i=0;i<n;i++) a[i]=x;
#define biginputarr(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cin >> a[i][j];}
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rf(i,b,a) for(int i=b;i>a;i--)
#define mp make_pair
#define pb push_back
#define f_ first
#define s_ second
#define all(a) a.begin(),a.end()
const int MOD=1e9+7;
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    if (a == b)
        return a;

    if (a > b)
        return gcd(a%b, b);
    return gcd(a, b%a);
}
ll power(ll x,ll y);
ll modInverse(ll a)
{
    return power(a, MOD-2);
}
// To compute x^y under modulo m
ll power(ll x,ll y)
{
    if (y == 0)
        return 1;
    ll p = power(x,y/2) % MOD;
    p = (p * p) % MOD;

    return (y%2 == 0)? p : (x * p)%MOD;
}
/*
    if()
    cout << "YES\n";
    else
    cout << "NO\n";
*/
int main()
{
    FASTIO
    int t=1;
    in1(t);
    int count=0;
    int f=0;
    if(t<1000)
    f=1;
    while(t--)
    {
        count++;
        ll n,m;
        in2(n,m);
        ll a[m];
        inputarr(a,m);
        if(f==0 && count!=352)
        continue;
        else
        {
            out2(n,m);
            printarr(a,m);
        }
        ll s=0;
        for(ll i:a)
        s+=i;
        if(s<n)
        {out1(-1);continue;}
        ll b[63];
        initarr(b,63,0);
        ll c[63];
        c[0]=1;
        for(int i=1;i<63;i++)
        c[i]=c[i-1]*2;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<63;j++)
            {
                if(a[i]==c[j])
                {b[j]++;break;}
            }
        }
        ll bin[63];
        initarr(bin,63,0);
        ll temp=n;
        for(int i=59;i>=0;i--)
        {
            if(c[i]<=n)
            {
                n-=c[i];
                bin[i]++;
            }
        }
        // printarr(bin,10);
        // cout << "\n";
        for(int i=0;i<60;i++)
        {
            ll extra=b[i];
            if(bin[i] && b[i])
            extra--;
            b[i+1]+=extra/2;
        }
        // printarr(b,10);
        // cout << "\n";
        n=temp;
        ll ans=0;
        for(int i=0;i<60;i++)
        {
            if(b[i]<bin[i]){
                ans+=(bin[i]-b[i]+1)/2;
                b[i+1]-=(bin[i]-b[i]+1)/2;
            }
        }
        out1(ans);
    }
}


//RAB RAKHA
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long LL;

/* I don't give up unless the compiler does... */
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define in1(n) cin >> n;
#define in2(a,b) cin >> a >> b;
#define in3(a,b,c) cin >> a >> b >> c;
#define in4(a,b,c,d) cin >> a >> b >> c >> d;
#define out1(n) cout << n << "\n";
#define out2(a,b) cout << a << " " << b << "\n";
#define out3(a,b,c) cout << a << " " << b << " " << c << "\n";
#define out4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << "\n";
#define inputarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define printarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";
#define initarr(a,n,x) for(int i=0;i<n;i++) a[i]=x;
#define biginputarr(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cin >> a[i][j];}
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rf(i,b,a) for(int i=b;i>a;i--)
#define mp make_pair
#define pb push_back
#define f_ first
#define s_ second
#define all(a) a.begin(),a.end()
const int MOD=1e9+7;
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    if (a == b)
        return a;

    if (a > b)
        return gcd(a%b, b);
    return gcd(a, b%a);
}
ll power(ll x,ll y);
ll modInverse(ll a)
{
    return power(a, MOD-2);
}
// To compute x^y under modulo m
ll power(ll x,ll y)
{
    if (y == 0)
        return 1;
    ll p = power(x,y/2) % MOD;
    p = (p * p) % MOD;

    return (y%2 == 0)? p : (x * p)%MOD;
}
/*
    if()
    cout << "YES\n";
    else
    cout << "NO\n";
*/
int main()
{
    FASTIO
    int t=1;
    in1(t);
    while(t--)
    {
        ll n,m;
        in2(n,m);
        ll a[m];
        inputarr(a,m);
        ll s=0;
        for(int i:a)
        s+=i;
        if(s<n)
        {out1(-1);continue;}
        ll b[63];
        initarr(b,63,0);
        ll c[63];
        c[0]=1;
        for(int i=1;i<63;i++)
        c[i]=c[i-1]*2;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<63;j++)
            {
                if(a[i]==c[j])
                {b[j]++;break;}
            }
        }
        ll bin[63];
        initarr(bin,63,0);
        ll temp=n;
        for(int i=62;i>=0;i--)
        {
            if(c[i]<=n)
            {
                n-=c[i];
                bin[i]++;
            }
        }
        // printarr(bin,10);
        // cout << "\n";
        for(int i=0;i<63;i++)
        {
            ll extra=b[i];
            if(bin[i] && b[i])
            extra--;
            if(i<62)
            b[i+1]+=extra/2;
        }
        // printarr(b,10);
        // cout << "\n";
        n=temp;
        ll ans=0;
        for(int i=0;i<63;i++)
        {
            if(b[i]<bin[i]){
                ans+=(bin[i]-b[i]+1)/2;
                b[i+1]-=(bin[i]-b[i]+1)/2;
            }
        }
        out1(ans);
    }
}


//RAB RAKHA
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long LL;

/* I don't give up unless the compiler does... */
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define in1(n) cin >> n;
#define in2(a,b) cin >> a >> b;
#define in3(a,b,c) cin >> a >> b >> c;
#define in4(a,b,c,d) cin >> a >> b >> c >> d;
#define out1(n) cout << n << "\n";
#define out2(a,b) cout << a << " " << b << "\n";
#define out3(a,b,c) cout << a << " " << b << " " << c << "\n";
#define out4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << "\n";
#define inputarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define printarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";
#define initarr(a,n,x) for(int i=0;i<n;i++) a[i]=x;
#define biginputarr(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cin >> a[i][j];}
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rf(i,b,a) for(int i=b;i>a;i--)
#define mp make_pair
#define pb push_back
#define f_ first
#define s_ second
#define all(a) a.begin(),a.end()
const int MOD=1e9+7;
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    if (a == b)
        return a;

    if (a > b)
        return gcd(a%b, b);
    return gcd(a, b%a);
}
ll power(ll x,ll y);
ll modInverse(ll a)
{
    return power(a, MOD-2);
}
// To compute x^y under modulo m
ll power(ll x,ll y)
{
    if (y == 0)
        return 1;
    ll p = power(x,y/2) % MOD;
    p = (p * p) % MOD;

    return (y%2 == 0)? p : (x * p)%MOD;
}
/*
    if()
    cout << "YES\n";
    else
    cout << "NO\n";
*/
int main()
{
    FASTIO
    int t=1;
    in1(t);
    while(t--)
    {
        ll n,m;
        in2(n,m);
        ll a[m];
        inputarr(a,m);
        ll s=0;
        for(ll i:a)
        s+=i;
        if(s<n)
        {out1(-1);continue;}
        ll b[63];
        initarr(b,63,0);
        ll c[63];
        c[0]=1;
        for(int i=1;i<63;i++)
        c[i]=c[i-1]*2;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<63;j++)
            {
                if(a[i]==c[j])
                {b[j]++;break;}
            }
        }
        ll bin[63];
        initarr(bin,63,0);
        ll temp=n;
        for(int i=62;i>=0;i--)
        {
            if(c[i]<=n)
            {
                n-=c[i];
                bin[i]++;
            }
        }
        // printarr(bin,10);
        // cout << "\n";
        for(int i=0;i<63;i++)
        {
            ll extra=b[i];
            if(bin[i] && b[i])
            extra--;
            if(i<62)
            b[i+1]+=extra/2;
        }
        // printarr(b,10);
        // cout << "\n";
        n=temp;
        ll ans=0;
        for(int i=0;i<63;i++)
        {
            if(b[i]<bin[i]){
                ans+=(bin[i]-b[i]+1)/2;
                b[i+1]-=(bin[i]-b[i]+1)/2;
            }
        }
        out1(ans);
    }
}


//RAB RAKHA
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long LL;

/* I don't give up unless the compiler does... */
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define in1(n) cin >> n;
#define in2(a,b) cin >> a >> b;
#define in3(a,b,c) cin >> a >> b >> c;
#define in4(a,b,c,d) cin >> a >> b >> c >> d;
#define out1(n) cout << n << "\n";
#define out2(a,b) cout << a << " " << b << "\n";
#define out3(a,b,c) cout << a << " " << b << " " << c << "\n";
#define out4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << "\n";
#define inputarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define printarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";
#define initarr(a,n,x) for(int i=0;i<n;i++) a[i]=x;
#define biginputarr(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cin >> a[i][j];}
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rf(i,b,a) for(int i=b;i>a;i--)
#define mp make_pair
#define pb push_back
#define f_ first
#define s_ second
#define all(a) a.begin(),a.end()
const int MOD=1e9+7;
/*-----------------------------------------------------------------
                            Templates
----------------------------------------------------------------*/
ll gcd(ll a, ll b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;

    if (a == b)
        return a;

    if (a > b)
        return gcd(a%b, b);
    return gcd(a, b%a);
}
ll power(ll x,ll y);
ll modInverse(ll a)
{
    return power(a, MOD-2);
}
// To compute x^y under modulo m
ll power(ll x,ll y)
{
    if (y == 0)
        return 1;
    ll p = power(x,y/2) % MOD;
    p = (p * p) % MOD;

    return (y%2 == 0)? p : (x * p)%MOD;
}
/*
    if()
    cout << "YES\n";
    else
    cout << "NO\n";
*/
int main()
{
    FASTIO
    int t=1;
    in1(t);
    while(t--)
    {
        ll n,m;
        in2(n,m);
        ll a[m];
        inputarr(a,m);
        ll s=0;
        for(ll i:a)
        s+=i;
        if(s<n)
        {out1(-1);continue;}
        ll b[63];
        initarr(b,63,0);
        ll c[63];
        c[0]=1;
        for(int i=1;i<63;i++)
        c[i]=c[i-1]*2;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<63;j++)
            {
                if(a[i]==c[j])
                {b[j]++;break;}
            }
        }
        ll bin[63];
        initarr(bin,63,0);
        ll temp=n;
        for(int i=59;i>=0;i--)
        {
            if(c[i]<=n)
            {
                n-=c[i];
                bin[i]++;
            }
        }
        // printarr(bin,10);
        // cout << "\n";
        for(int i=0;i<60;i++)
        {
            ll extra=b[i];
            if(bin[i] && b[i])
            extra--;
            b[i+1]+=extra/2;
        }
        // printarr(b,10);
        // cout << "\n";
        n=temp;
        ll ans=0;
        for(int i=0;i<60;i++)
        {
            if(b[i]<bin[i]){
                ans+=(bin[i]-b[i]+1)/2;
                b[i+1]-=(bin[i]-b[i]+1)/2;
            }
        }
        out1(ans);
    }
}


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

#define fi(a,b) for(int i=a;i<b;i++)
#define fj(a,b) for(int j=a;j<b;j++)
#define tc() int t; cin>>t; while(t--)
#define ll long long
#define ull unsigned long long
#define vit vector<int>::iterator it
typedef vector<int> vi;
typedef vector< pair<int,int> > vii;
typedef vector<int> vl;
typedef vector< pair<long,long> > vll;

ull power(int x, int y) {
  ull res = 1;
  while (y > 0) {
    if (y & 1)
      res = res*x;
    y = y>>1;
    x = x*x;
  }
  return res;
}

int main(){
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  #endif
  tc(){
    ull n,m;
    cin>>n>>m;
    vector<int>a(m);
    map<int,int>mp;
    ull sum=0;
    fi(0,m){
      cin>>a[i];
      sum+=a[i];
      mp[a[i]]++;
    }
    if(sum<n)
      cout<<-1<<'\n';
    else{
      ll ans=0;
      ll count=0;
      while(n>0){
        if(n & 1){
          if(mp[power(2,count)]){
            //cout<<"HRER"<<endl;
            mp[power(2,count)]--;
            mp[power(2,count+1)]+=mp[power(2,count)]/2;
          }
          else{
            ll temp=count;
            while(!mp[power(2,temp)]){
              temp++;
            }
            ans+=temp-count;
            while(temp!=count){
              mp[power(2,temp-1)]+=mp[power(2,temp)]/2;
              temp--;
            }
          }
        }
        else{
          mp[power(2,count+1)]+=mp[power(2,count)]/2;
        }
        n=n>>1;
        count++;
      }
      cout<<ans<<'\n';
    }

  }
}

//Modular Exponentiation O(Log y)
/*int power(int x, unsigned int y, int MODL) {
  int res = 1;
  x = x % MODL;
  while (y > 0) {
    if (y & 1)
      res = (res*x) % MODL;
    y = y>>1;
    x = (x*x) % MODL;
  }
  return res;
}

//Fermat's Little theorem, O(Log m)
int modInverse(int a, int m) {
     return power(a, m-2, m);
}*/


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

#define   fi(a,b) for(int i=a;i<b;i++)
#define   fj(a,b) for(int j=a;j<b;j++)
#define   ff first
#define   ss second
#define   ll long long
#define   ld long double
#define   ull unsigned long long
#define   bp(x) __builtin_popcount(x)
#define   pr(x) for(auto it: x) cout<<it<<" "; cout<<endl;
#define   getMax(x) max_element(x.begin(),x.end())
#define   getMin(x) min_element(x.begin(),x.end())
#define   endl "\n"
typedef   vector<int> vi;
typedef   vector< pair<int,int> > vii;
typedef   vector<long long> vl;
typedef   pair<int,int> pii;
typedef   pair<ll,ll> pll;
typedef   vector< pair<ll,ll> > vll;
//int dx[]={1,0,-1,0};
//int dy[]={0,1,0,-1};
//int dx[]={-1,0,1,1,1,0,-1,-1};
//int dy[]={-1,-1,-1,0,1,1,1,0};

void nikal_pehli_fursat_mai(){
    ll n,m;
    cin>>n>>m;
    vl a(m);
    ll sum = 0;
    vl arr(64, 0);
    fi(0,m){
        cin>>a[i];
        sum += a[i];
        ll temp = log2(a[i]);
        arr[temp]++;
    }
    if(sum<n){
        cout<<-1<<endl;
        return;
    }
    ll ans = 0;
    for(ll i=0; i<63; i++){
        if((n>>i) & 1){
            int k = i;
            while(arr[k]==0) k++;
            arr[k]--;
            fj(i, k) arr[j]++;
            ans += k-i;
        }
        arr[i+1] += arr[i]/2;
    }
    cout<<ans<<endl;
}

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


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

#define fi(a,b) for(int i=a;i<b;i++)
#define fj(a,b) for(int j=a;j<b;j++)
#define tc() int t; cin>>t; while(t--)
#define ll long long
#define ull unsigned long long
#define vit vector<int>::iterator it
typedef vector<int> vi;
typedef vector< pair<int,int> > vii;
typedef vector<int> vl;
typedef vector< pair<long,long> > vll;

ull power(ll x, ll y) {
  ull res = 1;
  while (y > 0) {
    if (y & 1)
      res = res*x;
    y = y>>1;
    x = x*x;
  }
  return res;
}

int main(){
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  #endif
  tc(){
    ull n,m;
    cin>>n>>m;
    vector<int>a(m);
    map<int,int>mp;
    ull sum=0;
    fi(0,m){
      cin>>a[i];
      sum+=a[i];
      mp[a[i]]++;
    }
    if(sum<n)
      cout<<-1<<'\n';
    else{
      ll ans=0;
      ll count=0;
      while(n>0){
        if(n & 1){
          if(mp[power((ull)2,count)]){
            //cout<<"HRER"<<endl;
            mp[power((ull)2,count)]--;
            mp[power((ull)2,count+1)]+=mp[power((ull)2,count)]/2;
            mp[power((ull)2,count)]=mp[power((ull)2,count)]%2;
          }
          else{
            ll temp=count;
            while(!mp[power((ull)2,temp)]){
              temp++;
            }
            ans+=temp-count;
            mp[power((ull)2,temp)]--;
            while(temp!=count){
              temp--;
              mp[power((ull)2,temp)]++;
            }
          }
        }
        else{
          mp[power((ull)2,count+1)]+=mp[power((ull)2,count)]/2;
          mp[power((ull)2,count)]=mp[power((ull)2,count)]%2;
        }
        n=n>>1;
        count++;
      }
      cout<<ans<<'\n';
    }

  }
}

//Modular Exponentiation O(Log y)
/*int power(int x, unsigned int y, int MODL) {
  int res = 1;
  x = x % MODL;
  while (y > 0) {
    if (y & 1)
      res = (res*x) % MODL;
    y = y>>1;
    x = (x*x) % MODL;
  }
  return res;
}

//Fermat's Little theorem, O(Log m)
int modInverse(int a, int m) {
     return power(a, m-2, m);
}*/


#include<bits/stdc++.h>
using namespace std;
#define sz 18409202
#define sz1 100003
#define sc scanf
#define pf printf
#define ll long long int
#define Mx 1000000007
#define PI 2*acos(0)
//bool isPrime[sz];
//bool cmp(pair<ll,ll>a, pair<ll,ll>b)

int main()
{
    ll n,m,x=0,y=0;
    cin>>n>>m;
    string st,st1;
    cin>>st>>st1;
    for(ll i=0; i<n; i++){
        if(st[i]!=st1[i]){
            x=i;
            break;
        }
    }
    ll p=n-1;
    for(ll i=m-1; i>=0; i--){
        if(st1[i]!=st[p]){
            y=i;
            break;
        }
    }
    if(y<x)
        swap(x,y);
    for(ll i=x; i<=y; i++){
        if(!isalpha(st1[i])){
            cout<<"NO"<<endl;
            return 0;
        }
    }
    if(x==y && st!=st1){
        cout<<"NO"<<endl;
        return 0;
    }
    cout<<"YES"<<endl;
    //cout<<x<<" "<<y<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define sz 18409202
#define sz1 100003
#define sc scanf
#define pf printf
#define ll long long int
#define Mx 1000000007
#define PI 2*acos(0)
//bool isPrime[sz];
//bool cmp(pair<ll,ll>a, pair<ll,ll>b)

int main()
{
    ll n,n1,x=1,y=1;
    cin>>n>>n1;
    string st,st1,st2="",st3="",st4="",st5="";
    cin>>st>>st1;
    if((n-n1)>1)
    {
        cout<<"NO"<<endl;
        return 0;
    }
    if(st==st1){
        cout<<"YES"<<endl;
        return 0;
    }
    for(ll i=0; i<n; i++){
        if(st[i]=='*'){
            x=i+1;
            y=i-1;
            break;
        }
        st2=st2+st[i];
    }
    //cout<<st2<<endl;
    for(ll i=x; i<n; i++){
        st3=st3+st[i];
    }
    ll n2=st3.size();
    for(ll i=0; i<x-1; i++){
        st4=st4+st1[i];
    }
    for(ll i=n1-1; i>=0; i--){
        //st5=st5+st1[i];
        if(st5.size()==n2 || i==y){
            y=i+1;
            break;
        }
        st5=st1[i]+st5;
    }
    //cout<<x-1<<" "<<y<<endl;
    if(y>x)
        swap(x,y);
    for(ll i=x-1; i<=y; i++){
        if(!isalpha(st1[i])){
            cout<<"NO"<<endl;
            return 0;
        }
    }
    //cout<<st2<<" "<<st3<<endl;
    //cout<<st4<<" "<<st5<<endl;
    if(st2==st4 && st3==st5){
        cout<<"YES"<<endl;
        return 0;
    }
    cout<<"NO"<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define sz 18409202
#define sz1 100003
#define sc scanf
#define pf printf
#define ll long long int
#define Mx 1000000007
#define PI 2*acos(0)
//bool isPrime[sz];
//bool cmp(pair<ll,ll>a, pair<ll,ll>b)

int main()
{
    ll n,m,x=0,y=0,c=0,d=0;
    cin>>n>>m;
    string st,st1;
    cin>>st>>st1;
    if((n-m)>1)
    {
        cout<<"NO"<<endl;
        return 0;
    }
    ll f=0;
    for(ll i=0; i<n; i++){
        if(st[i]!=st1[i]){
            x=i;
            if(st[i]=='*')
                f=1;
            break;
        }
        d++;
    }
    ll p=n-1;
    for(ll i=m-1; i>=0; i--){
        if(st1[i]!=st[p]){
            y=i;
            break;
        }
        p--;
        c++;
    }
    if(y<x)
        swap(x,y);
    //cout<<x<<" "<<y<<endl;
    if(y==m)
        y--;
    for(ll i=x; i<=y; i++){
        if(!isalpha(st1[i])){
            cout<<"NO"<<endl;
            return 0;
        }
    }
    //cout<<x<<" "<<y<<" "<<c<<" "<<d<<endl;
    /*if(x==y && st!=st1){
        cout<<"NO"<<endl;
        return 0;
    }*/
    if(st==st1){
        cout<<"YES"<<endl;
        return 0;
    }
    if(((c+d)!=n-1) || (n==1 && n==m && st!=st1 && st!="*")){
        cout<<"NO"<<endl;
        return 0;
    }
    if(n==m && f==0 && n>1){
        cout<<"NO"<<endl;
        return 0;
    }
    cout<<"YES"<<endl;
    //cout<<x<<" "<<y<<" "<<c<<" "<<d<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define sz 18409202
#define sz1 100003
#define sc scanf
#define pf printf
#define ll long long int
#define Mx 1000000007
#define PI 2*acos(0)
//bool isPrime[sz];
//bool cmp(pair<ll,ll>a, pair<ll,ll>b)

int main()
{
    ll n,n1,x=1,y=1;
    cin>>n>>n1;
    string st,st1,st2="",st3="",st4="",st5="";
    cin>>st>>st1;
    if((n-n1)>1)
    {
        cout<<"NO"<<endl;
        return 0;
    }
    if(st==st1){
        cout<<"YES"<<endl;
        return 0;
    }
    for(ll i=0; i<n; i++){
        if(st[i]=='*'){
            x=i+1;
            y=i-1;
            break;
        }
        st2=st2+st[i];
    }
    //cout<<st2<<endl;
    for(ll i=x; i<n; i++){
        st3=st3+st[i];
    }
    ll n2=st3.size();
    for(ll i=0; i<x-1; i++){
        st4=st4+st1[i];
    }
    for(ll i=n1-1; i>=0; i--){
        //st5=st5+st1[i];
        if(st5.size()==n2 || i==y){
            y=i+1;
            break;
        }
        st5=st1[i]+st5;
    }
    //cout<<x-1<<" "<<y<<endl;
    if(y<x)
        swap(x,y);
    for(ll i=x-1; i<=y; i++){
        if(!isalpha(st1[i])){
            cout<<"NO"<<endl;
            return 0;
        }
    }
    //cout<<st2<<" "<<st3<<endl;
    //cout<<st4<<" "<<st5<<endl;
    if(st2==st4 && st3==st5){
        cout<<"YES"<<endl;
        return 0;
    }
    cout<<"NO"<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define sz 18409202
#define sz1 100003
#define sc scanf
#define pf printf
#define ll long long int
#define Mx 1000000007
#define PI 2*acos(0)
//bool isPrime[sz];
//bool cmp(pair<ll,ll>a, pair<ll,ll>b)

int main()
{
    ll n,m,x=0,y=0;
    cin>>n>>m;
    string st,st1;
    cin>>st>>st1;
    for(ll i=0; i<n; i++){
        if(st[i]!=st1[i]){
            x=i;
            break;
        }
    }
    ll p=n-1;
    for(ll i=m-1; i>=0; i--){
        if(st1[i]!=st[p]){
            y=i;
            break;
        }
    }
    if(y<x)
        swap(x,y);
    for(ll i=x; i<=y; i++){
        if(!isalpha(st1[i])){
            cout<<"NO"<<endl;
            return 0;
        }
    }
    if(x==y){
        cout<<"NO"<<endl;
        return 0;
    }
    cout<<"YES"<<endl;
    //cout<<x<<" "<<y<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define sz 18409202
#define sz1 100003
#define sc scanf
#define pf printf
#define ll long long int
#define Mx 1000000007
#define PI 2*acos(0)
//bool isPrime[sz];
//bool cmp(pair<ll,ll>a, pair<ll,ll>b)

int main()
{
    ll n,n1,x=1,y=1;
    cin>>n>>n1;
    string st,st1,st2="",st3="",st4="",st5="";
    cin>>st>>st1;
    if((n-n1)>1)
    {
        cout<<"NO"<<endl;
        return 0;
    }
    if(st==st1){
        cout<<"YES"<<endl;
        return 0;
    }
    for(ll i=0; i<n; i++){
        if(st[i]=='*'){
            x=i+1;
            y=i-1;
            break;
        }
        st2=st2+st[i];
    }
    //cout<<st2<<endl;
    for(ll i=x; i<n; i++){
        st3=st3+st[i];
    }
    ll n2=st3.size();
    for(ll i=0; i<x-1; i++){
        st4=st4+st1[i];
    }
    for(ll i=n1-1; i>=0; i--){
        //st5=st5+st1[i];
        if(st5.size()==n2 || i==y){
            y=i+1;
            break;
        }
        st5=st1[i]+st5;
    }
    //cout<<x-1<<" "<<y<<endl;
    for(ll i=x-1; i<=y; i++){
        if(!isalpha(st1[i])){
            cout<<"NO"<<endl;
            return 0;
        }
    }
    //cout<<st2<<" "<<st3<<endl;
    //cout<<st4<<" "<<st5<<endl;
    if(st2==st4 && st3==st5){
        cout<<"YES"<<endl;
        return 0;
    }
    cout<<"NO"<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define sz 18409202
#define sz1 100003
#define sc scanf
#define pf printf
#define ll long long int
#define Mx 1000000007
#define PI 2*acos(0)
//bool isPrime[sz];
//bool cmp(pair<ll,ll>a, pair<ll,ll>b)

int main()
{
    ll n,m,x=0,y=0;
    cin>>n>>m;
    string st,st1;
    cin>>st>>st1;
    if((n-m)>1)
    {
        cout<<"NO"<<endl;
        return 0;
    }
    for(ll i=0; i<n; i++){
        if(st[i]!=st1[i]){
            x=i;
            break;
        }
    }
    ll p=n-1;
    for(ll i=m-1; i>=0; i--){
        if(st1[i]!=st[p]){
            y=i;
            break;
        }
    }
    if(y<x)
        swap(x,y);
    for(ll i=x; i<=y; i++){
        if(!isalpha(st1[i])){
            cout<<"NO"<<endl;
            return 0;
        }
    }
    if(x==y && st!=st1){
        cout<<"NO"<<endl;
        return 0;
    }
    cout<<"YES"<<endl;
    //cout<<x<<" "<<y<<endl;

}


#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORR(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define maxn 200005
using namespace std;
int x, y;
string s, t, h;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    //freopen(Task".inp", "r", stdin);
    cin >> x >> y;
    cin >> s >> t;
    if(s.find("*")==-1)
    {
        if(s!=t) cout <<"NO";
    }
    else
    if(s.length()-1 > t.length()) cout <<"NO";
    else{
        FOR(i,0,s.length()-1)
            if(s[i]=='*') {id = i; break;}
        FOR(i,0,id-1)
        {
            if(s[i]!=t[i])
            {
                cout <<"NO";
                return 0;
            }
        }
        int j = t.length()-1;
        FOR(i,s.length()-1,id+1)
        {
            if(s[i]!=t[j]) {cout <<"NO"; return 0;};
            j--;
        }
    cout <<"YES";

}
}

}


#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORR(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define maxn 10005
using namespace std;
int n, m;
string s, t;
int main()
{
    ios_base::sync_with_stdio(0);
    //freopen("inp.txt", "r", stdin);
    cin >> n >> m;
    cin >> s >> t;

    if(s.find("*") == -1)
    {
        cout << "NO";
        return 0;
    }
    if(s.size()-1 > t.size())
    {
        cout << "NO";
        return 0;
    }
    cout << "YES";
}


#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORR(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define maxn 10005
using namespace std;
int n, m;
string s, t, s2, s1;
int main()
{
    ios_base::sync_with_stdio(0);
    //freopen("inp.txt", "r", stdin);
    cin >> n >> m;
    cin >> s >> t;
    if(s.find("*") == -1)
    {
        cout << "NO";
        return 0;
    }
    if(s.size()-1 > t.size())
    {
        cout << "NO";
        return 0;
    }
    FOR(i, 0, s.size()-1) if(s[i] == '*')
    {
        s1 = s.substr(0, i);
        s2 = s.substr(i+1, n-i);
        if(t.find(s1) != 1 && t.find(s2) != 1)
        {
            cout << "YES";
            return 0;
        }
    }
    cout << "NO";
}


#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORR(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define maxn 10005
using namespace std;
int x, y;
string s, t, s2, s1;
int main()
{
    ios_base::sync_with_stdio(0);
    //freopen("inp.txt", "r", stdin);
     cin >> x >> y;
    cin >> s >> t;
    if(s.find("*")==-1)
    {
        if(s!=t) cout <<"NO";
    }
    else
    if(s.length()-1 > t.length()) cout <<"NO";
    else{
        FOR(i,0,s.length()-1)
            if(s[i]=='*') break;
            else
                if(s[i]!=t[i]) { cout <<"NO"; return 0;}
        FOR(i,0,s.length()-1)
            if(s[s.length()-i]=='*') break;
            else
                if(s[s.length()-i]!=t[t.length()-i])
            {
                cout <<"NO";
                return 0;
            }
            cout <<"YES";
    }

}


#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORR(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define maxn 10005
using namespace std;
int n, m;
string s, t;
int main()
{
    ios_base::sync_with_stdio(0);
    //freopen("inp.txt", "r", stdin);
    cin >> n >> m;
    cin >> s >> t;
    if(s.size() > t.size())
    {
        cout << "NO";
        return 0;
    }
    if(s.find("*") == -1)
    {
        cout << "NO";
        return 0;
    }
    cout << "YES";
}


#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORR(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define maxn 200005
using namespace std;
int x, y, id;
string s, t, h;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    //freopen(Task".inp", "r", stdin);
    //freopen(Task".out", "w", stdout);
    cin >> x >> y;
    cin >> s >> t;
    if(s.find("*")==-1)
    {
        if(s!=t) cout <<"NO";
    }
    else
    if(s.length()-1 > t.length()) cout <<"NO";
    else{
        FOR(i,0,s.length()-1)
            if(s[i]=='*') {id = i; break;}
        FOR(i,0,id-1)
        {
            if(s[i]!=t[i])
            {
                cout <<"NO";
                return 0;
            }
        }
        int j = t.length()-1;
        FOR(i,s.length()-1,id+1)
        {
            if(s[i]!=t[j]) {cout <<"NO"; return 0;};
            j--;
        }
    cout <<"YES";

}
}


#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORR(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define maxn 10005
using namespace std;
int n, m;
string s, t, s2, s1;
int main()
{
    ios_base::sync_with_stdio(0);
    //freopen("inp.txt", "r", stdin);
    cin >> n >> m;
    cin >> s >> t;
    if(s.find("*") == -1)
    {
        if(s != t) cout << "NO";
        return 0;
    }
    if(s.length()-1 > t.length()) cout <<"NO";
    else
    {
        FOR(i,0,s.length()-1)
            if(s[i]=='*') break;
            else
                if(s[i]!=t[i])
                {
                    cout <<"NO";
                    return 0;
                }
    }
    FOR(i,0,s.length()-1)
            if(s[s.length()-i]=='*') break;
            else
                if(s[s.length()-i]!=t[t.length()-i])
            {
                cout <<"NO";
                return 0;
            }
            cout <<"YES";
}


#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORR(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define maxn 10005
using namespace std;
int n, m;
string s, t, s2, s1;
int main()
{
    ios_base::sync_with_stdio(0);
    //freopen("inp.txt", "r", stdin);
    cin >> n >> m;
    cin >> s >> t;
    if(s.find("*") == -1)
    {
        cout << "NO";
        return 0;
    }
    FOR(i, 0, s.size()-1) if(s[i] == '*')
    {
        s1 = s.substr(0, i);
        s2 = s.substr(i+1, n-i);
        if(t.find(s1) != 1 && t.find(s2) != 1)
        {
            cout << "YES";
            return 0;
        }
    }
    cout << "NO";
}


#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORR(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define maxn 200005
using namespace std;
string sa , sb ;
long n , m , id = -1 ;
void nhap()
{
    cin >> n >> m ;
    cin >> sa ;
    cin >> sb ;
}
bool check ()
{
    if ( n - m >= 2 ) return false ;
    for ( long i = 0 ; i < id ; i ++ )
    {
        if ( sa[i] != sb[i] ) return false ;
    }
    long j = sb.size() - 1 ;
    for ( long i = sa.size() - 1; i > id ; i --   )
    {
        if ( sa[i] != sb[j] ) return false ;
        j -- ;
    }
    return true ;
}
void xl()
{
    for ( long i = 0 ; i < sa.size() ; i ++ )
    {
        if ( sa[i] == '*' )
        {
            id = i ;
            break ;
        }
    }
    if ( id == -1 )
    {
        if ( n != m )
        {
            cout <<"NO";
            return ;
        }
        for ( long i = 0 ; i < sa.size() ; i ++ )
        {
            if ( sa[i] != sb[i] )
            {
                cout <<"NO";
                return ;
            }
        }
        cout <<"YES" ;
        return ;
    }
    if ( check() )
        cout <<"YES" ;
    else
        cout <<"NO" ;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen(ZED".inp" , "r" , stdin ) ;
    nhap();
    xl() ;
    return 0;
}


#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORR(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define maxn 10005
using namespace std;
int n, m;
string s, t, s2, s1;
int main()
{
    ios_base::sync_with_stdio(0);
    freopen("inp.txt", "r", stdin);
    cin >> n >> m;
    cin >> s >> t;
    if(s.find("*") == -1)
    {
        cout << "NO";
        return 0;
    }
    if(s.size()-1 > t.size())
    {
        cout << "NO";
        return 0;
    }
    FOR(i, 0, s.size()-1) if(s[i] == '*')
    {
        s1 = s.substr(0, i);
        s2 = s.substr(i+1, n-i);
        if(t.find(s1) != 1 && t.find(s2) != 1)
        {
            cout << "YES";
            return 0;
        }
    }
    cout << "NO";
}


#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORR(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define maxn 200005
using namespace std;
int x, y;
string s, t, h;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    //freopen(Task".inp", "r", stdin);
    //freopen(Task".out", "w", stdout);
    cin >> x >> y;
    cin >> s >> t;
    if(s.find("*")==-1)
    {
        if(s!=t) cout <<"NO";
    }
    else
    if(s.length()-1 > t.length()) cout <<"NO";
    else{
        FOR(i,0,s.length()-1)
            if(s[i]=='*') break;
            else
                if(s[i]!=t[i]) { cout <<"NO"; return 0;}
        FOR(i,0,s.length()-1)
            if(s[s.length()-i]=='*') break;
            else
                if(s[s.length()-i]!=t[t.length()-i])
            {
                cout <<"NO";
                return 0;
            }
            cout <<"YES";
    }

}


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

int main(){
	long long int n, m;
	cin>>n>>m;
	string s, t;
	cin>>s>>t;
	long long int i=0, j=0;
	bool front = false;
	bool back = false;
	bool star = false;
	int star_i;
	while(i<n){
		if(s[i] == t[i]){
			i++;
		}
		else if(s[i] != t[i] && s[i] == '*'){
			star = true;
			front = true;
			star_i = i;
			break;
		}
		else if(s[i] != t[i]){
			break;
		}
	}
	if(i == n)
	front = true;

	if(front && star){

		i = n-1;
		j = m-1;
		while(s[i] != '*'){
			if(s[i] == t[j]){
				i--;
				j--;
				back = true;
			}
			else{
				back = false;
				break;
			}
		}
		if(j < star_i-1)
		back = false;
	}

	if(front && back)
	cout<<"YES";
	else
	cout<<"NO";

	return 0;
}


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

int main(){
	long long int n, m;
	cin>>n>>m;
	string s, t;
	cin>>s>>t;
	long long int i=1, j=1;
	if(s[0] == t[0]){

	while(i<n && j<m){

		if((s[i] == t[j]) && (s[i-1] != '*')){
			i++;
			j++;
		}
		else if(s[i] == '*'){
			i++;
		}
		else if(s[i] != t[j])
		j++;

	}
}
	if(i==n && j==m)
	cout<<"YES";
	else
	cout<<"NO";

	return 0;
}


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

int main(){
	long long int n, m;
	cin>>n>>m;
	string s, t;
	cin>>s>>t;
	long long int i=1, j=1;

	while(i<n && j<m){

		if((s[i] == t[j]) && (s[i-1] != '*')){
			i++;
			j++;
		}
		else if(s[i] == t[j]){
			i++;
			j++;
		}
		else if(s[i] == '*'){
			i++;
		}
		else if(s[i] != t[j])
		j++;

	}

	if(i==n && j==m)
	cout<<"YES";
	else
	cout<<"NO";

	return 0;
}


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

int main(){
	long long int n, m;
	cin>>n>>m;
	string s, t;
	cin>>s>>t;
	long long int i=0, j=0;
	bool front = false;
	bool back = true;
	bool star = false;
	int star_i;
	while(i<n){
		if(s[i] == t[i]){
			i++;
		}
		else if(s[i] != t[i] && s[i] == '*'){
			star = true;
			front = true;
			star_i = i;
			break;
		}
		else if(s[i] != t[i]){
			break;
		}
	}
	if(i == n)
	front = true;

	if(front && star){

		i = n-1;
		j = m-1;
		while(s[i] != '*'){
			if(s[i] == t[j]){
				i--;
				j--;
			}
			else{
				back = false;
				break;
			}
		}
		if(j < star_i-1)
		back = false;
	}

	if(front && back)
	cout<<"YES";
	else
	cout<<"NO";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n, m;
	cin>>n>>m;
	string s, t;
	cin>>s>>t;
	int i=0, j=0;
	while(i<n){
		if(s[i] == t[j]){
			i++;
			j++;
		}
		else if(s[i] == '*'){
			i++;
		}
		else if(s[i] != t[j])
		j++;
	}
	if(i==n && j==m)
	cout<<"YES";
	else
	cout<<"NO";

	return 0;
}


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

int main(){
	long long int n, m;
	cin>>n>>m;
	string s, t;
	cin>>s>>t;
	long long int i=0, j=0;
	while(i<n || j<m){

		if(s[i] == t[j]){
			i++;
			j++;
		}
		else if(s[i] == '*'){
			i++;
		}
		else if(s[i] != t[j])
		j++;

	}
	if(i==n && j==m)
	cout<<"YES";
	else
	cout<<"NO";

	return 0;
}


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

int main(){
	long long int n, m;
	cin>>n>>m;
	string s, t;
	cin>>s>>t;
	long long int i=0, j=0;

	while(i<n && j<m){

		if((s[i] == t[j]) && (s[i-1] == '*')){
			j++;
		}
		else if(s[i] == t[j]){
			i++;
			j++;
		}
		else if(s[i] == '*'){
			i++;
		}
		else if(s[i] != t[j])
		j++;

	}

	if(i==n && j==m)
	cout<<"YES";
	else
	cout<<"NO";

	return 0;
}


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

int main(){
	long long int n, m;
	cin>>n>>m;
	string s, t;
	cin>>s>>t;
	long long int i=0, j=0;
	bool front = false;
	bool back = false;
	bool star = false;
	int star_i;
	if(s == "*"){
		cout<<"YES";
	}
	else{

	while(i<n){
		if(s[i] == t[i]){
			i++;
		}
		else if(s[i] != t[i] && s[i] == '*'){
			star = true;
			front = true;
			star_i = i;
			break;
		}
		else if(s[i] != t[i]){
			break;
		}
	}
	if(i == n)
	front = true;

	if(front && star){

		i = n-1;
		j = m-1;
		while(s[i] != '*'){
			if(s[i] == t[j]){
				i--;
				j--;
				back = true;
			}
			else{
				back = false;
				break;
			}
		}
		if(j < star_i-1)
		back = false;
	}

	if(front && back)
	cout<<"YES";
	else
	cout<<"NO";
}
	return 0;
}


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

int main(){
	long long int n, m;
	cin>>n>>m;
	string s, t;
	cin>>s>>t;
	long long int i=0, j=0;
	while(i<n || j<m){

		if(s[i] == t[j]){
			i++;
			j++;
		}
		else if(s[i] == '*'){
			i++;
		}
		else if(s[i] != t[j] && j<m)
		j++;

	}
	if(i==n && j==m)
	cout<<"YES";
	else
	cout<<"NO";

	return 0;
}


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

int main(){
	long long int n, m;
	cin>>n>>m;
	string s, t;
	cin>>s>>t;
	long long int i=0, j=0;
	while(i<n && j<m){

		if(s[i] == t[j]){
			i++;
			j++;
		}
		else if(s[i] == '*'){
			i++;
		}
		else if(s[i] != t[j])
		j++;

	}
	if(i==n && j==m)
	cout<<"YES";
	else
	cout<<"NO";

	return 0;
}


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

int main(){
	long long int n, m;
	cin>>n>>m;
	string s, t;
	cin>>s>>t;
	long long int i=0, j=0;
	bool front = false;
	bool back = true;
	bool star = false;
	int star_i;

	while(i<n){
		if(s[i] == t[i]){
			i++;
		}
		else if(s[i] != t[i] && s[i] == '*'){
			star = true;
			front = true;
			star_i = i;
			break;
		}
		else if(s[i] != t[i]){
			break;
		}
	}
	if(i == n)
	front = true;

	if(front && star){

		i = n-1;
		j = m-1;
		while(s[i] != '*'){
			if(s[i] == t[j]){
				i--;
				j--;
			}
			else{
				back = false;
				break;
			}
		}
		if(j < star_i-1)
		back = false;
	}
	if(star == false){
		if(s != t)
		back = false;
	}

	if(front && back)
	cout<<"YES";
	else
	cout<<"NO";

	return 0;
}


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

int main(){
	long long int n, m;
	cin>>n>>m;
	string s, t;
	cin>>s>>t;
	long long int i=0, j=0;
	while(i<n || j<m){

		if(s[i] == t[j]){
			i++;
			j++;
		}
		else if(s[i] == '*'){
			i++;
		}
		else if(s[i] != t[j] && j<=m)
		j++;

	}
	if(i==n && j==m)
	cout<<"YES";
	else
	cout<<"NO";

	return 0;
}


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

#define F first
#define S second
#define pb push_back
#define mp make_pair
#define ll long long
#define Need_for_speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define sz size()

typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;

int a[100001];

int main(){
	int n,m,y=0;
	string s,t;
	cin>>n>>m>>s>>t;
	if(s==t){
		cout<<"YES";
		return 0;
	}
	for(int i=0;i<m;i++){
		if(s[y]!='*'){
			if(s[y]==t[i]){
				y++;
				s[y-1]='!';
				t[i]='@';
			}
			else {
				cout<<"NO";
				return 0;
			}
		}
		else{
			break;
		}
	}
	y=n-1;
	for(int i=m-1;i>=-1;i--){
		if(s[y]!='*'){
			if(s[y]==t[i]){
				s[y]='!';
				t[i]='@';
				y--;
			}
			else {
				cout<<"NO1";
				return 0;
			}
		}
		else{
			cout<<"YES";
			return 0;
		}
	}
	cout<<"NO2";
	return 0;
}