//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#include <bits/stdc++.h>
#include<set>
#include <array>
using namespace std;
#define ll long long
#define endl '\n'
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define con continue
#define ub upper_bound
#define lb lower_bound

#define si(x) int(x.size())
#define sum_all(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
const double pi = 2 * acos(0.0);
const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, -1, 0, 1 };

const int dx8[] = { -1, 0, 1, 0,1,1,-1,-1 };
const int dy8[] = { 0, -1, 0, 1,1,-1,1,-1 };

ll min(ll a,ll b)
{
    if(a<b)return a;
    return b; 
}
ll max(ll a,ll b)
{
    if(a>b)return a;
    return b; 
}
ll ceil1(ll a,ll b)
{
    return(a+b-1)/b;
}
void read(vector<ll> & arr)
{
  for(ll i=0;i<si(arr);i++)
    cin >> arr[i];

}

void read_graph(vector<vector<ll>>& g, ll m)
{
  while(m--)
  {
    ll x,y; cin >> x>> y ;
    x--,y--; 
    g[x].pb(y);
    g[y].pb(x);
  }
}
void solve()
{
 
  string x,y; 
  cin >> x>> y ;
   ll ans=-1; 
  for(ll i=1;i<=min(si(x),si(y));i++)
  {
    if(si(x)%i==0 and si(y)%i==0)
      {
             bool f=true; 
            string str= x.substr(0,i);
            for(ll j=0,k=0;j<si(x);j++,k=(k+1)%(si(str)))
            {
               if(str[k]!=x[j]) f=false; 
            }
            for(ll j=0,k=0;j<si(y);j++,k=(k+1)%(si(str)))
            {
               if(str[k]!=y[j]) f=false; 
            }
            if(f) ans=max(ans,i);
      }
  }
   if(ans==-1)
    {
      cout << -1 << endl; 
      return ; 
    }

    ll l= (si(x)*si(y))/ans;
    string res= x; 
    while(si(res)<l) {
      res+=x;
    } 
    cout << res << endl ;
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t; cin >> t; while (t--)
    solve();

}
