#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <utility>
#include <bits/stdc++.h>
using namespace std;
#define saleh \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr);
#define ll long long
void KMPalgo(const string &s)
{
int n = s.size();
vector<int> pi(n, 0);
for (int i = 1; i < n; i++)
{
int j = pi[i - 1];
while (j > 0 && s[j] != s[i])
j = pi[j - 1];
if (s[j] == s[i])
j++;
pi[i] = j;
}
// for(auto it : pi)cout<<it<<" ";cout<<endl;
int ans = 0;
for (int i = 0; i < n - 1; i++)
{
if (pi[i] == pi[n - 1] && pi[n - 1] != 0)
{
ans = pi[i];
break;
}
}
int length = pi[n - 1];
if (length == 0)
{
cout << "Just a legend\n";
return;
}
for (int i = 0; i < n - 1; i++)
{
if (pi[i] == length)
{
cout << s.substr(0, length) << "\n";
return;
}
}
length = pi[length - 1];
if (length > 0)
{
cout << length << " " << endl;
cout << s.substr(0, length) << "\n";
}
else
{
cout << "Just a legend\n";
}
}
bool check(int one, int five, int ten, int twenty, int fifty, int n, vector<int> &arr)
{
int rem = n - (one + 5 * five + 10 * ten + 20 * twenty + 50 * fifty);
if (rem != 0)
return false;
for (int price : arr)
{
int temp = price;
int t50 = fifty, t20 = twenty, t10 = ten, t5 = five, t1 = one;
while (temp >= 50 && t50 > 0)
temp -= 50, t50--;
while (temp >= 20 && t20 > 0)
temp -= 20, t20--;
while (temp >= 10 && t10 > 0)
temp -= 10, t10--;
while (temp >= 5 && t5 > 0)
temp -= 5, t5--;
while (temp >= 1 && t1 > 0)
temp -= 1, t1--;
if (temp != 0)
return false;
}
return true;
}
int main()
{
saleh;
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
vector<int> prices(m);
for (auto &price : prices)
cin >> price;
int mini = n;
vector<int> res(5, 0);
for (int one = 0; one <= 9; one++)
{
for (int five = 0; five <= 2; five++)
{
for (int ten = 0; ten <= 2; ten++)
{
for (int twenty = 0; twenty <= 4; twenty++)
{
int rem = n - (one + 5 * five + 10 * ten + 20 * twenty);
if (rem < 0 || rem % 50 != 0)
continue;
int fifty = rem / 50;
if (check(one, five, ten, twenty, fifty, n, prices))
{
// cout<<"__"<<endl;
int used = one + five + ten + twenty + fifty;
if (used < mini)
{
mini = used;
res = {one, five, ten, twenty, fifty};
}
}
}
}
}
}
for (auto note : res)
cout << note << " ";
cout << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxzdGFjaz4KI2luY2x1ZGUgPHV0aWxpdHk+CiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBzYWxlaCAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsgXAogICAgY2luLnRpZShudWxscHRyKTsKCiNkZWZpbmUgbGwgbG9uZyBsb25nCgp2b2lkIEtNUGFsZ28oY29uc3Qgc3RyaW5nICZzKQp7CiAgICBpbnQgbiA9IHMuc2l6ZSgpOwogICAgdmVjdG9yPGludD4gcGkobiwgMCk7CgogICAgZm9yIChpbnQgaSA9IDE7IGkgPCBuOyBpKyspCiAgICB7CiAgICAgICAgaW50IGogPSBwaVtpIC0gMV07CiAgICAgICAgd2hpbGUgKGogPiAwICYmIHNbal0gIT0gc1tpXSkKICAgICAgICAgICAgaiA9IHBpW2ogLSAxXTsKICAgICAgICBpZiAoc1tqXSA9PSBzW2ldKQogICAgICAgICAgICBqKys7CiAgICAgICAgcGlbaV0gPSBqOwogICAgfQoKICAgIC8vIGZvcihhdXRvIGl0IDogcGkpY291dDw8aXQ8PCIgIjtjb3V0PDxlbmRsOwoKICAgIGludCBhbnMgPSAwOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuIC0gMTsgaSsrKQogICAgewogICAgICAgIGlmIChwaVtpXSA9PSBwaVtuIC0gMV0gJiYgcGlbbiAtIDFdICE9IDApCiAgICAgICAgewogICAgICAgICAgICBhbnMgPSBwaVtpXTsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgfQogICAgfQoKICAgIGludCBsZW5ndGggPSBwaVtuIC0gMV07CiAgICBpZiAobGVuZ3RoID09IDApCiAgICB7CiAgICAgICAgY291dCA8PCAiSnVzdCBhIGxlZ2VuZFxuIjsKICAgICAgICByZXR1cm47CiAgICB9CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuIC0gMTsgaSsrKQogICAgewogICAgICAgIGlmIChwaVtpXSA9PSBsZW5ndGgpCiAgICAgICAgewogICAgICAgICAgICBjb3V0IDw8IHMuc3Vic3RyKDAsIGxlbmd0aCkgPDwgIlxuIjsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgIH0KCiAgICBsZW5ndGggPSBwaVtsZW5ndGggLSAxXTsKICAgIGlmIChsZW5ndGggPiAwKQogICAgewogICAgICAgIGNvdXQgPDwgbGVuZ3RoIDw8ICIgIiA8PCBlbmRsOwogICAgICAgIGNvdXQgPDwgcy5zdWJzdHIoMCwgbGVuZ3RoKSA8PCAiXG4iOwogICAgfQogICAgZWxzZQogICAgewogICAgICAgIGNvdXQgPDwgIkp1c3QgYSBsZWdlbmRcbiI7CiAgICB9Cn0KYm9vbCBjaGVjayhpbnQgb25lLCBpbnQgZml2ZSwgaW50IHRlbiwgaW50IHR3ZW50eSwgaW50IGZpZnR5LCBpbnQgbiwgdmVjdG9yPGludD4gJmFycikKewogICAgaW50IHJlbSA9IG4gLSAob25lICsgNSAqIGZpdmUgKyAxMCAqIHRlbiArIDIwICogdHdlbnR5ICsgNTAgKiBmaWZ0eSk7CiAgICBpZiAocmVtICE9IDApCiAgICAgICAgcmV0dXJuIGZhbHNlOwoKICAgIGZvciAoaW50IHByaWNlIDogYXJyKQogICAgewogICAgICAgIGludCB0ZW1wID0gcHJpY2U7CiAgICAgICAgaW50IHQ1MCA9IGZpZnR5LCB0MjAgPSB0d2VudHksIHQxMCA9IHRlbiwgdDUgPSBmaXZlLCB0MSA9IG9uZTsKCiAgICAgICAgd2hpbGUgKHRlbXAgPj0gNTAgJiYgdDUwID4gMCkKICAgICAgICAgICAgdGVtcCAtPSA1MCwgdDUwLS07CiAgICAgICAgd2hpbGUgKHRlbXAgPj0gMjAgJiYgdDIwID4gMCkKICAgICAgICAgICAgdGVtcCAtPSAyMCwgdDIwLS07CiAgICAgICAgd2hpbGUgKHRlbXAgPj0gMTAgJiYgdDEwID4gMCkKICAgICAgICAgICAgdGVtcCAtPSAxMCwgdDEwLS07CiAgICAgICAgd2hpbGUgKHRlbXAgPj0gNSAmJiB0NSA+IDApCiAgICAgICAgICAgIHRlbXAgLT0gNSwgdDUtLTsKICAgICAgICB3aGlsZSAodGVtcCA+PSAxICYmIHQxID4gMCkKICAgICAgICAgICAgdGVtcCAtPSAxLCB0MS0tOwoKICAgICAgICBpZiAodGVtcCAhPSAwKQogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICB9CgogICAgcmV0dXJuIHRydWU7Cn0KCmludCBtYWluKCkKewogICAgc2FsZWg7CiAgICBpbnQgdDsKICAgIGNpbiA+PiB0OwogICAgd2hpbGUgKHQtLSkKICAgIHsKICAgICAgICBpbnQgbiwgbTsKICAgICAgICBjaW4gPj4gbiA+PiBtOwogICAgICAgIHZlY3RvcjxpbnQ+IHByaWNlcyhtKTsKICAgICAgICBmb3IgKGF1dG8gJnByaWNlIDogcHJpY2VzKQogICAgICAgICAgICBjaW4gPj4gcHJpY2U7CgogICAgICAgIGludCBtaW5pID0gbjsKICAgICAgICB2ZWN0b3I8aW50PiByZXMoNSwgMCk7CiAgICAgICAgZm9yIChpbnQgb25lID0gMDsgb25lIDw9IDk7IG9uZSsrKQogICAgICAgIHsKICAgICAgICAgICAgZm9yIChpbnQgZml2ZSA9IDA7IGZpdmUgPD0gMjsgZml2ZSsrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBmb3IgKGludCB0ZW4gPSAwOyB0ZW4gPD0gMjsgdGVuKyspCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgZm9yIChpbnQgdHdlbnR5ID0gMDsgdHdlbnR5IDw9IDQ7IHR3ZW50eSsrKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgaW50IHJlbSA9IG4gLSAob25lICsgNSAqIGZpdmUgKyAxMCAqIHRlbiArIDIwICogdHdlbnR5KTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHJlbSA8IDAgfHwgcmVtICUgNTAgIT0gMCkKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlOwoKICAgICAgICAgICAgICAgICAgICAgICAgaW50IGZpZnR5ID0gcmVtIC8gNTA7CgogICAgICAgICAgICAgICAgICAgICAgICBpZiAoY2hlY2sob25lLCBmaXZlLCB0ZW4sIHR3ZW50eSwgZmlmdHksIG4sIHByaWNlcykpCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8vIGNvdXQ8PCJfXyI8PGVuZGw7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpbnQgdXNlZCA9IG9uZSArIGZpdmUgKyB0ZW4gKyB0d2VudHkgKyBmaWZ0eTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmICh1c2VkIDwgbWluaSkKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBtaW5pID0gdXNlZDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICByZXMgPSB7b25lLCBmaXZlLCB0ZW4sIHR3ZW50eSwgZmlmdHl9OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBmb3IgKGF1dG8gbm90ZSA6IHJlcykKICAgICAgICAgICAgY291dCA8PCBub3RlIDw8ICIgIjsKICAgICAgICBjb3V0IDw8IGVuZGw7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0=