← Home
#include<bits/stdc++.h>
#define N 400005
using namespace std;
int n,ans;
char s[N],t[N],ch;
bool check(int l,int r){
	for(int i=l+1;i<=r;i++)
		if(t[i]!=t[i-1]) return 0;
	return 1;
}
void solve(){
	scanf("%s%s",s+1,t+1);ans=0;
	n=strlen(s+1);
	for(int i=1,c=0;i<=n;i++){
		char x=s[i];
		if(c>0) x=ch,c--;
		if(x!=t[i]){
			if(i>1&&t[i-1]!=t[i]){
				puts("-1");
				return;
			}
			int p=i+c+1;
			while(p<=n&&s[p]==x) p++;
			if(p>n){
				puts("-1");
				return;
			}
			int q=p+1;
			while(q<=n&&s[q]==s[p]) q++;
			int l1=p-i,l2=q-p;
			ans++;
			if(!check(i,i+l2-1)){
				puts("-1");
				return;
			}
			i=i+l2-1;
			c=l1;ch=x;
		}
	}
	printf("%d\n",ans);
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--) solve();
}