#include <bits/stdc++.h>

#define f first
#define s second
#define loop(i, n) for (int i = 0; i < n; ++i)
#define read(a, n) loop(i, n) cin >> a[i];
#define show(a, n)                  \
    loop(i, n) cout << a[i] << " "; \
    cout << endl;

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int gcd(int a, int b){
	if(a < b){
		swap(a, b);
	}
	int r;
	while(b){
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}

int lcm(int a, int b){
	return a * b / gcd(a, b);
}

bool check(int p, string& s){
	int n = s.size();
	if(n % p)
		return false;
	loop(i, p){
		for(int j = i; j + p < n; j += p){
			if(s[j] != s[j + p])
				return false;
		}
	}
	return true;
}

int main(){
    int T;
    cin >> T;
    loop(q, T){
        string s, t;
        cin >> s >> t;
        bool sol = true;
        int i = 1, m = min(s.size(), t.size());
        for(; i <= m; ++i){
        	if(check(i, s) && check(i, t)){
        		break;
        	}
        	if(i == m){
        		sol = false;
        	}
        }
        if(sol){
        	loop(j, i){
	        	if(s[j] != t[j]){
	        		sol = false;
	        		break;
	        	}
	        }
        } 
        if(sol){
        	int k = lcm(s.size() / i, t.size() / i);
        	loop(j, k){
        		cout << s.substr(0, i);
        	}
        }else{
        	cout << -1;
        }
        cout << endl;
    }
    return 0;
}