#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>

#define ll long long
#define ld double
#define ft first
#define sd second
#define pb push_back
#define eb emplace_back

//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("fast-math")

using namespace std;

ll gcd(ll a, ll b) {
    if (a == 0)
        return b;
    return gcd(b % a , a);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
#ifdef FREOPEN
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ll t;

    cin >> t;

    for (ll step = 0; step < t; ++step) {
        string a, b, s1, s2;
        cin >> a >> b;
        ll len = (ll)a.size()*(ll)b.size() / gcd(a.size(), b.size());
        for (ll i = 0; i < len; i += (ll)a.size()) {
            s1 += a;
        }

        for (ll i = 0; i < len; i += (ll)b.size()) {
            s2 += b;
        }

        if (s1 == s2) {
            cout << s1 << '\n';
        }
        else {
            cout << -1 << '\n';
        }
    }
    return 0;
}