
#include<bits/stdc++.h>
 
using namespace std; 
  
long long mod=1000000007;

string func(string s)
{
	int m=s.length();
	string sub="";
	int fl=0;
	string prev="";
		 for(int i=0;i<m;i++)
		 {
		 	sub+=s[i];
		 	int fl2=0;
		 	
		 	for(int j=i+1;j<m;j=j+i+1)
		 	{
		 		string sub2=s.substr(j,i+1);
		 		
		 		if(sub!=sub2)
		 		{
		 			fl2=1;
		 			break;
				 }
			 }
			 
			 if(fl2==0)
			 {
			 	 
			 	//prev=sub;
			 	break;
			 }
			 
			 
		 }
		 
		 return sub;;
}

int gcd(int a, int b)
{
    // Everything divides 0 
    if (a == 0)
       return b;
    if (b == 0)
       return a;
  
    // base case
    if (a == b)
        return a;
  
    // a is greater
    if (a > b)
        return gcd(a-b, b);
    return gcd(a, b-a);
}
 
int main()
{ 
	int t;
	cin>>t;
	 
	while(t--)
	{ 
	
		string s,t;
		cin>>s>>t;
		
		int m=s.length();
		int n=t.length();
		
		string s1=func(s);
		string s2=func(t);
		
		//cout<<s1<<" "<<s2<<endl;
		if(s1!=s2)
		{
			cout<<-1<<endl;
		}
		else
		{
			
			int tm1=m/s1.length();
			int tm2=n/s2.length();
			
			int x=gcd(tm1,tm2);
			
			if(x>0)
			{
				int lcm=(tm1*tm2)/x;
			
				for(int i=0;i<lcm;i++)
				{
					cout<<s1<<"";
				}
				cout<<endl;
			}
			
		}
		
		
		 
		 
		 
		
	}
 
    return 0;
}
 
// mistakes:
// long long 
//




















 
