#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
template<class T1,class T2> bool cmax(T1 &x,const T2 &y) { if (x<y) { x=y; return 1; }return 0; }
#define all(x) (x).begin(),(x).end()
void solve()
{
string s1,s2; cin>>s1>>s2;
int n=s1.size(),m=s2.size();
vector f(n,vector(m,-1));
int l=0,r=n;
for (int i=0; i<n; ++i)
{
if (s1[i]==s2[0])
{
f[i][0]=i;
}
for (int j=1; j<m; ++j)
{
if (i>0&&s1[i]==s2[j])
{
cmax(f[i][j],f[i-1][j]);
}
}
if (f[i][m-1]>=0)
{
if (i+1-f[i][m-1]<r-l)
{
r=i+1;
l=f[i][m-1];
}
}
}
for (int i=l; i<r; ++i) cout<<s1[i];
cout<<'\n';
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
int T; cin>>T;
while (T--)
{
solve();
}
}