一道有趣的题目。因为只能从每个数组中拿出一个数字,所以每个数组有用的数字只有从小到大排序之后的前两个数字。另外一个性质是:如果往一个数组里面加入数字,那么这个数组的贡献不会变大。当从一个数组中取出一个数字之后,这个数字并不会消失,它会出现在其它数组中,也就是说所有数组的最小值最终一定会贡献给最终答案。综合这几点,就可以发现,我们可以把从每个数组中取出的数字放到同一个数组 $A$ 中,这个数组的最终贡献就是所有数组的最小值。剩下的数组的贡献是原先每个数组的第二小的值。由于数组 $A$ 的贡献是固定的,要让最终的结果最大,只需要让 $A$ 等于所有数组中第二小的值最小。
// Date: Sat Dec 9 21:17:37 2023
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const double eps = 1e-8;
const int dir[8][2] = {
{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1},
};
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;
const ull Pr = 131;
#define For(i, a, b) for (int i = int(a); i < int(b); ++i)
#define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define For1(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i)
#define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i])
#define f1 first
#define f2 second
#define pb push_back
#define has(a, x) (a.find(x) != a.end())
#define nonempty(a) (!a.empty())
#define all(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#ifdef _DEBUG
#define debug1(x) cout << #x " = " << x << endl;
#define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl;
#define debug3(x, y, z) \
cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl;
#else
#define debug1
#define debug2
#define debug3
#endif
const int N = 50010;
int t, n;
PII a[N];
int main(void) {
#ifdef _DEBUG
freopen("1859b.in", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> t;
while (t--) {
cin >> n;
int cnt, mb = 1e9 + 10;
For1(i, 1, n) {
cin >> cnt;
VI tmp(cnt, 0);
For(j, 0, cnt) { cin >> tmp[j]; }
sort(all(tmp));
a[i].f1 = tmp[0];
a[i].f2 = tmp[1];
mb = min(mb, a[i].f1);
}
sort(a + 1, a + n + 1, [&](const PII &x, const PII &y) {
if (x.f2 < y.f2)
return true;
else if (x.f2 > y.f2)
return false;
return x.f1 < y.f1;
});
ll res = mb;
For1(i, 2, n) { res += a[i].f2; }
cout << res << '\n';
}
return 0;
}