#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define double long double
const int M = 1000000007;
const int N = 3e5+9;
const int INF = 2e9+1;
const int MAXN = 100000;
const int LINF = 2000000000000000001;
//_ ***************************** START Below *******************************
vector<int> a;
//* Template1 for Atleast k (i.e. >= k )
//* We Start with Invalid window
//* Keep expanding Invalid window till it becomes valid
//* Once it's Valid
//* Keep shrinking till it's valid (i.e. becomes invalid)
//* Also keep Computing result
void consistency1(string str, string t){
int n = str.size();
int m = t.size();
unordered_map<char, int> mp;
for(int i=0; i<m; i++){
mp[t[i]]++;
}
int s = 0, e = 0;
int minLen = n+1;
int x = -1;
while(e<n){
//* Calculate State
if(mp.count(str[e])) mp[str[e]]--;
bool isValid = true;
for(auto& it : mp){
if(it.second > 0){
isValid = false;
break;
}
}
//* Invalid window => keep expanding
if(!isValid){
e++;
}
else{
//* Valid window => keep shrinking && Computing Result
while(s<=e){
bool isValid = true;
for(auto& it : mp){
if(it.second > 0){
isValid = false;
break;
}
}
if(!isValid) break;
int len = e-s+1;
if(len < minLen){
minLen = len;
x = s;
}
if(mp.count(str[s])) mp[str[s]]++;
s++;
}
e++;
}
}
if(x==-1){
cout << "-1" << endl;
return;
}
string ans = str.substr(x, minLen);
cout << ans << endl;
}
//* Template2 for Atleast k (i.e. >= k )
//* (based on Cache Invalidation)
//* We Start with Invalid window
//* If Window is valid
//* Keep shrinking till it's valid (i.e. becomes invalid)
//* Also keep Computing result
//* (similar to Cache Invalidation)
void consistency2(string str, string t){
int n = str.size();
int m = t.size();
unordered_map<char, int> mp;
for(int i=0; i<m; i++){
mp[t[i]]++;
}
int s = 0, e = 0;
int minLen = n+1;
int x = -1;
while(e<n){
//* Calculate State
if(mp.count(str[e])) mp[str[e]]--;
//* Valid window => keep shrinking && Computing Result
while(s<=e){
bool isValid = true;
for(auto& it : mp){
if(it.second > 0){
isValid = false;
break;
}
}
if(!isValid) break;
int len = e-s+1;
if(len < minLen){
minLen = len;
x = s;
}
if(mp.count(str[s])) mp[str[s]]++;
s++;
}
//* Invalid window => Expand
e++;
}
if(x==-1){
cout << "-1" << endl;
return;
}
string ans = str.substr(x, minLen);
cout << ans << endl;
}
void solve() {
string str, t;
cin >> str >> t;
consistency1(str, t);
consistency2(str, t);
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}