#include <bits/stdc++.h>

using namespace std;

int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a * b / gcd(a, b); }

const int N = 1000 + 10;

char s[N], t[N];

bool check(int n, int m, int len) {
    for (int i = 1; i <= len; i++) {
        if (s[i] != t[i]) {
            return false;
        }
    }
    for (int i = len + 1; i <= n; i++) {
        if (s[i] != s[i - len]) {
            return false;
        }
    }
    for (int i = len + 1; i <= m; i++) {
        if (t[i] != t[i - len]) {
            return false;
        }
    }
    return true;
}

char _ans[N];

int main() {
    int T;  scanf("%d", &T);
    while (T--) {
        scanf("%s", s + 1);
        int n = strlen(s + 1);
        scanf("%s", t + 1);
        int m = strlen(t + 1);
        int ans = -1;
        for (int i = 1; i <= min(n, m); i++) {
            if (n % i == 0 && m % i == 0 && check(n, m, i)) {
                int tans = lcm(n / i, m / i) * i;
                if (ans == -1 || tans < ans) {
                    ans = tans;
                    for (int j = 1; j <= tans; j++) {
                        _ans[j] = s[(j - 1) % i + 1];
                    }
                    _ans[tans + 1] = 0;
                }
            }
        }
        if (ans == -1)  puts("-1");
        else    printf("%s\n", _ans + 1);
    }
    return 0;
}