#include <bits/stdc++.h>
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define for1(i, n) for(int i = 1; i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define clr(x) memset(x, 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define prev asdfsdf
#define fi first
#define se second
using namespace std;
typedef long double ld;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long i64;
int nxt() {
int x;
scanf("%d", &x);
return x;
}
vector <int> x;
vector <vector <int> > ok;
void rec(int pos, set <int> used, int sum) {
if (sum < -4 || sum > 4) return;
if (used.count(0)) {
return;
}
if (sum == 1) {
ok.pb(x);
}
for (int i = -4; i <= 4; ++i) {
if (i == 0) continue;
set <int> u;
u.insert(i);
for (int x : used) {
u.insert(x + i);
u.insert(x);
}
x.pb(i);
rec(pos + 1, u, sum + i);
x.pop_back();
}
}
/*
* 11
-4 -4 3 3 3
-4 -1 3 3
-4 2 3
-3 2 2
-3 4
-2 -1 4
-2 3
-1 -1 -1 4
-1 -1 3
-1 2
1
*/
void solve() {
rec(0, set <int>(), 0);
for (auto & t : ok) {
sort(all(t));
}
sort(all(ok));
ok.erase(unique(all(ok)), ok.end());
/*cout << ok.size() << endl;
for (auto x : ok) {
for (auto y : x) {
cout << y << " ";
}
cout << endl;
} */
int n = nxt();
vector <long long> v[n];
set <pair <int, int> > x[10];
set <pair <int, int> > * y = x + 5;
int sum = 0;
long long ans = 0;
int cur[n];
auto add = [&](int pos, int x) {
ans += v[pos][x];
cur[pos] = x;
for (int i = 1; i < v[pos].size(); ++i) {
if (i == x) continue;
y[i - x].insert(mp(v[pos][x] - v[pos][i], pos));
}
};
auto erase = [&](int pos, int x) {
ans -= v[pos][x];
for (int i = 1; i < v[pos].size(); ++i) {
if (i == x) continue;
y[i - x].erase(mp(v[pos][x] - v[pos][i], pos));
}
};
for (int i = 0; i < n; ++i) {
int k = nxt();
sum += k;
v[i].resize(k + 1);
for (int j = 1; j <= k; ++j) {
v[i][j] = nxt();
}
}
int used[n];
clr(used);
int phase = 1;
for (int i = 0; i < n; ++i) {
add(i, 1);
}
cout << ans << " ";
for (int i = n + 1; i <= sum; ++i) {
long long delta = LLONG_MIN;
vector <pair <int, int> > best;
for (auto p : ok) {
do {
++phase;
vector <pair <int, int> > cur;
cur.reserve(p.size());
long long s = 0;
for (int d : p) {
int ch = -1;
for (auto z : y[d]) {
if (used[z.se] == phase) continue;
ch = z.se;
s -= z.fi;
used[ch] = phase;
break;
}
if (ch == -1) goto l1;
cur.pb(mp(d, ch));
}
if (s > delta) {
delta = s;
best = cur;
}
l1:;
} while(next_permutation(all(p)));
}
for (auto z : best) {
erase(z.se, cur[z.se]);
add(z.se, cur[z.se] + z.fi);
}
cout << ans << " ";
}
cout << "\n";
}
int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int t = 1;
while (t--) solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIGZvcmQoaSwgbikgZm9yKGludCBpID0gKGludCkobikgLSAxOyBpID49IDA7IGktLSkKI2RlZmluZSBmb3JuKGksIG4pIGZvcihpbnQgaSA9IDA7IGkgPCAoaW50KShuKTsgaSsrKQojZGVmaW5lIGZvcjEoaSwgbikgZm9yKGludCBpID0gMTsgaSA8PSAoaW50KShuKTsgaSsrKQojZGVmaW5lIGFsbCh4KSAoeCkuYmVnaW4oKSwgKHgpLmVuZCgpCiNkZWZpbmUgY2xyKHgpIG1lbXNldCh4LCAwLCBzaXplb2YoeCkpCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiNkZWZpbmUgcHJldiBhc2Rmc2RmCiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBkb3VibGUgbGQ7C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