/* Problem URL: https://codeforces.com/contest/2158/problem/D */ #include #include #include using namespace std; using namespace __gnu_pbds; template > using ordered_set = tree; #define V vector #define rmin(a, b) a = min(a, b) #define rmax(a, b) a = max(a, b) #define rep(i, lim) for (int i = 0; i < (lim); i++) #define nrep(i, s, lim) for (int i = s; i < (lim); i++) #define repv(i, v) for (auto &i : (v)) #define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } #define sortv(v) sort(v.begin(), v.end()) #define all(v) (v).begin(), (v).end() using vi = vector; using vvi = vector; using vvvi = vector; using vvvvi = vector; using ll = long long; using vl = vector; using vvl = vector; using vvvl = vector; using vvvvl = vector; template auto operator<<(ostream &os, const vector &vec)->ostream& { os << vec[0]; for (size_t i = 1; i < vec.size(); i++) { os << ' ' << vec[i]; } os << '\n'; return os; } template auto operator>>(istream &is, vector &vec)->istream& { for (auto &i : vec) { is >> i; } return is; } template auto operator<<(ostream &os, const vector> &vec)->ostream& { for (auto &i : vec) { os << i[0]; for (size_t j = 1; j < i.size(); j++) { os << ' ' << i[j]; } os << '\n'; } return os; } template auto operator>>(istream &is, vector> &vec)->istream& { for (auto &i : vec) { for (auto &j : i) { is >> j; } } return is; } const int oo = INT32_MAX >> 1; const ll OO = INT64_MAX >> 1; map>, char>> ops; map>> getlast[2]; void pre() { ops["0000"] = {{}, '0'}; ops["0001"] = {{{1, 3}}, '1'}; ops["0010"] = {{{1, 2}, {1, 3}}, '0'}; ops["0011"] = {{{1, 2}}, '1'}; ops["0100"] = {{{3, 4}, {2, 4}}, '0'}; ops["0101"] = {{{1, 3}, {3, 4}, {2, 4}}, '1'}; ops["0110"] = {{{2, 3}}, '0'}; ops["0111"] = {{{2, 4}}, '0'}; ops["1000"] = {{{2, 4}}, '1'}; ops["1001"] = {{{2, 3}}, '1'}; ops["1010"] = {{{1, 3}, {3, 4}, {2, 4}}, '0'}; ops["1011"] = {{{3, 4}, {2, 4}}, '1'}; ops["1100"] = {{{1, 2}}, '0'}; ops["1101"] = {{{1, 2}, {1, 3}}, '1'}; ops["1110"] = {{{1, 3}}, '0'}; ops["1111"] = {{}, '1'}; getlast[0]["0000"] = {}; getlast[0]["0001"] = {{1, 4}, {1, 3}}; getlast[0]["0010"] = {{1, 3}, {1, 2}}; getlast[0]["0011"] = {{3, 4}}; getlast[0]["0100"] = {{2, 4}, {3, 4}}; getlast[0]["0101"] = {{1, 3}, {1, 2}, {2, 4}}; getlast[0]["0110"] = {{2, 3}}; getlast[0]["0111"] = {{2, 4}}; getlast[0]["1000"] = {{1, 4}, {2, 4}}; getlast[0]["1001"] = {{2, 3}, {1, 4}}; getlast[0]["1010"] = {{2, 4}, {3, 4}, {1, 3}}; getlast[0]["1011"] = {{1, 4}, {2, 4}, {3, 4}}; getlast[0]["1100"] = {{1, 2}}; getlast[0]["1101"] = {{1, 4}, {1, 3}, {1, 2}}; getlast[0]["1110"] = {{1, 3}}; getlast[0]["1111"] = {{1, 4}}; getlast[1]["1111"] = {}; getlast[1]["1110"] = {{1, 4}, {1, 3}}; getlast[1]["1101"] = {{1, 3}, {1, 2}}; getlast[1]["1100"] = {{3, 4}}; getlast[1]["1011"] = {{2, 4}, {3, 4}}; getlast[1]["1010"] = {{1, 3}, {1, 2}, {2, 4}}; getlast[1]["1001"] = {{2, 3}}; getlast[1]["1000"] = {{2, 4}}; getlast[1]["0111"] = {{1, 4}, {2, 4}}; getlast[1]["0110"] = {{2, 3}, {1, 4}}; getlast[1]["0101"] = {{2, 4}, {3, 4}, {1, 3}}; getlast[1]["0100"] = {{1, 4}, {2, 4}, {3, 4}}; getlast[1]["0011"] = {{1, 2}}; getlast[1]["0010"] = {{1, 4}, {1, 3}, {1, 2}}; getlast[1]["0001"] = {{1, 3}}; getlast[1]["0000"] = {{1, 4}}; } #define TEST 1 void solve() { int n; cin >> n; string a, b; cin >> a >> b; auto [ans, cur] = ops[a.substr(0, 4)]; nrep(i, 4, n) { if (a[i] != cur) { ans.emplace_back(1, i); cur = a[i]; } } for (int i = n - 1; i > 3; i--) { if (b[i] != cur) { ans.emplace_back(1, i + 1); cur = b[i]; } } auto bf = getlast[cur - '0'][b.substr(0, 4)]; repv(i, bf) { ans.emplace_back(i); } cout << ans.size() << '\n'; repv(i, ans) { cout << i.first << ' ' << i.second << '\n'; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); pre(); int t; (TEST && cin >> t) || (t = 1); while (t--) { solve(); } }