#include <bits/stdc++.h>
using namespace std;
#define int              long long int
#define double           long double
#define print(a)         for(auto x : a) cout << x << " "; cout << endl


const int M = 1000000007;
const int N = 3e5+9;
const int INF = 2e9+1;
const int LINF = 2000000000000000001;

inline int power(int a, int b, int mod=M) {
    int x = 1;
    a %= mod;
    while (b) {
        if (b & 1) x = (x * a) % mod; 
        a = (a * a) % mod;
        b >>= 1;
    }
    return x;
}


//_ ***************************** START Below *******************************




string a;
string b;

int consistency(int n, int m){
	
	if(n<m) return -1;
	
	vector<int> prefix(26, 0);
	vector<int> sufix(26, 0);
	for(int i=0; i<n; i++){
		prefix[a[i]-'a']++;
		sufix[a[i]-'a']++;
	}
	
	//* fwd 
	int count = 0;
	int i = 0, j = 0;
	
	int s1 = n+1;
	
	while(i<n && j<m){
		
		if(sufix[b[j]-'a'] > 0){
			while(i<n && a[i] != b[j]){
				sufix[a[i]-'a']--;
				i++;
			}
		}
		else{
			count++;
		}
		
		if(i==n) break;
		
		s1 = min(s1, i);
		sufix[a[i]-'a']--;
		i++;
		j++;
	}
	
	if(count > 1 || j!=m ) s1 = -1;
	if(count == 0) s1 = 0;
	
	
	
	//* bkwd 
	count = 0;
	i = n-1, j = m-1;
	
	int s2 = n+1;
	
	while(i>=0 && j>=0){
		
		if(prefix[b[j]-'a'] > 0){
			while(i>=0 && a[i] != b[j]){
				prefix[a[i]-'a']--;
				i--;
			}
		}
		else{
			count++;
		}
		
		if(i==-1) break;
		
		s2 = min(s2, i);
		prefix[a[i]-'a']--;
		i--;
		j--;
	}
	
	if(count > 1 || j!=-1) s2 = -1;
	if(count == 0) s2 = 0;
	
	
	if(s1 == -1 && s2 == -1) return -1;
	if(s1 == -1) return s2+1;
	if(s2 == -1) return s1+1;
	return min(s1, s2)+1;
	
}













// string a;
// string b;

int practice(int n, int m){


    return 0;
}





void solve() {
    
    cin >> a >> b;
	int n = a.size();
	int m = b.size();
    
    cout << consistency(n, m) << endl;


}





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;
}