#include using namespace std; int rank_char(char c) { if (c == 'o') return 0; if (c == 'g') return 1; return 2; // d } vector count_chars(const string& s) { vector cnt(3, 0); // d, o, g for (char c : s) { if (c == 'd') cnt[0]++; else if (c == 'o') cnt[1]++; else cnt[2]++; } return cnt; } bool has_good_around_after_swap(const string& s, int i) { int n = (int)s.size(); auto get_char_after_swap = [&](int pos) -> char { if (pos == i) return s[i + 1]; if (pos == i + 1) return s[i]; return s[pos]; }; for (int l = i - 3; l <= i; l++) { if (l < 0 || l + 3 >= n) continue; if (get_char_after_swap(l) == 'g' && get_char_after_swap(l + 1) == 'o' && get_char_after_swap(l + 2) == 'o' && get_char_after_swap(l + 3) == 'd') { return true; } } return false; } // 嘘の正規化: // 左から見て、good を作らない隣接逆転を見つけたら即 swap。 // 差分だけ見て good 判定する。 string fake_normalize(string s) { int n = (int)s.size(); while (true) { bool changed = false; for (int i = 0; i + 1 < n; i++) { if (rank_char(s[i]) <= rank_char(s[i + 1])) continue; if (has_good_around_after_swap(s, i)) continue; swap(s[i], s[i + 1]); changed = true; break; } if (!changed) break; } return s; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; while (Q--) { string S, T; cin >> S >> T; if (count_chars(S) != count_chars(T)) { cout << "No\n"; continue; } cout << (fake_normalize(S) == fake_normalize(T) ? "Yes" : "No") << '\n'; } return 0; }