#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50;
int mp[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
char buf[N];
int main()
{
    int T, n;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%s", &n, buf);
        int sum = accumulate(buf, buf + n, 0, [](auto a, auto b) { return a + mp[b - '0']; });
        for (int i = 0; i < n - 1; i++)
        {
            int cur = sum - (n - i - 1) * 2;
            if (cur >= 6)
                putchar('9'), sum -= 6;
            else if (cur >= 3)
                putchar('7'), sum -= 3;
            else
                putchar('1'), sum -= 2;
        }
        for (int i = 9; i >= 0; i--)
            if (sum == mp[i])
                putchar(i + '0'), sum -= mp[i];
        putchar('\n');
    }
    return 0;
}
