import std.algorithm;
import std.conv;
import std.numeric;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip;
		auto t = readln.strip;
		auto m = s.length;
		auto n = t.length;
		auto g = gcd (m, n);
		auto x = s[0..g];
		auto r = x;
		while (r.length % m != 0 || r.length % n != 0)
		{
			r ~= x;
		}
		writeln ((r[0..m] == s && r[0..n] == t) ? r : "-1");
	}
}
