#include #include #include #include #include #include #include #include #include #include #include #include #include #include typedef long long ll; using namespace std; const ll MOD = 1000000007LL; typedef pair P; int main() { cin.sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector> customer(n); for (int i = 0; i < n; i++) { int p; cin >> p; for (int j = 0; j < p; j++) { int a; cin >> a; customer[i].push(a); } } queue

q; for (int i = 0; i < n; i++) { if (customer[i].size()) { q.push(P(customer[i].front(), i)); customer[i].pop(); } } while (q.size()) { P p = q.front(); q.pop(); cout << p.first << ' '; if (customer[p.second].size()) { q.push(P(customer[p.second].front(), p.second)); customer[p.second].pop(); } } cout << "\n"; return 0; }