#include <bits/stdc++.h>
using namespace std;
 
long long gcd(long long a, long long b)
{
	while(a && b)
	{
		if(a > b) a = a%b;
		else b = b%a;
	}
	return a+b;
}
long long fact(int n)
{
	if(n == 0) return 1;
	return fact(n-1)*n;
}
long long lcm(long long a, long long b)
{
    long long temp = gcd(a, b);
    return temp ? (a / temp * b) : 0;
}
bool isPolindrom(string s) 
{
	for(int i = 0; i < s.length()/2; ++i)
	{
		if(s[i] != s[s.length()-1-i]) return false;
	}
	return true;
}


int main() { 
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	long long t; cin >> t; 
	while(t--)
	{
		string a, b; cin >> a >> b;
		long long l = lcm(a.length(), b.length());
		int aa = l/a.length();
		string ta = a;
		while(--aa) a+=ta;
		int bb = l/b.length();
		string tb = b;
		while(--bb) b+=tb;
		if(a == b) cout << a << endl;
		else cout << -1 << endl;
	}
	return 0;
}