#include using namespace std; bool is_good_string(const string& s) { return s.find("good") != string::npos; } bool wall_type(const string& X) { bool hasG = false, hasD = false; bool seenD = false; bool orderOK = true; // Remove all 'o'. The remaining string must be g...gd...d. for (char c : X) { if (c == 'o') continue; if (c == 'd') { hasD = true; seenD = true; } else { // c == 'g' hasG = true; if (seenD) orderOK = false; } } if (!hasG || !hasD || !orderOK) return false; int lastG = -1; int firstD = (int)X.size(); for (int i = 0; i < (int)X.size(); i++) { if (X[i] == 'g') lastG = i; if (X[i] == 'd') { firstD = i; break; } } int betweenO = 0; for (int i = lastG + 1; i < firstD; i++) { if (X[i] == 'o') betweenO++; } return betweenO >= 3; } bool fast_judge(const string& S, const string& T) { vector cntS(3, 0), cntT(3, 0); // d, o, g for (char c : S) { if (c == 'd') cntS[0]++; else if (c == 'o') cntS[1]++; else if (c == 'g') cntS[2]++; } for (char c : T) { if (c == 'd') cntT[0]++; else if (c == 'o') cntT[1]++; else if (c == 'g') cntT[2]++; } if (cntS != cntT) return false; return wall_type(S) == wall_type(T); } bool bfs_judge(const string& S, const string& T) { if (S.size() != T.size()) return false; if (is_good_string(S) || is_good_string(T)) return false; queue que; set seen; que.push(S); seen.insert(S); while (!que.empty()) { string cur = que.front(); que.pop(); if (cur == T) return true; int n = (int)cur.size(); for (int i = 0; i + 1 < n; i++) { string nxt = cur; swap(nxt[i], nxt[i + 1]); if (is_good_string(nxt)) continue; if (seen.count(nxt)) continue; seen.insert(nxt); que.push(nxt); } } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; const int BFS_LIMIT = 9; while (Q--) { string S, T; cin >> S >> T; bool ans = fast_judge(S, T); if ((int)S.size() <= BFS_LIMIT) { bool brute = bfs_judge(S, T); if (ans != brute) { cerr << "Mismatch found!\n"; cerr << "S = " << S << '\n'; cerr << "T = " << T << '\n'; cerr << "fast = " << (ans ? "Yes" : "No") << '\n'; cerr << "brute = " << (brute ? "Yes" : "No") << '\n'; return 1; } } cout << (ans ? "Yes" : "No") << '\n'; } return 0; }