#include <bits/stdc++.h>

using namespace std;

#define all(x) begin(x), end(x)

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

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

    int h;
    cin >> h;
    ++h;
    vi a(h);

    int n = 0;
    for (int i = 0; i < h; ++i) {
        cin >> a[i];
        n += a[i];
    }

    vi left_par(n), right_par(n);
    left_par[0] = right_par[0] = -1;
    int s = 1;
    for (int i = 1; i < h; ++i) {
        int old_s = s - a[i - 1];
        int e = s + a[i];

        left_par[s] = old_s;
        right_par[s] = old_s;

        for (int j = s + 1; j < e; ++j) {
            left_par[j] = old_s;
            right_par[j] = s - 1;
        }

        s = e;
    }

    if (left_par == right_par) {
        cout << "perfect\n";
    } else {
        cout << "ambiguous\n";
        for (int i = 0; i < n; ++i) {
            cout << left_par[i] + 1 << ' ';
        }
        cout << '\n';
        for (int i = 0; i < n; ++i) {
            cout << right_par[i] + 1 << ' ';
        }
        cout << '\n';
    }

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

void solve() {
	int n;
	cin >> n;
	n++;
	vector<int> v(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i];
	}
	for (int i = 1; i < n; i++) {
		if (v[i - 1] != 1) {
			cout << "ambiguous\n";
			int ve = 0;
			for (int j = 0; j < n; j++) {
				for (int k = 0; k < v[j]; k++) {
					cout << ve << ' ';
				}
				ve += v[j];
			}
			cout << '\n';
			ve = 0;
			bool is = 1;
			for (int j = 0; j < n; j++) {
				for (int k = 0; k < v[j]; k++) {
					if (j == i && is) {
						cout << ve - 1 << ' ';
						is = 0;
					}
					else
						cout << ve << ' ';
				}
				ve += v[j];
			}
			return;
		}
	}
	cout << "perfect";
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int q = 1;
	//cin >> q;
	for (; q > 0; q--) {
		solve();
		cout << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

void solve() {
	int n;
	cin >> n;
	n++;
	vector<int> v(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i];
	}
	for (int i = 1; i < n; i++) {
		if (v[i - 1] != 1 && v[i] != 1) {
			cout << "ambiguous\n";
			int ve = 0;
			for (int j = 0; j < n; j++) {
				for (int k = 0; k < v[j]; k++) {
					cout << ve << ' ';
				}
				ve += v[j];
			}
			cout << '\n';
			ve = 0;
			bool is = 1;
			for (int j = 0; j < n; j++) {
				for (int k = 0; k < v[j]; k++) {
					if (j == i && is) {
						cout << ve - 1 << ' ';
						is = 0;
					}
					else
						cout << ve << ' ';
				}
				ve += v[j];
			}
			return;
		}
	}
	cout << "perfect";
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int q = 1;
	//cin >> q;
	for (; q > 0; q--) {
		solve();
		cout << endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define pa pair<int,int>
#define v1d vector<int>
#define v2d vector<vector<int>>
#define M 1000000007
#define mp make_pair
#define ff first
#define ss second
#define ump map<int,int>
#define endl "\n"
#define v1s vector<string>
#define S(v) sort(v.begin(),v.end())
#define mxpq priority_queue <int>
#define mnpq priority_queue<int,vector<int>,greater<int>>
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

void dfs(int i , v2d &v, v1d &isit , int &ver  ,int &edge){

    if(isit[i] == 1){
        return ;
    }

    isit[i]++;
    ver++;
    edge+=v[i].size();

    for(int j = 0 ; j < v[i].size() ; j++){
        if(isit[v[i][j]] == 0){
           dfs(v[i][j] ,v, isit , ver , edge);
        }
    }
}

int32_t main(){

    fio
    int n;cin>>n;

    v1d v(n+1);
    int i = 0;
    for( i = 0; i <= n ; i++){
        cin>>v[i];
    }

    for( i = 0 ; i < n ;  i++){
        if(v[i]!=1){
            break;
        }

    }
    // cout<<i<<": "<<n;
    if(i == n){
        cout<<"perfect";
    }else{
        cout<<"ambiguous"<<endl;

        int first = 0;
        int last = 0;
        int count = 0;
        for(int i =  0 ; i <= n ; i++){
            int k = last;

            for(int j =  0;j<v[i];j++){

                cout<<k<<" ";
                count++;
                if(j == 0){
                    first = count;
                }

                if(j == v[i] - 1){
                    last = count;
                }
            }

        }

        cout<<endl;

        first = 0;
        last = 0;
        count = 0;
        for(int i =  0 ; i <= n ; i++){
            int k = first;
            for(int j =  0;j<v[i];j++){

                cout<<k<<" ";
                count++;

                if(j == 0){
                    first = count;
                }

                if(j == v[i] - 1){
                    last = count;
                }
            }

        }
    }

}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define pa pair<int,int>
#define v1d vector<int>
#define v2d vector<vector<int>>
#define M 1000000007
#define mp make_pair
#define ff first
#define ss second
#define ump map<int,int>
#define endl "\n"
#define v1s vector<string>
#define S(v) sort(v.begin(),v.end())
#define mxpq priority_queue <int>
#define mnpq priority_queue<int,vector<int>,greater<int>>
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

void dfs(int i , v2d &v, v1d &isit , int &ver  ,int &edge){

    if(isit[i] == 1){
        return ;
    }

    isit[i]++;
    ver++;
    edge+=v[i].size();

    for(int j = 0 ; j < v[i].size() ; j++){
        if(isit[v[i][j]] == 0){
           dfs(v[i][j] ,v, isit , ver , edge);
        }
    }
}

int32_t main(){

    fio
    int n;cin>>n;

    v1d v(n+1);
    int i = 0;
    for( i = 0; i <= n ; i++){
        cin>>v[i];
    }
int y = 0;
    for( i = 0 ; i < n ;  i++){
        if(v[i]!=1){
            y++;
            if(y==2)
                break;
        }else{
            y=0;
        }

    }
    // cout<<i<<": "<<n;
    if(i == n){
        cout<<"perfect";
    }else{
        cout<<"ambiguous"<<endl;

        int first = 0;
        int last = 0;
        int count = 0;
        for(int i =  0 ; i <= n ; i++){
            int k = last;

            for(int j =  0;j<v[i];j++){

                cout<<k<<" ";
                count++;
                if(j == 0){
                    first = count;
                }

                if(j == v[i] - 1){
                    last = count;
                }
            }

        }

        cout<<endl;

        first = 0;
        last = 0;
        count = 0;
        for(int i =  0 ; i <= n ; i++){
            int k = first;
            int k1  = last;
            for(int j =  0;j<v[i];j++){

                if(j < v[i]/2){
                    cout<<k<<" ";
                }else{
                    cout<<k1<<" ";
                }
                count++;

                if(j == 0){
                    first = count;
                }

                if(j == v[i] - 1){
                    last = count;
                }
            }

        }
    }

}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define pa pair<int,int>
#define v1d vector<int>
#define v2d vector<vector<int>>
#define M 1000000007
#define mp make_pair
#define ff first
#define ss second
#define ump map<int,int>
#define endl "\n"
#define v1s vector<string>
#define S(v) sort(v.begin(),v.end())
#define mxpq priority_queue <int>
#define mnpq priority_queue<int,vector<int>,greater<int>>
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

void dfs(int i , v2d &v, v1d &isit , int &ver  ,int &edge){

    if(isit[i] == 1){
        return ;
    }

    isit[i]++;
    ver++;
    edge+=v[i].size();

    for(int j = 0 ; j < v[i].size() ; j++){
        if(isit[v[i][j]] == 0){
           dfs(v[i][j] ,v, isit , ver , edge);
        }
    }
}

int32_t main(){

    fio
    int n;cin>>n;

    v1d v(n+1);
    int i = 0;
    for( i = 0; i <= n ; i++){
        cin>>v[i];
    }

    for( i = 0 ; i < n ;  i++){
        if(v[i]!=1){
            break;
        }

    }
    // cout<<i<<": "<<n;
    if(i == n){
        cout<<"perfect";
    }else{
        cout<<"ambiguous"<<endl;

        int first = 0;
        int last = 0;
        int count = 0;
        for(int i =  0 ; i <= n ; i++){
            int k = last;

            for(int j =  0;j<v[i];j++){

                cout<<k<<" ";
                count++;
                if(j == 0){
                    first = count;
                }

                if(j == v[i] - 1){
                    last = count;
                }
            }

        }

        cout<<endl;

        first = 0;
        last = 0;
        count = 0;
        for(int i =  0 ; i <= n ; i++){
            int k = first;
            int k1  = last;
            for(int j =  0;j<v[i];j++){

                if(j < v[i]/2){
                    cout<<k<<" ";
                }else{
                    cout<<k1<<" ";
                }
                count++;

                if(j == 0){
                    first = count;
                }

                if(j == v[i] - 1){
                    last = count;
                }
            }

        }
    }

}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define pa pair<int,int>
#define v1d vector<int>
#define v2d vector<vector<int>>
#define M 1000000007
#define mp make_pair
#define ff first
#define ss second
#define ump map<int,int>
#define endl "\n"
#define v1s vector<string>
#define S(v) sort(v.begin(),v.end())
#define mxpq priority_queue <int>
#define mnpq priority_queue<int,vector<int>,greater<int>>
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

void dfs(int i , v2d &v, v1d &isit , int &ver  ,int &edge){

    if(isit[i] == 1){
        return ;
    }

    isit[i]++;
    ver++;
    edge+=v[i].size();

    for(int j = 0 ; j < v[i].size() ; j++){
        if(isit[v[i][j]] == 0){
           dfs(v[i][j] ,v, isit , ver , edge);
        }
    }
}

int32_t main(){

    fio
    int n;cin>>n;

    v1d v(n+1);
    int i = 0;
    for( i = 0; i <= n ; i++){
        cin>>v[i];
    }
     int y = 0;
    for( i = 0 ; i < n ;  i++){
        if(v[i]!=1){
            y++;
            if(y==2)
                break;
        }else{
            y=0;
        }

    }
    // cout<<i<<": "<<n;
    if(i == n){
        cout<<"perfect";
    }else{
        cout<<"ambiguous"<<endl;

        int first = 0;
        int last = 0;
        int count = 0;
        for(int i =  0 ; i <= n ; i++){
            int k = last;

            for(int j =  0;j<v[i];j++){

                cout<<k<<" ";
                count++;
                if(j == 0){
                    first = count;
                }

                if(j == v[i] - 1){
                    last = count;
                }
            }

        }

        cout<<endl;

        first = 0;
        last = 0;
        count = 0;
        for(int i =  0 ; i <= n ; i++){
            int k = first;
            int k1  = last;
            for(int j =  0;j<v[i];j++){

                if(j < v[i]/2){
                    cout<<k<<" ";
                }else{
                    cout<<k1<<" ";
                }
                count++;

                if(j == 0){
                    first = count;
                }

                if(j == v[i] - 1){
                    last = count;
                }
            }

        }
    }

}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define pa pair<int,int>
#define v1d vector<int>
#define v2d vector<vector<int>>
#define M 1000000007
#define mp make_pair
#define ff first
#define ss second
#define ump map<int,int>
#define endl "\n"
#define v1s vector<string>
#define S(v) sort(v.begin(),v.end())
#define mxpq priority_queue <int>
#define mnpq priority_queue<int,vector<int>,greater<int>>
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

void dfs(int i , v2d &v, v1d &isit , int &ver  ,int &edge){

    if(isit[i] == 1){
        return ;
    }

    isit[i]++;
    ver++;
    edge+=v[i].size();

    for(int j = 0 ; j < v[i].size() ; j++){
        if(isit[v[i][j]] == 0){
           dfs(v[i][j] ,v, isit , ver , edge);
        }
    }
}

int32_t main(){

    fio
    int n;cin>>n;

    v1d v(n+1);
    int i = 0;
    for( i = 0; i <= n ; i++){
        cin>>v[i];
    }
     int y = 0;
    for( i = 0 ; i <= n ;  i++){
        if(v[i]!=1){
            y++;
            if(y==2)
                break;
        }else{
            y=0;
        }

    }
    // cout<<i<<": "<<n;
    if(i == n+1){
        cout<<"perfect";
    }else{
        cout<<"ambiguous"<<endl;

        int first = 0;
        int last = 0;
        int count = 0;
        for(int i =  0 ; i <= n ; i++){
            int k = last;

            for(int j =  0;j<v[i];j++){

                cout<<k<<" ";
                count++;
                if(j == 0){
                    first = count;
                }

                if(j == v[i] - 1){
                    last = count;
                }
            }

        }

        cout<<endl;

        first = 0;
        last = 0;
        count = 0;
        for(int i =  0 ; i <= n ; i++){
            int k = first;
            int k1  = last;
            for(int j =  0;j<v[i];j++){

                if(j < v[i]/2){
                    cout<<k<<" ";
                }else{
                    cout<<k1<<" ";
                }
                count++;

                if(j == 0){
                    first = count;
                }

                if(j == v[i] - 1){
                    last = count;
                }
            }

        }
    }

}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#define N (1000005)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define sz(v) ((int)(v).size())
#define ed(v) v.end()
#define contains(v,i) (v.find(i)!=v.end())
#define forn(i,a,b) for(ll i = a; i < b; i++)
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define sq(v) ((a)*(a))
#define cb(v) ((a)*(a)*(a))

typedef long long int ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef vector<ll> vi;
typedef map<ll,ll> mll;

int main(){

    fast

    ll h;
    cin >> h;

    vi a(h+1);
    ll sum = 0;
    forn(i, 0, h+1){
      cin >> a[i];
    }

    bool perfect = true;
    forn(i, 0, h){
      if(a[i] != 1){
        perfect = false;
      }
    }

    if(perfect){
      cout << "perfect" << endl;
    } else {
      cout << "ambiguous" << endl;

      cout << 0 << " ";
      ll parent = 1;
      forn(i, 1, h+1){
        ll parentNext = 0;
        forn(j, 0, a[i]){
          parentNext = parent + j + 1;
          cout << parent << " ";
        }
        parent = parentNext;
      }
      cout << endl;

      cout << 0 << " ";
      parent = 1;
      forn(i, 1, h+1){
        ll parentNext = 0;
        forn(j, 0, a[i]){
          parentNext = parent + j + 1;
          if(i>0 && a[i-1] > 1){
            cout << parent - 1 << " ";
          } else {
            cout << parent << " ";
          }
        }
        parent = parentNext;
      }
      cout << endl;
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#define N (1000005)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define sz(v) ((int)(v).size())
#define ed(v) v.end()
#define contains(v,i) (v.find(i)!=v.end())
#define forn(i,a,b) for(ll i = a; i < b; i++)
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define sq(v) ((a)*(a))
#define cb(v) ((a)*(a)*(a))

typedef long long int ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef vector<ll> vi;
typedef map<ll,ll> mll;

int main(){

    fast

    ll h;
    cin >> h;

    vi a(h+1);
    ll sum = 0;
    forn(i, 0, h+1){
      cin >> a[i];
    }

    bool perfect = true;
    forn(i, 0, h){
      if(a[i] != 1 && a[i+1] != 1){
        perfect = false;
      }
    }

    if(perfect){
      cout << "perfect" << endl;
    } else {
      cout << "ambiguous" << endl;

      cout << 0 << " ";
      ll parent = 1;
      forn(i, 1, h+1){
        ll parentNext = 0;
        forn(j, 0, a[i]){
          parentNext = parent + j + 1;
          cout << parent << " ";
        }
        parent = parentNext;
      }
      cout << endl;

      cout << 0 << " ";
      parent = 1;
      forn(i, 1, h+1){
        ll parentNext = 0;
        forn(j, 0, a[i]){
          parentNext = parent + j + 1;

          cout << parent - min(j, a[i - 1] - 1) << " ";
        }
        parent = parentNext;
      }
      cout << endl;
    }

    return 0;
}


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

//cout
#define p(a)               cout<<a<<"\n"
#define yes                cout<<"YES\n"
#define no                 cout<<"NO\n"

//loop
#define rep(i,n)           for(int i=0;i<(n);++i)
#define drep(i,n)          for(int i=(n-1);i>=(0);--i)
#define rfor(i,a,b)        for(int i=(a);i<(b);++i)
#define dfor(i,a,b)        for(int i=(a);i>=(b);--i)

//vector
#define vi                 vector<int>
#define vll                vector<ll>
#define vpi                vector<pair<int,int>>
#define vpll               vector<pair<ll,ll>>
#define pb                 push_back

//pair
#define pi                 pair<int,int>
#define pll                pair<ll,ll>
#define mp                 make_pair
#define ff                 first
#define ss                 second

//map
#define mii                map<int, int>
#define mivi               map<int, vi>
#define mpii               map<pi, int>
#define mipi               map<int, pi>

//for sorting
#define sortvec(a)         sort(a.begin(),a.end())
#define dsortvec(a)        sort(a.begin(), a.end(), greater <int>())
#define sortarr(a, n)      sort(a, a+n)
#define dsortarr(a, n)     sort(a, a+n, greater <int>())

bool sort_by_second(const pair<int, int> &a, const pair<int, int> &b)
{
    if(a.second == b.second)
        return (a.first < b.first);
    return (a.second < b.second);
}

// Main function
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    //cin>>t;
    while(t--)
    {
        int h; cin>>h;
        int a[h+1];
        ll tot = 0, done;
        bool cnt22 = true;
        int ambigity;
        rep(i, h+1)
        {
            cin>>a[i];
            if(a[i]>=2 and i>0)
            {
                if(a[i-1]>=2)
                {
                    cnt22 = false;
                    ambigity = i;
                    done = tot;
                }
            }
            tot += a[i];
        }
        if(h<2 or h==tot-1 or cnt22)
            p("perfect");
        else //if(cnt22>1 or )
        {
            p("ambiguous");
            int cntr = 0;
            int parent = 0;
            vi seq;
            rep(outer, h+1)
            {
                rep(i, a[outer])
                {
                    cout<<parent<<" ";
                    seq.pb(parent);
                    ++cntr;
                }
                parent = cntr;
            }
            p("");
            ll dd = 1;
            rfor(i, 1, h+1)
            {
                if(dd==done)
                {
                    --seq[done];
                    break;
                }
                dd += a[i];
            }
            rep(i, seq.size())
                cout<<seq[i]<<" ";
            p("");
        }

    }
    return 0;
}


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

//cout
#define p(a)               cout<<a<<"\n"
#define yes                cout<<"YES\n"
#define no                 cout<<"NO\n"

//loop
#define rep(i,n)           for(int i=0;i<(n);++i)
#define drep(i,n)          for(int i=(n-1);i>=(0);--i)
#define rfor(i,a,b)        for(int i=(a);i<(b);++i)
#define dfor(i,a,b)        for(int i=(a);i>=(b);--i)

//vector
#define vi                 vector<int>
#define vll                vector<ll>
#define vpi                vector<pair<int,int>>
#define vpll               vector<pair<ll,ll>>
#define pb                 push_back

//pair
#define pi                 pair<int,int>
#define pll                pair<ll,ll>
#define mp                 make_pair
#define ff                 first
#define ss                 second

//map
#define mii                map<int, int>
#define mivi               map<int, vi>
#define mpii               map<pi, int>
#define mipi               map<int, pi>

//for sorting
#define sortvec(a)         sort(a.begin(),a.end())
#define dsortvec(a)        sort(a.begin(), a.end(), greater <int>())
#define sortarr(a, n)      sort(a, a+n)
#define dsortarr(a, n)     sort(a, a+n, greater <int>())

bool sort_by_second(const pair<int, int> &a, const pair<int, int> &b)
{
    if(a.second == b.second)
        return (a.first < b.first);
    return (a.second < b.second);
}

// Main function
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    //cin>>t;
    while(t--)
    {
        int h; cin>>h;
        int a[h+1];
        ll tot = 0;
        int cnt22 = 0;
        rep(i, h+1)
        {
            cin>>a[i];
            if(a[i]>=2)
                ++cnt22;
            tot += a[i];
        }
        if(h<2 or h==tot-1 or (cnt22==1))
            p("perfect");
        else //if(cnt22>1 or )
        {
            p("ambiguous");
            int cntr = 0;
            int parent = 0;
            vi seq;
            rep(outer, h+1)
            {
                rep(i, a[outer])
                {
                    cout<<parent<<" ";
                    seq.pb(parent);
                    ++cntr;
                }
                parent = cntr;
            }
            p("");
            ll done = 1;
            rfor(i, 1, h+1)
            {
                if(a[i-1]>=2)
                {
                    --seq[done];
                    break;
                }
                done += a[i];
            }
            rep(i, seq.size())
                cout<<seq[i]<<" ";
            p("");
        }

    }
    return 0;
}


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

//cout
#define p(a)               cout<<a<<"\n"
#define yes                cout<<"YES\n"
#define no                 cout<<"NO\n"

//loop
#define rep(i,n)           for(int i=0;i<(n);++i)
#define drep(i,n)          for(int i=(n-1);i>=(0);--i)
#define rfor(i,a,b)        for(int i=(a);i<(b);++i)
#define dfor(i,a,b)        for(int i=(a);i>=(b);--i)

//vector
#define vi                 vector<int>
#define vll                vector<ll>
#define vpi                vector<pair<int,int>>
#define vpll               vector<pair<ll,ll>>
#define pb                 push_back

//pair
#define pi                 pair<int,int>
#define pll                pair<ll,ll>
#define mp                 make_pair
#define ff                 first
#define ss                 second

//map
#define mii                map<int, int>
#define mivi               map<int, vi>
#define mpii               map<pi, int>
#define mipi               map<int, pi>

//for sorting
#define sortvec(a)         sort(a.begin(),a.end())
#define dsortvec(a)        sort(a.begin(), a.end(), greater <int>())
#define sortarr(a, n)      sort(a, a+n)
#define dsortarr(a, n)     sort(a, a+n, greater <int>())

bool sort_by_second(const pair<int, int> &a, const pair<int, int> &b)
{
    if(a.second == b.second)
        return (a.first < b.first);
    return (a.second < b.second);
}

// Main function
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    //cin>>t;
    while(t--)
    {
        int h; cin>>h;
        int a[h+1];
        ll tot = 0;
        int cnt22 = 0;
        rep(i, h+1)
        {
            cin>>a[i];
            if(a[i]>=2)
                ++cnt22;
            tot += a[i];
        }
        if(h<2 or h==tot-1 or (cnt22==1 and a[h]>=2))
            p("perfect");
        else //if(cnt22>1 or )
        {
            p("ambiguous");
            int cntr = 0;
            int parent = 0;
            vi seq;
            rep(outer, h+1)
            {
                rep(i, a[outer])
                {
                    cout<<parent<<" ";
                    seq.pb(parent);
                    ++cntr;
                }
                parent = cntr;
            }
            p("");
            ll done = 1;
            rfor(i, 1, h+1)
            {
                if(a[i-1]>=2)
                {
                    --seq[done];
                    break;
                }
                done += a[i];
            }
            rep(i, seq.size())
                cout<<seq[i]<<" ";
            p("");
        }

    }
    return 0;
}


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

//cout
#define p(a)               cout<<a<<"\n"
#define yes                cout<<"YES\n"
#define no                 cout<<"NO\n"

//loop
#define rep(i,n)           for(int i=0;i<(n);++i)
#define drep(i,n)          for(int i=(n-1);i>=(0);--i)
#define rfor(i,a,b)        for(int i=(a);i<(b);++i)
#define dfor(i,a,b)        for(int i=(a);i>=(b);--i)

//vector
#define vi                 vector<int>
#define vll                vector<ll>
#define vpi                vector<pair<int,int>>
#define vpll               vector<pair<ll,ll>>
#define pb                 push_back

//pair
#define pi                 pair<int,int>
#define pll                pair<ll,ll>
#define mp                 make_pair
#define ff                 first
#define ss                 second

//map
#define mii                map<int, int>
#define mivi               map<int, vi>
#define mpii               map<pi, int>
#define mipi               map<int, pi>

//for sorting
#define sortvec(a)         sort(a.begin(),a.end())
#define dsortvec(a)        sort(a.begin(), a.end(), greater <int>())
#define sortarr(a, n)      sort(a, a+n)
#define dsortarr(a, n)     sort(a, a+n, greater <int>())

bool sort_by_second(const pair<int, int> &a, const pair<int, int> &b)
{
    if(a.second == b.second)
        return (a.first < b.first);
    return (a.second < b.second);
}

// Main function
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    //cin>>t;
    while(t--)
    {
        int h; cin>>h;
        int a[h+1];
        ll tot = 0;
        bool cnt22 = true;
        rep(i, h+1)
        {
            cin>>a[i];
            if(a[i]>=2 and i>0)
            {
                if(a[i-1]>=2)
                    cnt22 = false;
            }
            tot += a[i];
        }
        if(h<2 or h==tot-1 or cnt22)
            p("perfect");
        else //if(cnt22>1 or )
        {
            p("ambiguous");
            int cntr = 0;
            int parent = 0;
            vi seq;
            rep(outer, h+1)
            {
                rep(i, a[outer])
                {
                    cout<<parent<<" ";
                    seq.pb(parent);
                    ++cntr;
                }
                parent = cntr;
            }
            p("");
            ll done = 1;
            rfor(i, 1, h+1)
            {
                if(a[i-1]>=2)
                {
                    --seq[done];
                    break;
                }
                done += a[i];
            }
            rep(i, seq.size())
                cout<<seq[i]<<" ";
            p("");
        }

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector <int>
#define vl vector <ll>
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define ff first
#define ss second
#define MOD 1000000007
ll power(ll a, ll b) { //a^b
	ll res = 1;
	a = a % MOD;
	while (b > 0) {
		if (b & 1) {res = (res * a) % MOD; b--;}
		a = (a * a) % MOD;
		b >>= 1;
	}
	return res;
}

ll gcd(ll a, ll b) {return (b == 0) ? a : gcd(b, a % b);}

int main() {
#ifndef ONLINE_JUDGE
	// for getting input from input.txt
	freopen("input.txt", "r", stdin);
	// for writing output to output.txt
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
	cin >> n;
	vl v(n + 1);
	pair<ll, ll> q;
	int f = 0;
	for (ll i = 0; i <= n; i++) {
		cin >> v[i];
		if (i) {
			if (v[i] > 1 && v[i - 1] > 1) {
				f = 1;
				q.ff = i - 1;
				q.ss = i;
			}
		}
	}
	if (!f)
		cout << "perfect";
	else {
		cout << "ambiguous\n";
		ll c = 0;
		for (ll i = 0; i <= n; i++) {
			ll rev = c;
			for (ll j = 1; j <= v[i]; j++) {
				c++;
				cout << rev << " ";
			}
		}
		cout << "\n";
		c = 0;
		for (ll i = 0; i <= n; i++) {
			ll rev = c;
			if (i == q.ss) {
				cout << rev - 1 << " ";
				c++;
				for (ll j = 1; j <= v[i] - 1; j++) {
					c++;
					cout << rev << " ";
				}
				continue;
			}
			for (ll j = 1; j <= v[i]; j++) {
				c++;
				cout << rev << " ";
			}
		}
	}

}


#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector <int>
#define vl vector <ll>
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define ff first
#define ss second
#define MOD 1000000007
ll power(ll a, ll b) { //a^b
	ll res = 1;
	a = a % MOD;
	while (b > 0) {
		if (b & 1) {res = (res * a) % MOD; b--;}
		a = (a * a) % MOD;
		b >>= 1;
	}
	return res;
}

ll gcd(ll a, ll b) {return (b == 0) ? a : gcd(b, a % b);}

int main() {
#ifndef ONLINE_JUDGE
	// for getting input from input.txt
	freopen("input.txt", "r", stdin);
	// for writing output to output.txt
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
	cin >> n;
	vl v(n + 1);
	pair<ll, ll> q;
	int f = 0;
	for (ll i = 0; i <= n; i++) {
		cin >> v[i];
		if (i) {
			if (v[i] > 1 && v[i - 1] > 1) {
				f = 1;
				q.ff = i - 1;
				q.ss = i;
			}
		}
	}
	if (!f)
		cout << "perfect";
	else {
		cout << "ambiguous\n";
		ll c = 0;
		for (ll i = 0; i <= n; i++) {
			ll rev = c;
			for (ll j = 1; j <= v[i]; j++) {
				c++;
				cout << rev << " ";
			}
		}
		cout << "\n";
		c = 0;
		for (ll i = 0; i <= n; i++) {
			ll rev = c;
			if (i == q.ss) {
				cout << c - 1 << " ";
				for (ll j = 1; j <= v[i] - 1; j++) {
					c++;
					cout << rev << " ";
				}
				continue;
			}
			for (ll j = 1; j <= v[i]; j++) {
				c++;
				cout << rev << " ";
			}
		}
	}

}


//#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

//defines
typedef long long ll;
typedef long double ld;
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define prev _prev
#define y0 _y0
#define kill _kill

//permanent constants
const ld pi = acos(-1.0);
const int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int digarr[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int dxo[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dyo[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
const int alf = 26;
const int dig = 10;
const int two = 2;
const int th = 3;
const ll prost = 239;
const ll btc = 30;
const ld eps = 1e-8;
const ll INF = (ll)(1e18 + 239);
const int BIG = (int)(1e9 + 239);
const int MOD = 998244353; //1e9 + 7;

//random
mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

//constants
const int M = (int)(2e5 + 239);
const int N = (int)(2e3 + 239);
const int L = 20;
const int T = (1 << 18) + 239;
const int B = 500;
const int X = 1e4 + 239;

int h, a[M];

int32_t main()
{
#ifdef ONPC
    freopen("input", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> h;
    for (int i = 0; i <= h; i++)
        cin >> a[i];
    bool ch = true;
    for (int i = 0; i <= h - 1; i++)
        ch &= (a[i] == 1 || a[i + 1] == 1);
    if (ch)
    {
        cout << "perfect\n";
        return 0;
    }
    cout << "ambiguous\n";
    vector<int> p;
    p.push_back(0);
    for (int i = 1; i <= h; i++)
    {
        int to = (int)p.size();
        for (int x = 0; x < a[i]; x++)
            p.push_back(to);
    }
    for (int i : p)
        cout << i << " ";
    cout << "\n";
    p.clear();
    int ls = 1;
    p.push_back(0);
    for (int i = 1; i <= h; i++)
    {
        int to = (int)p.size();
        p.push_back(ls);
        for (int x = 1; x < a[i]; x++)
            p.push_back(to);
        ls = to + 1;
    }
    for (int i : p)
        cout << i << " ";
    cout << "\n";
    return 0;
}


//#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

//defines
typedef long long ll;
typedef long double ld;
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define prev _prev
#define y0 _y0
#define kill _kill

//permanent constants
const ld pi = acos(-1.0);
const int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int digarr[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int dxo[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dyo[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
const int alf = 26;
const int dig = 10;
const int two = 2;
const int th = 3;
const ll prost = 239;
const ll btc = 30;
const ld eps = 1e-8;
const ll INF = (ll)(1e18 + 239);
const int BIG = (int)(1e9 + 239);
const int MOD = 998244353; //1e9 + 7;

//random
mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

//constants
const int M = (int)(2e5 + 239);
const int N = (int)(2e3 + 239);
const int L = 20;
const int T = (1 << 18) + 239;
const int B = 500;
const int X = 1e4 + 239;

int h, a[M];

int32_t main()
{
#ifdef ONPC
    freopen("input", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> h;
    for (int i = 0; i <= h; i++)
        cin >> a[i];
    bool ch = true;
    for (int i = 0; i <= h - 1; i++)
        ch &= (a[i] == 1);
    if (ch)
    {
        cout << "perfect\n";
        return 0;
    }
    cout << "ambiguous\n";
    vector<int> p;
    p.push_back(0);
    for (int i = 1; i <= h; i++)
    {
        int to = (int)p.size();
        for (int x = 0; x < a[i]; x++)
            p.push_back(to);
    }
    for (int i : p)
        cout << i << " ";
    cout << "\n";
    p.clear();
    int ls = 1;
    p.push_back(0);
    for (int i = 1; i <= h; i++)
    {
        int to = (int)p.size();
        p.push_back(ls);
        for (int x = 1; x < a[i]; x++)
            p.push_back(to);
        ls = to + 1;
    }
    for (int i : p)
        cout << i << " ";
    cout << "\n";
    return 0;
}


//#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

//defines
typedef long long ll;
typedef long double ld;
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define prev _prev
#define y0 _y0
#define kill _kill

//permanent constants
const ld pi = acos(-1.0);
const int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int digarr[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int dxo[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dyo[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
const int alf = 26;
const int dig = 10;
const int two = 2;
const int th = 3;
const ll prost = 239;
const ll btc = 30;
const ld eps = 1e-8;
const ll INF = (ll)(1e18 + 239);
const int BIG = (int)(1e9 + 239);
const int MOD = 998244353; //1e9 + 7;

//random
mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

//constants
const int M = (int)(2e5 + 239);
const int N = (int)(2e3 + 239);
const int L = 20;
const int T = (1 << 18) + 239;
const int B = 500;
const int X = 1e4 + 239;

int h, a[M];

int32_t main()
{
#ifdef ONPC
    freopen("input", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> h;
    for (int i = 0; i <= h; i++)
        cin >> a[i];
    bool ch = true;
    for (int i = 0; i <= h - 1; i++)
        ch &= (a[i] == 1);
    if (ch)
    {
        cout << "perfect\n";
        return 0;
    }
    cout << "ambiguous\n";
    vector<int> p;
    p.push_back(0);
    for (int i = 1; i <= h; i++)
    {
        int to = (int)p.size();
        for (int x = 0; x < a[i]; x++)
            p.push_back(to);
    }
    for (int i : p)
        cout << i << " ";
    cout << "\n";
    p.clear();
    int ls = 1;
    p.push_back(0);
    for (int i = 1; i <= h; i++)
    {
        int to = (int)p.size();
        for (int x = 0; x < a[i]; x++)
            p.push_back(ls);
        ls = to + 1;
    }
    for (int i : p)
        cout << i << " ";
    cout << "\n";
    return 0;
}


/*
                        ********************
			**  MANAN DIYORA  **
			**    DA-IICT     **
			**   diyora13     **
			********************
*/
#include <bits/stdc++.h>
using namespace std;

//#include <time.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define setpre(x,d) fixed<<setprecision(d)<<x
#define inarr(a,n) for(int i=0;i<n;i++) cin>>a[i]
#define outarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl
#define maxa(a,n) *max_element(a,a+n)
#define mina(a,n) *min_element(a,a+n)
#define fr(i,s,e) for(ll i=s;i<e;i++)
#define rf(i,s,e) for(ll i=s-1;i>=e;i--)
#define gcd(a,b) __gcd(a,b)
#define deb1(x) cout<<x<<endl;
#define deb2(x,y) cout<<x<<" : "<<y<<endl;
#define deb3(x,y,z) cout<<x<<" : "<<y<<" : "<<z<<endl;
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define sz size()
#define len length()
#define all(v) v.begin(),v.end()
#define PI 3.141592653589793
#define MOD 1000000007
#define NN (const int)5e5+10

typedef long long  ll;
typedef long double lld;
typedef pair<long long,long long> pll;
typedef vector<long double> vll;
typedef vector<pair<long long,long long>> vpll;

// bool sortbysec(const pair<int,int> &a,const pair<int,int> &b) { return (a.second < b.second); }
// void fileio()	{	freopen("input.txt","r",stdin);	freopen("output.txt","w",stdout);	}
//	vll fact(NN),invfact(NN);
//	void EE(ll a,ll b,ll &x,ll &y) {	if(b==0) {	x=1,y=0; return ;	}		EE(b,a%b,x,y);	ll temp=x; x=y; y=temp-(a/b)*y;	}
//	ll inverse(ll a,ll mod) 	{	ll x,y;	EE(a,mod,x,y);	return (x+mod)%mod;	}
//	void build() { fact[0]=1; fr(i,1,NN) fact[i]=(fact[i-1]*i)%MOD;	invfact[NN-1]=inverse(fact[NN-1],MOD); rf(i,NN-1,0) invfact[i]=(invfact[i+1]*(i+1))%MOD; }
//	ll ncr(ll n,ll r)       	{	if(n<0 || r<0 || r>n) return 0;	return (fact[n]*((invfact[n-r]*invfact[r])%MOD))%MOD;	}
//	ll choose(ll n,ll k) 	  	{	if(k==0)  return 1; return (n* choose(n-1,k-1))/k;	}
ll logy(ll x,ll y)	{	return ceil((lld)(log(x)/log(y)));	}
ll powe(ll a,ll b)	{	ll re=1;	while(b){	if(b&1) re=(re*a);	a=(a*a);	b=b>>1;}	return re;	}

bool flag=0,flag1=0,flag2=0;

int main()
{
	IOS
	ll tt=1;
//	   cin>>tt;

	while(tt--)
	{
		ll n; cin>>n;
		ll a[n+1],h=1; inarr(a,n+1);
		vll v1,v2; v1.pb(0);
		fr(i,1,n+1)
		{
			fr(j,0,a[i]) v1.pb(h);
			h+=a[i-1];
		}
		h=1;
//		fr(i,0,v1.sz) cout<<v1[i]<<" ";
		fr(i,0,v1.sz) v2.pb(v1[i]);
		fr(i,1,n+1)
		{
			if(a[i]!=1 && a[i-1]!=1)
			{
				flag=1;
				fr(j,h,v2.sz) v2[j]++;
				break;
			}
			h+=a[i];
		}
		if(flag==0) cout<<"perfect";
		else
		{
			cout<<"ambiguous"<<endl;
			fr(i,0,v1.sz) cout<<v1[i]<<" "; cout<<endl;
			fr(i,0,v2.sz) cout<<v2[i]<<" "; cout<<endl;
		}
	}
	return 0;
}


/*
                        ********************
			**  MANAN DIYORA  **
			**    DA-IICT     **
			**   diyora13     **
			********************
*/
#include <bits/stdc++.h>
using namespace std;

//#include <time.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define setpre(x,d) fixed<<setprecision(d)<<x
#define inarr(a,n) for(int i=0;i<n;i++) cin>>a[i]
#define outarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl
#define maxa(a,n) *max_element(a,a+n)
#define mina(a,n) *min_element(a,a+n)
#define fr(i,s,e) for(ll i=s;i<e;i++)
#define rf(i,s,e) for(ll i=s-1;i>=e;i--)
#define gcd(a,b) __gcd(a,b)
#define deb1(x) cout<<x<<endl;
#define deb2(x,y) cout<<x<<" : "<<y<<endl;
#define deb3(x,y,z) cout<<x<<" : "<<y<<" : "<<z<<endl;
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define sz size()
#define len length()
#define all(v) v.begin(),v.end()
#define PI 3.141592653589793
#define MOD 1000000007
#define NN (const int)5e5+10

typedef long long  ll;
typedef long double lld;
typedef pair<long long,long long> pll;
typedef vector<long double> vll;
typedef vector<pair<long long,long long>> vpll;

// bool sortbysec(const pair<int,int> &a,const pair<int,int> &b) { return (a.second < b.second); }
// void fileio()	{	freopen("input.txt","r",stdin);	freopen("output.txt","w",stdout);	}
//	vll fact(NN),invfact(NN);
//	void EE(ll a,ll b,ll &x,ll &y) {	if(b==0) {	x=1,y=0; return ;	}		EE(b,a%b,x,y);	ll temp=x; x=y; y=temp-(a/b)*y;	}
//	ll inverse(ll a,ll mod) 	{	ll x,y;	EE(a,mod,x,y);	return (x+mod)%mod;	}
//	void build() { fact[0]=1; fr(i,1,NN) fact[i]=(fact[i-1]*i)%MOD;	invfact[NN-1]=inverse(fact[NN-1],MOD); rf(i,NN-1,0) invfact[i]=(invfact[i+1]*(i+1))%MOD; }
//	ll ncr(ll n,ll r)       	{	if(n<0 || r<0 || r>n) return 0;	return (fact[n]*((invfact[n-r]*invfact[r])%MOD))%MOD;	}
//	ll choose(ll n,ll k) 	  	{	if(k==0)  return 1; return (n* choose(n-1,k-1))/k;	}
ll logy(ll x,ll y)	{	return ceil((lld)(log(x)/log(y)));	}
ll powe(ll a,ll b)	{	ll re=1;	while(b){	if(b&1) re=(re*a);	a=(a*a);	b=b>>1;}	return re;	}

bool flag=0,flag1=0,flag2=0;

int main()
{
	IOS
	ll tt=1;
//	   cin>>tt;

	while(tt--)
	{
		ll n; cin>>n;
		ll a[n+1],h=1; inarr(a,n+1);
		vll v1,v2; v1.pb(0);
		fr(i,1,n+1)
		{
			fr(j,0,a[i]) v1.pb(h);
			h+=a[i-1];
		}
		h=1;
//		fr(i,0,v1.sz) cout<<v1[i]<<" ";
		fr(i,0,v1.sz) v2.pb(v1[i]);
		fr(i,1,n+1)
		{
			if(a[i]!=1 && a[i-1]!=1)
			{
				flag=1;
				v2[h+a[i]-1]++;
				break;
			}
			h+=a[i];
		}
		if(flag==0) cout<<"perfect";
		else
		{
			cout<<"ambiguous"<<endl;
			fr(i,0,v1.sz) cout<<v1[i]<<" "; cout<<endl;
			fr(i,0,v2.sz) cout<<v2[i]<<" "; cout<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){
	ll n,*a,j,x=2,k=0;
	cin>>n;
	a=new ll [n+1];
	bool f=1;
	for(int i=0; i<=n; i++){
		cin>>a[i];
		if(a[i]>1 && a[i-1]>1) f=0,j=i;
	}
	if(f) {
		cout<<"perfect";
		return 0;
	}
	cout<<"ambiguous\n";
	while(x--){
		for(int i=0; i<n+1; i++){
			if(i==j){
				cout<<(f ? k-1 : k)<<" ";
				if(!f) f=1,a[i]--;
			}
			for(int ii=0; ii<a[i]; ii++)
				cout<<k<<" ";
			k+=a[i];
			if(i==j) k++;
		}
		cout<<endl;
		k=0;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){
	ll n,*a,j,x=2,k=0;
	cin>>n;
	a=new ll [n+1];
	bool f=1;
	for(int i=0; i<=n; i++){
		cin>>a[i];
		if(a[i]>1) f=0,j=i;
	}
	if(f) {
		cout<<"perfect";
		return 0;
	}
	cout<<"ambiguous\n";
	while(x--){
		for(int i=0; i<n+1; i++){
			if(i==j){
				cout<<(f ? k-1 : k)<<" ";
				if(!f) f=1,a[i]--;
			}
			for(int ii=0; ii<a[i]; ii++)
				cout<<k<<" ";
			k+=a[i];
		}
		cout<<endl;
		k=0;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){
	ll n,*a,j,x=2,k=0;
	cin>>n;
	a=new ll [n+1];
	bool f=1;
	for(int i=0; i<=n; i++){
		cin>>a[i];
		if(a[i]>1 && a[i-1]>1) f=0,j=i;
	}
	if(f) {
		cout<<"perfect";
		return 0;
	}
	cout<<"ambiguous\n";
	while(x--){
		for(int i=0; i<n+1; i++){
			if(i==j){
				cout<<(f ? k-1 : k)<<" ";
				if(!f) f=1,a[i]--;
			}
			for(int ii=0; ii<a[i]; ii++)
				cout<<k<<" ";
			k+=a[i];
		}
		cout<<endl;
		k=0;
	}
}


/*
ID: essi
LANG: C++
TASK: .cpp
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld double long
#define all(n) n.begin(), n.end()
#define rall(s) (s).rbegin(),(s).rend()
#define bs(x) (bitset<60>(x)).to_string()
#define sep ios::sync_with_stdio(0),cin.tie(0)
const ll MAX = 2e5 + 14;

int main() {
    //ifstream fin ("word.in");
    //ofstream fout ("word.out");
    sep;
    ll n; cin >> n;
    vector<ll> v(n + 1);
    bool ff = true;
    for (ll i = 0; i <=n; ++i) cin >> v[i];
    for (ll i = 0; i < n; ++i)
    	if (v[i] >= 2 && v[i + 1] >= 2) {ff = false;break;}
    if (ff) {cout << "perfect\n"; return 0;}

    cout << "ambiguous\n";
    ll root1 = 1, root2 = 1, ver = 2;
    ff = false;
    unordered_map<ll, ll> p1;
    unordered_map<ll, ll> p2;
    for (ll i = 1; i <= n; ++i) {
    	if (v[i] >= 2 && v[i - 1] >=2 && ff == false) {
    		ff = true;
    		ll VER = ver;
    		for (ll j = 0; j < v[i]; ++j) {
    			p2[VER] = root2;
    			++VER;
    		}
    		root2 = VER - 1;

    		p1[ver] = root1 - 1;
    		++ver;
    		for (ll j = 1; j < v[i]; ++j) {
    			p1[ver] = root1;
    			++ver;
    		}
    		root1 = ver - 1;

    	}
    	else {
    		for (ll j = 0; j < v[i]; ++j) {
    			p1[ver] = root1;
    			p2[ver] = root2;
    			++ver;
    		}
    		root1 = ver - 1;
    		root2 = root1;

    	}
    }
    for (ll i = 1; i < ver; ++i)
    	cout << p1[i] << " ";
    cout << "\n";
    for (ll i = 1; i < ver; ++i)
    	cout << p2[i] << " ";
    cout << "\n";
    return 0;
}


/*
ID: essi
LANG: C++
TASK: .cpp
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld double long
#define all(n) n.begin(), n.end()
#define rall(s) (s).rbegin(),(s).rend()
#define bs(x) (bitset<60>(x)).to_string()
#define sep ios::sync_with_stdio(0),cin.tie(0)
const ll MAX = 2e5 + 14;
vector<ll> Tree1[MAX];
vector<ll> Tree2[MAX];

int main() {
    //ifstream fin ("word.in");
    //ofstream fout ("word.out");
    sep;
    ll n; cin >> n;
    vector<ll> v(n + 1);
    bool ff = true;
    for (ll i = 0; i <=n; ++i) cin >> v[i];
    for (ll i = 0; i < n; ++i)
    	if (v[i] >= 2 && v[i + 1] >= 2) {ff = false;break;}
    if (ff) {cout << "perfect\n"; return 0;}

    cout << "ambiguous\n";
    ll root1 = 1, root2 = 1, ver = 2;
    ff = false;
    unordered_map<ll, ll> p1;
    unordered_map<ll, ll> p2;
    for (ll i = 1; i <= n; ++i) {
    	if (v[i] >= 2 && v[i - 1] >=2 && ff == false) {
    		ff = true;
    		ll VER = ver;
    		for (ll j = 0; j < v[i]; ++j) {
    			Tree2[root2].emplace_back(VER);
    			p2[VER] = root2;
    			++VER;
    		}
    		root2 = VER - 1;

    		Tree1[root1 - 1].emplace_back(ver);
    		p1[ver] = root1 - 1;
    		++ver;
    		for (ll j = 1; j < v[i]; ++j) {
    			Tree1[root1].emplace_back(ver);
    			p1[ver] = root1;
    			++ver;
    		}
    		root1 = ver - 1;

    	}
    	else {
    		for (ll j = 0; j < v[i]; ++j) {
    			Tree1[root1].emplace_back(ver);
    			p1[ver] = root1;
    			Tree2[root2].emplace_back(ver);
    			p2[ver] = root2;
    			++ver;
    		}
    		root1 = ver - 1;
    		root2 = root1;

    	}
    }
    for (ll i = 1; i < ver; ++i)
    	cout << p1[i] << " ";
    cout << "\n";
    for (ll i = 1; i < ver; ++i)
    	cout << p2[i] << " ";
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(i,A,B) for(int i=A;i<=B;i++)
#define mod 998244353
#define v1d vector<int>
#define v1c vector<char>
#define v2d vector<vector<int>>
#define v1s vector<string>
#define vip vector<pair<int,int> >
#define v2s vector<vector<string> >
#define vsp vector<pair<string,string> >

bool isperfect(v1d &v){
    for(int i=1;i<=v.size();i++){
        if(v[i]!=1&&v[i-1]!=1)return false;
    }
    return true;
}

int32_t main(){
    IOS;
    int n;cin>>n;
    n++;
    v1d v(n+1);
    int s=0;
    for(int i=1;i<=n;i++){cin>>v[i];s+=v[i];}
    if(isperfect(v)){
        cout<<"perfect";return 0;
    }
    cout<<"ambiguous\n";
    v1d a(s+1,0),b(s+1,0);
    int x=0;
    bool f=false;
    for(int i=1;i<=n;i++){
        if(v[i]!=1&&v[i-1]!=1){
            x=i;break;
        }
    }
    s=1;
    int ind=2;
    int y;
    for(int i=2;i<=n;i++){
        int r= s-v[i-1];
        r++;
        for(int j=0;j<v[i];j++){
            a[ind++]=r;
        }
        if(i==x)y=r+1;
        s+= v[i];
    }
    for(int i=1;i<a.size();i++)cout<<a[i]<<" ";
    s=1;
    ind=2;
    for(int i=2;i<=n;i++){
        int r= s-v[i-1];
        r++;
        for(int j=0;j<v[i];j++){
            a[ind++]=r;
        }
        if(i==x){
            a[ind-1]=y;
        }
        s+= v[i];
    }
    cout<<"\n";
    for(int i=1;i<a.size();i++)cout<<a[i]<<" ";

    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(i,A,B) for(int i=A;i<=B;i++)
#define mod 998244353
#define v1d vector<int>
#define v1c vector<char>
#define v2d vector<vector<int>>
#define v1s vector<string>
#define vip vector<pair<int,int> >
#define v2s vector<vector<string> >
#define vsp vector<pair<string,string> >

bool isperfect(v1d &v){
    for(int i=1;i<v.size();i++){
        if(v[i]!=1&&v[i-1]!=1)return false;
    }
    return true;
}

int32_t main(){
    IOS;
    int n;cin>>n;
    n++;
    v1d v(n+1);
    int s=0;
    for(int i=1;i<=n;i++){cin>>v[i];s+=v[i];}
    if(isperfect(v)){
        cout<<"perfect";return 0;
    }
    cout<<"ambiguous\n";
    v1d a(s+1,0),b(s+1,0);
    int x=0;
    bool f=false;
    for(int i=1;i<=n;i++){
        if(v[i]!=1&&v[i-1]!=1){
            x=i;break;
        }
    }
    s=1;
    int ind=2;
    int y;
    for(int i=2;i<=n;i++){
        int r= s-v[i-1];
        r++;
        for(int j=0;j<v[i];j++){
            a[ind++]=r;
        }
        if(i==x)y=r+1;
        s+= v[i];
    }
    for(int i=1;i<a.size();i++)cout<<a[i]<<" ";
    s=1;
    ind=2;
    for(int i=2;i<=n;i++){
        int r= s-v[i-1];
        r++;
        for(int j=0;j<v[i];j++){
            a[ind++]=r;
        }
        if(i==x){
            a[ind-1]=y;
        }
        s+= v[i];
    }
    cout<<"\n";
    for(int i=1;i<a.size();i++)cout<<a[i]<<" ";

    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rngll(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main(){
	cin.tie(0)->sync_with_stdio(0);
	auto kill = [](){
	    cout << "perfect";
	    exit(0);
	};
	int n;
	cin >> n, ++ n;
	vector<int> a(n);
	int cnt = 0;
	for(auto &x: a){
		cin >> x;
		cnt += (x > 1);
	}
	if(cnt <= 1){
		kill();
	}
	cout << "ambiguous\n";
	cnt = 2;
	vector<int> b{0}, c{0};
	int l = 0, r = 1;
	for(int i = 1; i < n; ++ i){
		if(r - l == 1){
			for(int j = 0; j < a[i]; ++ j){
				b.push_back(l + 1);
				c.push_back(l + 1);
			}
		}
		else{
			b.push_back(l + 1);
			c.push_back(l + 1);
			for(int j = 1; j < a[i]; ++ j){
				b.push_back(l + 1);
				c.push_back(l + 2);
			}
		}
		l = r, r += a[i];
	}
	for(auto x: b){
		cout << x << " ";
	}
	cout << "\n";
	for(auto x: c){
		cout << x << " ";
	}
	return 0;
}

/*

*/

////////////////////////////////////////////////////////////////////////////////////////
//                                                                                //
//                                   Coded by Aeren                                   //
//                                                                                //
////////////////////////////////////////////////////////////////////////////////////////


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rngll(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main(){
	cin.tie(0)->sync_with_stdio(0);
	int n;
	cin >> n, ++ n;
	vector<int> a(n);
	for(auto &x: a){
		cin >> x;
	}
	bool ok = true;
	for(int i = 1; i < n; ++ i){
		if(a[i] > 1 && a[i - 1] > 1){
			ok = false;
			break;
		}
	}
	if(ok){
		cout << "perfect";
		return 0;
	}
	cout << "ambiguous\n";
	vector<int> b{0}, c{0};
	int l = 0, r = 1;
	for(int i = 1; i < n; ++ i){
		if(r - l == 1){
			for(int j = 0; j < a[i]; ++ j){
				b.push_back(l + 1);
				c.push_back(l + 1);
			}
		}
		else{
			b.push_back(l + 1);
			c.push_back(l + 1);
			for(int j = 1; j < a[i]; ++ j){
				b.push_back(l + 1);
				c.push_back(l + 2);
			}
		}
		l = r, r += a[i];
	}
	for(auto x: b){
		cout << x << " ";
	}
	cout << "\n";
	for(auto x: c){
		cout << x << " ";
	}
	return 0;
}

/*

*/

////////////////////////////////////////////////////////////////////////////////////////
//                                                                                //
//                                   Coded by Aeren                                   //
//                                                                                //
////////////////////////////////////////////////////////////////////////////////////////


#pragma GCC optimize("Ofast,fast-math,unroll-loops")

#include <bits/stdc++.h>

//#define int ll
//#define double long double
#define endl '\n'
#define all(C) (C).begin(), (C).end()
#define rall(C) (C).rbegin(), (C).rend()
#define mp make_pair
#define pb emplace_back
#define dbg(x) cerr << #x << " : " << x << endl
#define PI 3.14159265358979

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair <int, int>;

/*
const ll MAX_MEM = 7e8;
char MEM[MAX_MEM];
ll MEM_POS = 0;
void* operator new(size_t x)
{
    auto ret = MEM + MEM_POS;
    MEM_POS += x;
    assert(MEM_POS < MAX_MEM);
    return ret;
}
void operator delete(void*)
{}
*/

template <class T>
istream& operator>> (istream &in, vector <T> &a)
{
    for (auto &i : a)
        in >> i;
    return in;
}

template <class T>
ostream& operator<< (ostream &out, vector <T> a)
{
    for (auto &i : a)
        out << i << ' ';
    return out;
}

template <class T, class U>
istream& operator>> (istream &in, pair <T, U> &p)
{
    in >> p.first >> p.second;
    return in;
}

template <class T, class U>
ostream& operator<< (ostream &out, pair <T, U> p)
{
    out << p.first << " " << p.second << " ";
    return out;
}

inline void Start()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
}

signed main()
{
    Start();
    int h;
    cin >> h;
    vector <int> a(h + 1);
    cin >> a;
    int n = 0;
    for (auto i : a)
        n += i;
    if (a == vector <int> (h + 1, 1))
        return cout << "perfect", 0;
    int flag = 1;
    for (int i = 0; i < h; ++i)
    {
        if (a[i] != 1)
            flag = 0;
    }
    if (flag)
        return cout << "perfect", 0;
    vector <int> p1(n), p2(n);
    vector <vector <int>> b(h + 1);
    int cur = 0;
    for (int i = 0; i < h + 1; ++i)
    {
        for (int j = 0; j < a[i]; ++j)
        {
            if (cur != 0)
                p1[cur] = b[i - 1][0] + 1;
            b[i].pb(cur++);
        }
    }
    p2 = p1;
    for (int i = 1; i < h; ++i)
    {
        if (a[i] != 1 && a[i + 1] != 1)
        {
            p2[b[i + 1][1]] = b[i][1] + 1;
            break;
        }
    }
    if (p1 == p2)
        return cout << "perfect", 0;
    cout << "ambiguous\n";

    cout << p1 << endl << p2;
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define lb long double
#define mp make_pair
#define pb push_back
#define loop(i,start,n) for(ll i=start;i<n;i++)
#define rloop(i,n,start)for(ll i=n;i>=start;i--)
#define mod (ll)1e18
#define endl "\n"
#define INF     1e15
#define NEG_INF -1e15
#define pll pair<ll,ll>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
ll power(ll x, ll y)
{
    ll temp,f;
    if( y == 0)
        return 1;
    temp = power(x, y/2) ;
    if (y%2 == 0)
        return (temp*temp) ;
    else
    {
        f=(x*temp) ;
        f=(f*temp) ;
        return f;
    }
}
void solve()
{
	IOS;
	ll n,m;
	cin>>n>>m;
	ll a[m],sum=0;
	multiset<pll> s;
	loop(i,0,m)
	{
		cin>>a[i];
		sum+=a[i];
		s.insert({a[i],0});
	}
	if(sum<n)
	{
		cout<<"-1"<<endl;
		return ;
	}
	ll poweri;
	trace1(power(2,51));
	loop(i,0,51)
	{
		poweri=power(2,i);
		ll k=n&poweri;
		if(k)
		{
			auto t1=(s.lower_bound({poweri,NEG_INF}));
			if(t1!=s.end()&&t1->x==poweri)
			{
				s.erase(t1);
				n-=poweri;
				//trace1(poweri);
			}
		}
		while(1)
		{

			auto t1=s.lower_bound({poweri,NEG_INF});
			ll cost=0;
			if(t1!=s.end()&&t1->x==poweri)
			{
				cost=min(cost,t1->y);
				s.erase(t1);
			}
			else
				break;
			auto t2=s.lower_bound({poweri,NEG_INF});
			if(t2!=s.end()&&t2->x==poweri)
			{
				cost=min(cost,t2->y);
				s.erase(t2);
			}
			else
			{
				s.insert({poweri,cost});
				break;
			}
			s.insert({2*poweri,cost-1});
		}

	}

	ll cost=0;
	//for(auto &node :s)
	//	trace2(node.x,node.y);
	trace1(n);
	ll cal=0;
	rloop(i,51,0)
	{
		poweri=power(2,i);
		ll k=n&poweri;
		if(k)
		{

			auto t1=s.lower_bound({poweri,NEG_INF});
			n-=poweri;
			cost+=max((ll)t1->y,(ll)(0));
			s.erase(t1);

		}

		while(1)
		{
			auto temp=s.lower_bound({poweri,NEG_INF});
			if(temp==s.end()||temp->x!=poweri)
				break;
			cal++;
			assert(cal<1000000);
			ll cost=temp->y+1;
			s.erase(temp);
			s.insert({poweri/2,cost});
		}

	}
	cout<<cost<<endl;
}
int main()
{

	ll t;
	cin>>t;
	while(t--)
		solve();
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define lb long double
#define mp make_pair
#define pb push_back
#define loop(i,start,n) for(ll i=start;i<n;i++)
#define rloop(i,n,start)for(ll i=n;i>=start;i--)
#define mod 1000000007
#define endl "\n"
#define INF     1e15
#define NEG_INF -1e15
#define pll pair<ll,ll>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
ll power(ll x, ll y)
{
    ll temp,f;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return (temp*temp);
    else
    {
        f=(x*temp);
        f=(f*temp);
        return f;
    }
}
void solve()
{
	ll n,m,sum=0;
	cin>>n>>m;
	ll a[m];
	vector<multiset<ll> > vect(60);
	loop(i,0,m)
	{
		cin>>a[i];
		sum+=a[i];
		ll temp=log2(a[i]);
		//trace1(temp);
		vect[temp].insert(0);
	}

	if(n>sum)
	{
		cout<<"-1"<<endl;
		return ;
	}
	ll cost=0,poweri;
	//return ;
	loop(i,0,54)
	{
		poweri=power(2,i);
		if(n&poweri)
		{
			//trace1(poweri);
			if(vect[i].size()!=0)
			{
				cost+=*(vect[i].begin());
				vect[i].erase(vect[i].begin());
			}
			else
			{
				ll temp;
				loop(j,i+1,54)
				{
					if(vect[j].size()!=0)
					{
						temp=j;
						//trace1(temp);
						vect[j].erase(vect[j].begin());
						break;
					}

				}
				ll cost=1;
				rloop(j,temp-1,i+1)
				{
					vect[j].insert(0);
					vect[j].insert(0);
					cost++;
				}
				vect[i].insert(cost);
				vect[i].insert(cost);
				//trace3(i,temp,cost);
				i--;continue;
			}
		}

		t=vect[i].size()/2;
		loop(j,0,t)
			vect[i+1].insert(0);
	}
	cout<<cost<<endl;
}
int main()
{
	IOS;
	ll t;
	cin>>t;
	while(t--)
		solve();
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define lb long double
#define mp make_pair
#define pb push_back
#define loop(i,start,n) for(ll i=start;i<n;i++)
#define rloop(i,n,start)for(ll i=n;i>=start;i--)
#define mod (ll)1e18
#define endl "\n"
#define INF     1e15
#define NEG_INF -1e15
#define pll pair<ll,ll>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
ll power(ll x, ll y)
{
    ll temp,f;
    if( y == 0)
        return 1;
    temp = power(x, y/2) ;
    if (y%2 == 0)
        return (temp*temp) ;
    else
    {
        f=(x*temp) ;
        f=(f*temp) ;
        return f;
    }
}
void solve()
{
	//IOS;
	ll n,m;
	cin>>n>>m;
	ll a[m]={0},sum=0;
	multiset<pll> s;
	loop(i,0,m)
	{
		cin>>a[i];
		sum+=a[i];
		s.insert({a[i],0});
	}
	if(sum<n)
	{
		cout<<"-1"<<endl;
		return ;
	}
	ll poweri;
	trace1(power(2,51));
	loop(i,0,51)
	{
		poweri=power(2,i);
		ll k=n&poweri;
		if(k)
		{
			auto t1=(s.lower_bound({poweri,NEG_INF}));
			if(t1!=s.end()&&t1->x==poweri)
			{
				s.erase(t1);
				n-=poweri;
				//trace1(poweri);
			}
		}
		while(1)
		{

			auto t1=s.lower_bound({poweri,NEG_INF});
			ll cost=0;
			if(t1!=s.end()&&t1->x==poweri)
			{
				cost=min(cost,t1->y);
				s.erase(t1);
			}
			else
				break;
			auto t2=s.lower_bound({poweri,NEG_INF});
			if(t2!=s.end()&&t2->x==poweri)
			{
				cost=min(cost,t2->y);
				s.erase(t2);
			}
			else
			{
				s.insert({poweri,cost});
				break;
			}
			s.insert({2*poweri,cost-1});
		}

	}

	ll cost=0;
	//for(auto &node :s)
	//	trace2(node.x,node.y);
	trace1(n);
	ll cal=0;
	rloop(i,51,0)
	{
		poweri=power(2,i);
		ll k=n&poweri;
		if(k)
		{

			auto t1=s.lower_bound({poweri,NEG_INF});
			n-=poweri;
			cost+=max((ll)t1->y,(ll)(0));
			s.erase(t1);

		}

		while(1)
		{
			auto temp=s.lower_bound({poweri,NEG_INF});
			if(temp==s.end()||temp->x!=poweri)
				break;
			cal++;
			assert(cal<1000000);
			ll cost=temp->y+1;
			s.erase(temp);
			s.insert({poweri/2,cost});
		}

	}
	cout<<cost<<endl;
}
int main()
{

	ll t;
	cin>>t;
	while(t--)
		solve();
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define lb long double
#define mp make_pair
#define pb push_back
#define loop(i,start,n) for(ll i=start;i<n;i++)
#define rloop(i,n,start)for(ll i=n;i>=start;i--)
#define mod (ll)1e18
#define endl "\n"
#define INF     1e15
#define NEG_INF -1e15
#define pll pair<ll,ll>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
ll power(ll x, ll y)
{
    ll temp,f;
    if( y == 0)
        return 1;
    temp = power(x, y/2) ;
    if (y%2 == 0)
        return (temp*temp) ;
    else
    {
        f=(x*temp) ;
        f=(f*temp) ;
        return f;
    }
}
void solve()
{
	//IOS;
	ll n,m;
	cin>>n>>m;
	ll a[m]={0},sum=0;
	multiset<pll> s;
	loop(i,0,m)
	{
		cin>>a[i];
		sum+=a[i];
		s.insert({a[i],0});
	}
	if(sum<n)
	{
		cout<<"-1"<<endl;
		return ;
	}
	ll poweri;
	//trace1(power(2,51));
	loop(i,0,51)
	{
		poweri=power(2,i);
		ll k=n&poweri;
		if(k)
		{
			auto t1=(s.lower_bound({poweri,NEG_INF}));
			if(t1!=s.end()&&t1->x==poweri)
			{
				s.erase(t1);
				n-=poweri;
				//trace1(poweri);
			}
		}
		while(1)
		{

			auto t1=s.lower_bound({poweri,NEG_INF});
			ll cost=0;
			if(t1!=s.end()&&t1->x==poweri)
			{
				cost=min(cost,t1->y);
				s.erase(t1);
			}
			else
				break;
			auto t2=s.lower_bound({poweri,NEG_INF});
			if(t2!=s.end()&&t2->x==poweri)
			{
				cost=min(cost,t2->y);
				s.erase(t2);
			}
			else
			{
				s.insert({poweri,cost});
				break;
			}
			s.insert({2*poweri,cost-1});
		}

	}

	ll cost=0;
	//for(auto &node :s)
	//	trace2(node.x,node.y);
	//trace1(n);
 	//return ;
	rloop(i,51,0)
	{
		poweri=power(2,i);
		ll k=n&poweri;
		if(k)
		{

			auto t1=s.lower_bound({poweri,NEG_INF});
			n-=poweri;
			cost+=max((ll)t1->y,(ll)(0));
			s.erase(t1);

		}
		ll count=1;
		while(count--)
		{
			auto temp=s.lower_bound({poweri,NEG_INF});
			if(temp==s.end()||temp->x!=poweri)
				break;
			//trace1(poweri);
			ll cost=temp->y+1;
			s.erase(temp);
			s.insert({poweri/2,cost});
			s.insert({poweri/2,cost});
		}

	}
	//trace1(cost);
	cout<<cost<<endl;
}
int main()
{

	ll t;
	cin>>t;
	while(t--)
		solve();
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define lb long double
#define mp make_pair
#define pb push_back
#define loop(i,start,n) for(ll i=start;i<n;i++)
#define rloop(i,n,start)for(ll i=n;i>=start;i--)
#define mod (ll)1e18
#define endl "\n"
#define INF     1e15
#define NEG_INF -1e15
#define pll pair<ll,ll>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
ll power(ll x, ll y)
{
    ll temp,f;
    if( y == 0)
        return 1;
    temp = power(x, y/2) ;
    if (y%2 == 0)
        return (temp*temp) ;
    else
    {
        f=(x*temp) ;
        f=(f*temp) ;
        return f;
    }
}
void solve()
{
	ll n,m;
	cin>>n>>m;
	ll a[m],sum=0;
	multiset<pll> s;
	loop(i,0,m)
	{
		cin>>a[i];
		sum+=a[i];
		s.insert({a[i],0});
	}
	if(sum<n)
	{
		cout<<"-1"<<endl;
		return ;
	}
	ll poweri;
	loop(i,0,51)
	{
		poweri=power(2,i);
		ll k=n&poweri;
		if(k)
		{
			auto t1=(s.lower_bound({poweri,NEG_INF}));
			if(t1!=s.end()&&t1->x==poweri)
			{
				s.erase(t1);
				n-=poweri;
				//trace1(poweri);
			}
		}
		while(1)
		{

			auto t1=s.lower_bound({poweri,NEG_INF});
			ll cost=0;
			if(t1!=s.end()&&t1->x==poweri)
			{
				cost=min(cost,t1->y);
				s.erase(t1);
			}
			else
				break;
			auto t2=s.lower_bound({poweri,NEG_INF});
			if(t2!=s.end()&&t2->x==poweri)
			{
				cost=min(cost,t2->y);
				s.erase(t2);
			}
			else
			{
				s.insert({poweri,cost});
				break;
			}
			s.insert({2*poweri,cost-1});
		}

	}

	ll cost=0;
	//for(auto &node :s)
	//	trace2(node.x,node.y);
	//trace1(n);

	rloop(i,51,0)
	{
		poweri=power(2,i);
		ll k=n&poweri;
		if(k)
		{

			auto t1=s.lower_bound({poweri,NEG_INF});
			n-=poweri;
			cost+=max((ll)t1->y,(ll)(0));
			s.erase(t1);

		}

		while(1)
		{
			auto temp=s.lower_bound({poweri,NEG_INF});
			if(temp==s.end()||temp->x!=poweri)
				break;

			ll cost=temp->y+1;
			s.erase(temp);
			s.insert({poweri/2,cost});
		}

	}
	cout<<cost<<endl;
}
int main()
{
	IOS;
	ll t;
	cin>>t;
	while(t--)
		solve();
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define lb long double
#define mp make_pair
#define pb push_back
#define loop(i,start,n) for(ll i=start;i<n;i++)
#define rloop(i,n,start)for(ll i=n;i>=start;i--)
#define mod (ll)1e18
#define endl "\n"
#define INF     1e15
#define NEG_INF -1e15
#define pll pair<ll,ll>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
ll power(ll x, ll y)
{
    ll temp,f;
    if( y == 0)
        return 1;
    temp = power(x, y/2) ;
    if (y%2 == 0)
        return (temp*temp) ;
    else
    {
        f=(x*temp) ;
        f=(f*temp) ;
        return f;
    }
}
void solve()
{
	//IOS;
	ll n,m;
	cin>>n>>m;
	ll a[m]={0},sum=0;
	multiset<pll> s;
	loop(i,0,m)
	{
		cin>>a[i];
		sum+=a[i];
		s.insert({a[i],0});
	}
	if(sum<n)
	{
		cout<<"-1"<<endl;
		return ;
	}
	ll poweri;
	//trace1(power(2,51));
	loop(i,0,51)
	{
		poweri=power(2,i);
		ll k=n&poweri;
		if(k)
		{
			auto t1=(s.lower_bound({poweri,NEG_INF}));
			if(t1!=s.end()&&t1->x==poweri)
			{
				s.erase(t1);
				n-=poweri;
				//trace1(poweri);
			}
		}
		while(1)
		{

			auto t1=s.lower_bound({poweri,NEG_INF});
			ll cost=0;
			if(t1!=s.end()&&t1->x==poweri)
			{
				cost=min(cost,t1->y);
				s.erase(t1);
			}
			else
				break;
			auto t2=s.lower_bound({poweri,NEG_INF});
			if(t2!=s.end()&&t2->x==poweri)
			{
				cost=min(cost,t2->y);
				s.erase(t2);
			}
			else
			{
				s.insert({poweri,cost});
				break;
			}
			s.insert({2*poweri,cost-1});
		}

	}

	ll cost=0;
	for(auto &node :s)
		trace2(node.x,node.y);
	trace1(n);
 	//return ;
	rloop(i,51,0)
	{
		poweri=power(2,i);
		ll k=n&poweri;
		if(k)
		{

			auto t1=s.lower_bound({poweri,NEG_INF});
			n-=poweri;
			cost=max((ll)t1->y,(ll)(0));
			s.erase(t1);

		}
		ll count=1;
		while(count--)
		{
			auto temp=s.lower_bound({poweri,NEG_INF});
			if(temp==s.end()||temp->x!=poweri)
				break;
			//trace1(poweri);
			ll cost=temp->y+1;
			s.erase(temp);
			s.insert({poweri/2,cost});
			s.insert({poweri/2,cost});
		}

	}
	//trace1(cost);
	cout<<cost<<endl;
}
int main()
{

	ll t;
	cin>>t;
	while(t--)
		solve();
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define lb long double
#define mp make_pair
#define pb push_back
#define loop(i,start,n) for(ll i=start;i<n;i++)
#define rloop(i,n,start)for(ll i=n;i>=start;i--)
#define mod (ll)1e18
#define endl "\n"
#define INF     1e15
#define NEG_INF -1e15
#define pll pair<ll,ll>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
ll power(ll x, ll y)
{
    ll temp,f;
    if( y == 0)
        return 1;
    temp = power(x, y/2) ;
    if (y%2 == 0)
        return (temp*temp) ;
    else
    {
        f=(x*temp) ;
        f=(f*temp) ;
        return f;
    }
}
void solve()
{
	ll n,m;
	cin>>n>>m;
	ll a[m],sum=0;
	multiset<pll> s;
	loop(i,0,m)
	{
		cin>>a[i];
		sum+=a[i];
		s.insert({a[i],0});
	}
	if(sum<n)
	{
		cout<<"-1"<<endl;
		return ;
	}
	ll poweri;
	loop(i,0,61)
	{
		poweri=power(2,i);

		if(n&poweri)
		{
			auto t1=(s.lower_bound({poweri,NEG_INF}));
			if(t1!=s.end()&&t1->x==poweri)
			{
				s.erase(t1);
				n-=poweri;
			}
		}
		while(1)
		{

			auto t1=s.lower_bound({poweri,NEG_INF});
			ll cost=0;
			if(t1!=s.end()&&t1->x==poweri)
			{
				cost=min(cost,t1->y);
				s.erase(t1);
			}
			else
				break;
			auto t2=s.lower_bound({poweri,NEG_INF});
			if(t2!=s.end()&&t1->x==poweri)
			{
				cost=min(cost,t1->y);
				s.erase(t2);
			}
			else
			{
				s.insert({poweri,cost});
				break;
			}
			//trace2(poweri,2*poweri);
			s.insert({2*poweri,cost-1});
		}

	}

	ll cost=0;
	//for(auto &node :s)
	//	trace2(node.x,node.y);
	//trace1(n);

	rloop(i,60,0)
	{
		poweri=power(2,i);
		if(n&poweri)
		{

			auto t1=s.lower_bound({poweri,NEG_INF});
			n-=poweri;
			cost+=max((ll)t1->y,(ll)(0));
			//trace3(poweri,t1->x,t1->y);
			s.erase(t1);

		}

		while(1)
		{
			auto temp=s.lower_bound({poweri,NEG_INF});
			if(temp==s.end()||temp->x!=poweri)
				break;
			//trace1(poweri);
			ll cost=temp->y+1;
			s.erase(temp);
			s.insert({poweri/2,cost});
		}
		//trace1(i);
		//return ;
	}
	cout<<cost<<endl;
}
int main()
{
	IOS;
	ll t;
	cin>>t;
	while(t--)
		solve();
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define lb long double
#define mp make_pair
#define pb push_back
#define loop(i,start,n) for(ll i=start;i<n;i++)
#define rloop(i,n,start)for(ll i=n;i>=start;i--)
#define mod (ll)1e18
#define endl "\n"
#define INF     1e15
#define NEG_INF -1e15
#define pll pair<ll,ll>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
ll power(ll x, ll y)
{
    ll temp,f;
    if( y == 0)
        return 1;
    temp = power(x, y/2) ;
    if (y%2 == 0)
        return (temp*temp) ;
    else
    {
        f=(x*temp) ;
        f=(f*temp) ;
        return f;
    }
}
void solve()
{
	IOS;
	ll n,m;
	cin>>n>>m;
	ll a[m]={0},sum=0;
	multiset<pll> s;
	loop(i,0,m)
	{
		cin>>a[i];
		sum+=a[i];
		s.insert({a[i],0});
	}
	if(sum<n)
	{
		cout<<"-1"<<endl;
		return ;
	}
	ll poweri;
	trace1(power(2,51));
	loop(i,0,51)
	{
		poweri=power(2,i);
		ll k=n&poweri;
		if(k)
		{
			auto t1=(s.lower_bound({poweri,NEG_INF}));
			if(t1!=s.end()&&t1->x==poweri)
			{
				s.erase(t1);
				n-=poweri;
				//trace1(poweri);
			}
		}
		while(1)
		{

			auto t1=s.lower_bound({poweri,NEG_INF});
			ll cost=0;
			if(t1!=s.end()&&t1->x==poweri)
			{
				cost=min(cost,t1->y);
				s.erase(t1);
			}
			else
				break;
			auto t2=s.lower_bound({poweri,NEG_INF});
			if(t2!=s.end()&&t2->x==poweri)
			{
				cost=min(cost,t2->y);
				s.erase(t2);
			}
			else
			{
				s.insert({poweri,cost});
				break;
			}
			s.insert({2*poweri,cost-1});
		}

	}

	ll cost=0;
	//for(auto &node :s)
	//	trace2(node.x,node.y);
	trace1(n);
	ll cal=0;
	rloop(i,51,0)
	{
		poweri=power(2,i);
		ll k=n&poweri;
		if(k)
		{

			auto t1=s.lower_bound({poweri,NEG_INF});
			n-=poweri;
			cost+=max((ll)t1->y,(ll)(0));
			s.erase(t1);

		}

		while(1)
		{
			auto temp=s.lower_bound({poweri,NEG_INF});
			if(temp==s.end()||temp->x!=poweri)
				break;
			cal++;
			assert(cal<1000000);
			ll cost=temp->y+1;
			s.erase(temp);
			s.insert({poweri/2,cost});
		}

	}
	cout<<cost<<endl;
}
int main()
{

	ll t;
	cin>>t;
	while(t--)
		solve();
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define lb long double
#define mp make_pair
#define pb push_back
#define loop(i,start,n) for(ll i=start;i<n;i++)
#define rloop(i,n,start)for(ll i=n;i>=start;i--)
#define mod 1000000007
#define endl "\n"
#define INF     1e15
#define NEG_INF -1e15
#define pll pair<ll,ll>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
ll power(ll x, ll y)
{
    ll temp,f;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return (temp*temp);
    else
    {
        f=(x*temp);
        f=(f*temp);
        return f;
    }
}
void solve()
{
	ll n,m,sum=0;
	cin>>n>>m;
	ll a[m];
	vector<multiset<ll> > vect(60);
	loop(i,0,m)
	{
		cin>>a[i];
		sum+=a[i];
		ll temp=log2(a[i]);
		//trace1(temp);
		vect[temp].insert(0);
	}

	if(n>sum)
	{
		cout<<"-1"<<endl;
		return ;
	}
	ll cost=0,poweri;
	//return ;
	loop(i,0,54)
	{
		poweri=power(2,i);
		if(n&poweri)
		{
			//trace1(poweri);
			if(vect[i].size()!=0)
			{
				cost+=*(vect[i].begin());
				vect[i].erase(vect[i].begin());
			}
			else
			{
				ll temp;
				loop(j,i+1,54)
				{
					if(vect[j].size()!=0)
					{
						temp=j;
						//trace1(temp);
						vect[j].erase(vect[j].begin());
						break;
					}

				}
				ll cost=1;
				rloop(j,temp-1,i+1)
				{
					vect[j].insert(0);
					vect[j].insert(0);
					cost++;
				}
				vect[i].insert(cost);
				vect[i].insert(cost);
				//trace3(i,temp,cost);
				i--;continue;
			}
		}

		ll t=vect[i].size()/2;
		loop(j,0,t)
			vect[i+1].insert(0);
	}
	cout<<cost<<endl;
}
int main()
{
	IOS;
	ll t;
	cin>>t;
	while(t--)
		solve();
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define lb long double
#define mp make_pair
#define pb push_back
#define loop(i,start,n) for(ll i=start;i<n;i++)
#define rloop(i,n,start)for(ll i=n;i>=start;i--)
#define mod 1000000007
#define endl "\n"
#define INF     1e15
#define NEG_INF -1e15
#define pll pair<ll,ll>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
ll power(ll x, ll y)
{
    ll temp,f;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return (temp*temp);
    else
    {
        f=(x*temp);
        f=(f*temp);
        return f;
    }
}
void solve()
{
	ll n,m,sum=0;
	cin>>n>>m;
	ll a[m];
	vector<multiset<ll> > vect(60);
	loop(i,0,m)
	{
		cin>>a[i];
		sum+=a[i];
		ll temp=log2(a[i]);
		//trace1(temp);
		vect[temp].insert(0);
	}

	if(n>sum)
	{
		cout<<"-1"<<endl;
		return ;
	}
	ll cost=0,poweri;
	//return ;
	loop(i,0,54)
	{
		poweri=power(2,i);
		if(n&poweri)
		{
			//trace1(poweri);
			if(vect[i].size()!=0)
			{
				cost+=*(vect[i].begin());
				vect[i].erase(vect[i].begin());
			}
			else
			{
				ll temp;
				loop(j,i+1,54)
				{
					if(vect[j].size()!=0)
					{
						temp=j;
						//trace1(temp);
						vect[j].erase(vect[j].begin());
						break;
					}

				}
				ll cost=1;
				rloop(j,temp-1,i+1)
				{
					vect[j].insert(0);
					cost++;
				}
				vect[i].insert(cost);
				vect[i].insert(cost);
				//trace3(i,temp,cost);
				i--;continue;
			}
		}

		ll t=vect[i].size()/2;
		loop(j,0,t)
			vect[i+1].insert(0);
	}
	cout<<cost<<endl;
}
int main()
{
	IOS;
	ll t;
	cin>>t;
	while(t--)
		solve();
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define lb long double
#define mp make_pair
#define pb push_back
#define loop(i,start,n) for(ll i=start;i<n;i++)
#define rloop(i,n,start)for(ll i=n;i>=start;i--)
#define mod 1000000007
#define endl "\n"
#define INF     1e15
#define NEG_INF -1e15
#define pll pair<ll,ll>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
ll power(ll x, ll y)
{
    ll temp,f;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return (temp*temp);
    else
    {
        f=(x*temp);
        f=(f*temp);
        return f;
    }
}
void solve()
{
	ll n,m,sum=0;
	cin>>n>>m;
	ll a[m];
	vector<multiset<ll> > vect(60);
	loop(i,0,m)
	{
		cin>>a[i];
		sum+=a[i];
		ll temp=log2(a[i]);
		//trace1(temp);
		vect[temp].insert(0);
	}

	if(n>sum)
	{
		cout<<"-1"<<endl;
		return ;
	}
	ll cost=0,poweri;
	//return ;
	loop(i,0,50)
	{
		poweri=power(2,i);
		if(n&poweri)
		{
			//trace1(poweri);
			if(vect[i].size()!=0)
			{
				cost+=*(vect[i].begin());
				vect[i].erase(vect[i].begin());
			}
			else
			{
				ll temp;
				loop(j,i+1,50)
				{
					if(vect[j].size()!=0)
					{
						temp=j;
						//trace1(temp);
						vect[j].erase(vect[j].begin());
						break;
					}

				}
				ll cost=1;
				rloop(j,temp-1,i+1)
				{
					vect[j].insert(0);
					vect[j].insert(0);
					cost++;
				}
				vect[i].insert(cost);
				vect[i].insert(cost);
				//trace3(i,temp,cost);
				i--;continue;
			}
		}

		ll t=0;
		for(auto &node:vect[i])
		{
			if(node==0)
				t++;
		}
		t/=2;
		loop(j,0,t)
			vect[i+1].insert(0);
	}
	cout<<cost<<endl;
}
int main()
{
	IOS;
	ll t;
	cin>>t;
	while(t--)
		solve();
}


#include<bits/stdc++.h>
#include<vector>
#include<utility>
#define pb push_back
#define ll long long
using namespace std;
int t[65],s[65];

int main()
{
	int tst;
	cin>>tst;
	while(tst--)
	{
		memset(t, 0, sizeof(t));
		memset(s, 0, sizeof(s));
		ll n,m,a;
		cin>>n>>m;
		for(int i=0; i<=60; i++)
		{
			if(n&(1LL<<i))
			{
				t[i]++;
			}
		}
		for(int i=0; i<m; i++)
		{

			cin>>a;
			for(int j=0; j<=20; j++)
			{
				if(a&(1LL<<j)) s[j]++;
			}
		}
		bool flag=true;
		int cnt=0;
		for(int i=0; i<=60; i++)
		{
			if(s[i]&&t[i]) s[i]--,t[i]--;
			int x=(s[i])/2;
			s[i+1]+=x;
			if(s[i]==0&&t[i])
			{
				flag=false;
				for(int j=i+1; j<=20; j++)
				{
					if((s[j]&&!t[j])||(s[j]>1))
					{
						flag=true;
						cnt+=(j-i);
					}
				}
			}
			if(!flag) break;
		}
		if(flag) cout<<cnt<<endl;
		else cout<<-1<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#include<vector>
#include<utility>
#define pb push_back
#define ll long long
using namespace std;
int t[65],s[65];

int main()
{
	int tst;
	cin>>tst;
	while(tst--)
	{
		memset(t, 0, sizeof(t));
		memset(s, 0, sizeof(s));
		ll n,m,a;
		cin>>n>>m;
		for(int i=0; i<=60; i++)
		{
			if(n&(1LL<<i))
			{
				t[i]++;
			}
		}
		for(int i=0; i<m; i++)
		{

			cin>>a;
			for(int j=0; j<=31; j++)
			{
				if(a&(1LL<<j)) s[j]++;
			}
		}
		bool flag=true;
		int cnt=0;
		for(int i=0; i<=60; i++)
		{
			if(s[i]&&t[i]) s[i]--,t[i]--;
			int x=(s[i])/2;
			s[i+1]+=x;
			if(s[i]==0&&t[i])
			{
				flag=false;
				for(int j=i+1; j<=31; j++)
				{
					if((s[j]&&!t[j])||(s[j]>1))
					{
						flag=true;
						cnt+=(j-i);
						s[j]--;
					}
				}
			}
			if(!flag) break;
		}
		if(flag) cout<<cnt<<endl;
		else cout<<-1<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#include<vector>
#include<utility>
#define pb push_back
#define ll long long
using namespace std;
int t[65],s[65];

int main()
{
	int tst;
	cin>>tst;
	while(tst--)
	{
		memset(t, 0, sizeof(t));
		memset(s, 0, sizeof(s));
		ll n,m,a;
		cin>>n>>m;
		for(int i=0; i<=60; i++)
		{
			if(n&(1LL<<i))
			{
				t[i]++;
			}
		}
		for(int i=0; i<m; i++)
		{

			cin>>a;
			for(int j=0; j<=31; j++)
			{
				if(a&(1LL<<j)) s[j]++;
			}
		}
		bool flag=true;
		int cnt=0;
		for(int i=0; i<=60; i++)
		{
			if(s[i]&&t[i]) s[i]--,t[i]--;
			int x=(s[i])/2;
			s[i+1]+=x;
			if(s[i]==0&&t[i])
			{
				flag=false;
				for(int j=i+1; j<=31; j++)
				{
					if((s[j]&&!t[j])||(s[j]>1))
					{
						flag=true;
						cnt+=(j-i);
					}
				}
			}
			if(!flag) break;
		}
		if(flag) cout<<cnt<<endl;
		else cout<<-1<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#include<vector>
#include<utility>
#define pb push_back
#define ll long long
using namespace std;
int t[65],s[65];

int main()
{
	int tst;
	cin>>tst;
	while(tst--)
	{
		memset(t, 0, sizeof(t));
		memset(s, 0, sizeof(s));
		ll n,m,a;
		cin>>n>>m;
		for(int i=0; i<=60; i++)
		{
			if(n&(1LL<<i))
			{
				t[i]++;
			}
		}
		for(int i=0; i<m; i++)
		{

			cin>>a;
			for(int j=0; j<=60; j++)
			{
				if(a&(1LL<<j)) s[j]++;
			}
		}
		bool flag=true;
		int cnt=0;
		for(int i=0; i<=60; i++)
		{
			if(s[i]&&t[i]) s[i]--,t[i]--;
			int x=(s[i])/2;
			s[i+1]+=x;
			if(s[i]==0&&t[i])
			{
				flag=false;
				for(int j=i+1; j<=60; j++)
				{
					if((s[j]&&!t[j])||(s[j]>1))
					{
						flag=true;
						cnt+=(j-i);
						s[j]--;
					}
				}
			}
			if(!flag) break;
		}
		if(flag) cout<<cnt<<endl;
		else cout<<-1<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#include<vector>
#include<utility>
#define pb push_back
#define ll long long
using namespace std;
int t[65],s[65];

int main()
{
	int tst;
	cin>>tst;
	while(tst--)
	{
		memset(t, 0, sizeof(t));
		memset(s, 0, sizeof(s));
		ll n,m,a;
		cin>>n>>m;
		for(int i=0; i<=60; i++)
		{
			if(n&(1LL<<i))
			{
				t[i]++;
			}
		}
		for(int i=0; i<m; i++)
		{

			cin>>a;
			for(int j=0; j<=60; j++)
			{
				if(a&(1LL<<j)) s[j]++;
			}
		}
		bool flag=true;
		int cnt=0;
		for(int i=0; i<=60; i++)
		{
			int x;
			if(s[i]>0&&t[i]>0)
			{
				s[i]--,t[i]--;

			x=(s[i])/2;
			s[i+1]+=x;
			continue;
			}
			else if(s[i]==0&&t[i])
			{
				flag=false;
				for(int j=i+1; j<=60; j++)
				{
					if((s[j]>0&&t[j]==0)||(s[j]>1))
					{
						flag=true;
						cnt+=(j-i);
						s[j]--;
					}
				}
			}
			else
			{
				x=s[i]/2;
				s[i+1]+=x;
			}
			if(!flag) break;
		}
		if(flag) cout<<cnt<<endl;
		else cout<<-1<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#include<vector>
#include<utility>
#define pb push_back
#define ll long long
using namespace std;
int t[65],s[65];

int main()
{
	int tst;
	cin>>tst;
	while(tst--)
	{
		memset(t, 0, sizeof(t));
		memset(s, 0, sizeof(s));
		ll n,m,a;
		cin>>n>>m;
	/*	for(int i=0; i<=60; i++)
		{
			if(n&(1LL<<i))
			{
				t[i]++;
			}
		}
		*/
		ll sum=0LL;
		for(int i=0; i<m; i++)
		{

			cin>>a;
			sum+=a;
			for(int j=0; j<=60; j++)
			{
				if(a&(1LL<<j)) s[j]++;
			}
		}
		if(sum<n)
		{
			cout<<-1<<endl;
			continue;
		}
		bool flag=true;
		int cnt=0;
		for(int i=0; i<=60; i++)
		{
			int x;
			if(s[i]>0&&(n&(1LL<<i)))
			{
				s[i]--;

			x=(s[i])/2;
			s[i+1]+=x;
			continue;
			}
			else if(s[i]==0&&(n&(1LL<<i)))
			{
				flag=false;
				for(int j=i+1; j<=60; j++)
				{
					if((s[j]>0&&!(n&(1LL<<j)))||(s[j]>1))
					{
						flag=true;
						cnt+=(j-i);
						s[j]--;
						j--;
						while(i!=j)
						{
							s[j]++;
							j--;

						}
						break;
					//	t[i]--;
					}
				}

			}
			else
			{
				x=s[i]/2;
				s[i+1]+=x;
			}
			if(!flag) break;
		}
		if(flag) cout<<cnt<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#include<vector>
#include<utility>
#define pb push_back
#define ll long long
using namespace std;
int t[65],s[65];

int main()
{
	int tst;
	cin>>tst;
	while(tst--)
	{
		memset(t, 0, sizeof(t));
		memset(s, 0, sizeof(s));
		ll n,m,a;
		cin>>n>>m;
	/*	for(int i=0; i<=60; i++)
		{
			if(n&(1LL<<i))
			{
				t[i]++;
			}
		}
		*/
		ll sum=0LL;
		for(int i=0; i<m; i++)
		{

			cin>>a;
			sum+=a;
			for(int j=0; j<=60; j++)
			{
				if(a&(1LL<<j)) s[j]++;
			}
		}
		if(sum<n)
		{
			cout<<-1<<endl;
			continue;
		}
		bool flag=true;
		int cnt=0;
		for(int i=0; i<=60; i++)
		{
			int x;
			if(s[i]>0&&(n&(1LL<<i)))
			{
				s[i]--;

			x=(s[i])/2;
			s[i+1]+=x;
			continue;
			}
			else if(s[i]==0&&(n&(1LL<<i)))
			{
				flag=false;
				for(int j=i+1; j<=60; j++)
				{
					if((s[j]>0&&!(n&(1LL<<j)))||(s[j]>1))
					{
						flag=true;
						cnt+=(j-i);
						s[j]--;
					//	t[i]--;
					}
				}

			}
			else
			{
				x=s[i]/2;
				s[i+1]+=x;
			}
			if(!flag) break;
		}
		if(flag) cout<<cnt<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define mp make_pair
#define pb push_back
#define VI vector<int>
#define pii pair<int,int>
using namespace std;
LL n; int m;
int A[100010];
map<LL,LL> W;
void upd(LL t){
	for(LL k = 2; k <= t; k <<= 1LL){
		W[k] += W[k>>1]/2;
		W[k>>1] &= 1;
	}
}
int main(){
	int t;
	scanf("%d", &t);
	while(t--){
		W.clear();
		scanf("%lld %d", &n, &m);
		LL sum = 0;
		for(int i = 0; i < m; i++) scanf("%d", &A[i]), sum += A[i];
		if(sum < n){
			printf("-1\n");
			continue;
		}
		sort(A, A + m);
		int j = 0;
		LL ans = 0;
		for(LL i = 0; i < 62; i++){
			if(!(n & (1LL<<i))) continue;
			while(!W[(1LL<<i)]){
				if(A[j] <= (1LL<<i)){
					W[A[j++]]++;
				}else{
					LL d = 1;
					while(A[j] > (1LL<<i)){
						d <<= 1;
						A[j] >>= 1;
						ans++;
					}
					W[A[j++]] += d;
				}
				upd(1LL<<i);
			}
		}
		printf("%lld\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define mp make_pair
#define pb push_back
#define VI vector<int>
#define pii pair<int,int>
using namespace std;
LL n; int m;
int A[100010];
map<LL,LL> W;
void upd(LL t){
	for(LL k = 2; k <= t; k <<= 1LL){
		W[k] += W[k>>1LL]/2;
		W[k>>1LL] &= 1;
	}
}
int main(){
	int t;
	scanf("%d", &t);
	while(t--){
		W.clear();
		scanf("%lld %d", &n, &m);
		LL sum = 0;
		for(int i = 0; i < m; i++) scanf("%d", &A[i]), sum += A[i];
		if(sum < n){
			printf("-1\n");
			continue;
		}
		sort(A, A + m);
		int j = 0;
		LL ans = 0;
		for(LL i = 0; i < 62; i++){
			if(!(n & (1LL<<i))) continue;
			while(true){
				upd(1LL<<i);
				if(W[1LL<<i]) break;
				if(A[j] <= (1LL<<i)){
					W[A[j++]]++;
				}else{
					LL d = 1;
					while(A[j] > (1LL<<i)){
						d <<= 1;
						A[j] >>= 1;
						ans++;
					}
					W[A[j++]] += d-1;
					break;
				}
			}
		}
		printf("%lld\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define mp make_pair
#define pb push_back
#define VI vector<int>
#define pii pair<int,int>
using namespace std;
LL n; int m;
int A[100010];
map<LL,LL> W;
void upd(LL t){
	for(LL k = 2; k <= t; k <<= 1LL){
		W[k] += W[k>>1]/2;
		W[k>>1] &= 1;
	}
}
int main(){
	int t;
	scanf("%d", &t);
	while(t--){
		W.clear();
		scanf("%lld %d", &n, &m);
		LL sum = 0;
		for(int i = 0; i < m; i++) scanf("%d", &A[i]), sum += A[i];
		if(sum < n){
			printf("-1\n");
			continue;
		}
		sort(A, A + m);
		int j = 0;
		LL ans = 0;
		for(LL i = 0; i < 62; i++){
			if(!(n & (1LL<<i))) continue;
			while(true){
				upd(1LL<<i);
				if(W[1LL<<i]) break;
				if(A[j] <= (1LL<<i)){
					W[A[j++]]++;
				}else{
					LL d = 1;
					while(A[j] > (1LL<<i)){
						d <<= 1;
						A[j] >>= 1;
						ans++;
					}
					W[A[j++]] += d;
				}
				upd(1LL<<i);
			}
		}
		printf("%lld\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define pb push_back
using namespace std;
map<LL,LL> W;
int t, m;
LL n;
int main(){
	scanf("%d", &t);
	while(t--){
		scanf("%lld %d", &n, &m);
		LL sum = 0LL;
		for(int i = 0; i < m; i++){
			LL x;
			scanf("%lld", &x);
			W[x]++;
			sum += x;
		}
		if(sum < n){
			printf("-1\n");
			continue;
		}
		LL ans = 0LL;
		for(LL i = 1; i <= n; i <<= 1LL){
			if(!(n & i)) W[i<<1LL] += (W[i]>>1LL);
			else if(W[i]){
				W[i]--;
				W[i<<1LL] += (W[i]>>1LL);
			}else{
				for(LL j = 2*i; ; j++){
					if(W[j]){
						W[j]--;
						while(j > i){
							ans++;
							W[j>>=1LL]++;
						}
						break;
					}
				}
			}
		}
		printf("%lld\n", ans);
		W.clear();
	}
	return 0;
}