44a197d1179e1553dbe3c24d161f8c7b64f108656307a259aa91b429d81e4429
// https://sio2.mimuw.edu.pl/c/oi34-1/p/hak/
#include <bits/stdc++.h>
// #define GARY_DBG
#define GARY_LIB
constexpr int sizik = 7;
constexpr int sizik2 = 100 * 1001;
constexpr int sizik3 = 18;
#define ar std::array
typedef std::vector<std::vector<int>> _kra;
std::deque<int> wieze[sizik];
std::deque<int> stan_wieze[sizik];
int curr_looker[sizik];
std::pair<int, int> nums[sizik2];
int q(int x, int y) {
return (x + y) % y;
}
int k;
int next(int x) {
return (x == k) ? 1 : x + 1;
}
int prev(int x) {
return (x == 1) ? k : x - 1;
}
struct Move {
int from, to;
};
std::vector<Move> moves;
int kol[sizik2], kol_cnt = 0;
int check(int x, int y) {
return !!(x & (1 << y));
}
int wieza_id[sizik2];
void solve1() {
moves.reserve(40 * sizik2);
int n = 0;
for (int i = 1; i <= k; i++) {
int c;
std::cin >> c;
wieze[i].resize(c);
for (int j = 0; j < c; j++) {
std::cin >> wieze[i][j];
n = std::max(wieze[i][j], n);
wieza_id[wieze[i][j]] = i;
}
for (const auto& a : wieze[i]) {
kol[a] = ++kol_cnt;
}
std::reverse(wieze[i].begin(), wieze[i].end());
}
for (int i = 1; i <= k; i++) {
int c;
std::cin >> c;
stan_wieze[i].resize(c);
for (int j = 0; j < c; j++) {
std::cin >> stan_wieze[i][j];
}
std::reverse(stan_wieze[i].begin(), stan_wieze[i].end());
}
auto wykonaj = [](int a, int b) {
assert(!stan_wieze[a].empty());
moves.push_back({a, b});
stan_wieze[b].push_front(stan_wieze[a].back());
stan_wieze[a].pop_back();
};
for (int i = 2; i <= k; i++) {
while (!stan_wieze[i].empty()) {
wykonaj(i, 1);
}
}
for (int i = 0; i < sizik3; i++) {
for (int j = 0; j < n; j++) {
if (check(kol[stan_wieze[1].back()], i)) {
wykonaj(1, 2);
} else {
wykonaj(1, 1);
}
}
while (!stan_wieze[2].empty()) {
wykonaj(2, 1);
}
}
for (int i = 0; i < n; i++) {
wykonaj(1, wieza_id[stan_wieze[1].back()]);
}
std::cout << moves.size() << '\n';
for (const auto& [a, b] : moves) {
std::cout << a << " " << b << '\n';
}
}
void solve() {
std::cin >> k;
solve1();
}
int32_t main() {
#ifndef GARY_DBG
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
#endif
int t = 1;
// std::cin >> t;
for (; t > 0; t--) {
solve();
}
return 0;
}