// BY : VIDIT BHARDWAJ
#include<bits/stdc++.h>
using namespace std;

#define int                 long long int
#define ff                  first
#define ss                  second
#define pb                  push_back
#define popb                pop_back
#define lb                  lower_bound
#define ub                  upper_bound
#define all(v)              (v).begin(),(v).end()
#define tc(t)               int t; cin>>t; while(t--)
#define pii                 pair<int,int>
#define max_pq              priority_queue<int>
#define min_pq              priority_queue<int,vector<int>, greater<int> >
#define fast_ios            ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ps(x,y)             fixed<<setprecision(y)<<x 
#define setbits(x)          __builtin_popcountll(x)
#define zerobits(x)         __builtin_ctzll(x)
#define YES                 cout<<"YES"<<"\n"
#define NO                  cout<<"NO"<<"\n"
#define Yes                 cout<<"Yes"<<"\n"
#define No                  cout<<"No"<<"\n"
#define endl                "\n"
        
const int N=200010;
const long long mod=1000000007;
// const long long mod=998244353;
const long long inf=1000000000000000000;
const long double pi=3.14159265358979323846264338;

/*_______________________________________________________________________*/

signed main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    fast_ios
    tc(t){
        string str,s; cin>>str>>s;
        int n=str.size(),m=s.size();

        if(n<m){
        	string temp=str;
        	str=s,s=temp;
        	n=str.size(),m=s.size();
        }

        if(s!=str.substr(0,s.size())){
        	cout<<"-1"<<endl;
        	continue;
        }

    	int it=500,yes=0;
    	string temp=s;
    	while(it>0){
    		if(s.size()%str.size()==0){
    			yes=1;
    			break;
    		}
    		s+=temp,it--;
    	}
    	if(yes){
    		temp=str;
    		int x=s.size()/str.size();
    		for(int i=1;i<x;i++){
    			str+=temp;
    		}
    		if(str==s) cout<<s<<endl;
    		else cout<<"-1"<<endl;
    	}
    	else cout<<"-1"<<endl;
    }

    return 0;
}
/*

*/