#include using namespace std; #ifdef LOCAL #include #else #define debug(...) 42 #endif int main() { ios_base::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; int qq = 0; while (tt--) { string x, y; cin >> x >> y; ++qq; if (qq == 58) debug(qq, x, y); int sw = 0; if (x.size() > y.size()) { swap(x, y); sw = 1; } int n = (int) x.size(); vector f(n + 1); f[0] = -1; for (int i = 0, j = -1; i < n; i++, j++) { while (j != -1 && x[i] != x[j]) { j = f[j]; } f[i + 1] = j + 1; } int m = (int) y.size(); int ans = -1; int last_x = -1; for (int i = 0; i < 2 * m; i++) { int px = i % n; int py = i % m; if (x[px] < y[py]) { ans = 0; break; } if (x[px] > y[py]) { ans = 1; break; } } debug(ans, sw); if (n % (n - f[n]) == 0) { debug("period", n - f[n]); if (m % (n - f[n]) == 0) { debug("ok"); if (ans == -1) { ans = (n < m ? 0 : 1); } } else { if (ans == -1) { int r = m % (n - f[n]); debug(n, m, r, n - f[n]); for (int i = 0, j = 0; i < n; i++, j++) { if (x[(i + r) % (n - f[n])] < y[j % m]) { ans = 0; debug("here", ans); break; } if (x[(i + r) % (n - f[n])] > y[j % m]) { ans = 1; debug("here", ans); break; } } } } } else { if (m % n == 0 && ans == -1) { ans = (n < m ? 0 : 1); } } assert(ans != -1); ans ^= sw; cout << (ans == 0 ? "Y" : "X") << '\n'; } return 0; }