// Created by Nikolay Budin

#ifdef DEBUG
#  define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ff first
#define ss second
#define szof(x) ((int)x.size())
#ifndef LOCAL
#  define cerr __get_ce
#endif

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef unsigned long long ull;

using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;

int const INF = (int)1e9 + 1e3;
ll const INFL = (ll)1e18 + 1e6;
#ifdef LOCAL
	mt19937 tw(9450189);
#else
	mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }

ll get_min_with_mask(int mask) {
	ll res = 0;
	vector<int> digs;
	for (int i = 0; i < 10; ++i) {
		if (mask & (1 << i)) {
			digs.push_back(i);
		}
	}

	if (szof(digs) == 1 && digs[0] == 0) {
		digs.push_back(1);
	}

	if (szof(digs) && digs[0] == 0) {
		swap(digs[0], digs[1]);
	}

	for (int d : digs) {
		res = res * 10 + d;
	}

	return res;
}

void solve() {
	int n;
	cin >> n;
	string s;
	cin >> s;
	vector<int> st_need(n);
	for (int i = 0; i < n; ++i) {
		st_need[i] = 1 << (s[i] - '0');
	}

	ll ans = INFL;

	function<void(vector<int>, ll, ll, bool)> rec = [&](vector<int> need, ll suff, ll suff_coef, bool last_zero) {
		if (szof(need) == 1) {
			int mask = need[0];
			
			ll res = get_min_with_mask(mask);

			ans = min(ans, res * suff_coef + suff);
			return;
		}

		if (szof(need) == 2) {
			// option 1: ... d -> ... d + 1

			for (int d = 0; d < 9; ++d) {
				int comm = (need[0] & ~(1 << d)) | (need[1] & ~(1 << (d + 1)));
				if (d == 0 && comm == 0) {
					comm |= 2;
				}
				ll res = get_min_with_mask(comm);
				res = res * 10 + d;

				ans = min(ans, res * suff_coef + suff);
			}

			// option 2: ... d 9 -> ... d + 1 0

			for (int d = 0; d < 9; ++d) {
				int comm = (need[0] & ~(1 << d) & ~(1 << 9)) | 
					(need[1] & ~(1 << (d + 1)) & ~(1 << 0));
				if (d == 0 && comm == 0) {
					comm |= 2;
				}
				ll res = get_min_with_mask(comm);
				res = (res * 10 + d) * 10 + 9;

				ans = min(ans, res * suff_coef + suff);
			}

			return;
		}

		for (int d = 0; d < 10; ++d) {
			vector<int> next_need;
			for (int i = 0; i < szof(need); ++i) {
				int cd = (i + d) % 10;
				if (i == 0 || cd == 0) {
					next_need.push_back(0);
				}
				next_need.back() |= need[i] & ~(1 << cd);
			}

			rec(next_need, suff + suff_coef * d, suff_coef * 10, d == 0);
		}
	};

	rec(st_need, 0, 1, true);

	cout << ans << "\n";
}


int main() {
#ifdef LOCAL
	auto start_time = clock();
	cerr << setprecision(3) << fixed;
#endif
	cout << setprecision(15) << fixed;
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int test_count = 1;
	cin >> test_count;
	for (int test = 1; test <= test_count; ++test) {
		solve();
	}

#ifdef LOCAL
	auto end_time = clock();
	cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}