#include <bits/stdc++.h>
#define FILE_NAME "seif"
#define fast ios_base :: sync_with_stdio(0); cin.tie(0);
#pragma GCC optimize("O3")
#define NMAX 1000010
using namespace std;
ifstream f(FILE_NAME ".in");
ofstream g(FILE_NAME ".out");
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pi;
typedef pair<ld,ld> pct;
const ll inf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;
void add(ll &a , ll b)
{
a += b;
a %= mod;
}
void sub(ll &a, ll b)
{
a = (a - b + mod) % mod;
}
int n, m, pd[1100][1100],t,cate,k, nxt[2][1100][30],ok;
string a,b;
char sol[1100];
int main()
{
f >> t;
while(t--)
{
f >> k;
f >> a >> b;
memset(pd, 0, sizeof pd);
memset(nxt, 0, sizeof nxt);
a = ' ' + a;
b = ' ' + b;
n = a.size() - 1;
m = b.size() - 1;
for(int i = n; i >= 1; --i)
for(int j = m; j >= 1; --j)
if(a[i] == b[j])
pd[i][j] = pd[i+1][j+1] + 1;
else
pd[i][j] = max(pd[i+1][j], pd[i][j+1]);
for(int i = n; i >= 1; --i)
{
for(int c = 0; c <= 25; ++c)
nxt[0][i][c] = nxt[0][i + 1][c];
nxt[0][i][ a[i] - 'A' ] = i;
}
for(int i = m; i >= 1; --i)
{
for(int c = 0; c <= 25; ++c)
nxt[1][i][c] = nxt[1][i + 1][c];
nxt[1][i][ b[i] - 'A' ] = i;
}
int x = 1, y = 1;
do
{
ok = 0;
for(int c = 25; c >= 0; --c)
if(nxt[0][x][c] && nxt[1][y][c] && pd[nxt[0][x][c] + 1][nxt[1][y][c] +1 ] >= k - 1)
{
g << char(c + 'A');
x = nxt[0][x][c] + 1;
y = nxt[1][y][c] + 1;
ok = 1;
k--;
break;
}
}while(ok);
g << '\n';
}
f.close();
g.close();
return 0;
}