#include <bits/stdc++.h>
using namespace std;
long long solve() {
  long long n;
  cin >> n;
  vector<pair<long long, long long>> pk(n);
  for (long long i = 0; i < (n); i++) {
    cin >> pk[i].second;
    pk[i].first = 0;
    for (long long j = 0; j < (pk[i].second); j++) {
      long long x;
      cin >> x;
      pk[i].first = max(pk[i].first, x - j);
    }
  }
  sort((pk).begin(), (pk).end());
  long long ans = 1, cp = 0;
  for (auto x : pk) {
    ans = max(ans, x.first - cp + 1);
    cp += x.second;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ifstream ifs;
  if (""[0]) cin.rdbuf((ifs = ifstream("")).rdbuf());
  ;
  ofstream ofs;
  if (""[0]) cout.rdbuf((ofs = ofstream("")).rdbuf());
  ;
  ofstream lfs;
  if (""[0]) clog.rdbuf((lfs = ofstream("")).rdbuf());
  ;
  int t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    auto s = solve();
    cout << s << endl;
  }
}
