#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int j = 1; j <= t; j++) {
    int n;
    cin >> n;
    vector<pair<int, int> > Vect;
    for (int i = 1; i <= n; i++) {
      int k;
      cin >> k;
      int maxVal = -1;
      for (int l = 0; l < k; l++) {
        int a;
        cin >> a;
        maxVal = max(a - l, maxVal);
      }
      Vect.push_back(make_pair(maxVal, k));
    }
    int minPower = -1;
    sort(Vect.begin(), Vect.end());
    int MonsterSum = 0;
    for (int i = 0; i < n; i++) {
      minPower = max(Vect[i].first - MonsterSum, minPower);
      MonsterSum += Vect[i].second;
    }
    cout << minPower + 1 << endl;
  }
  return 0;
}
