#include <bits/stdc++.h>
using namespace std;
template <typename U, typename V>
void Min(U &a, const V &b) {
  if (a > b) a = b;
}
template <typename U, typename V>
void Max(U &a, const V &b) {
  if (a < b) a = b;
}
template <typename U, typename V>
void add(U &a, const V &b) {
  a = (a + b) % 1000000007;
}
template <typename U>
U gcd(U a, U b) {
  if (a == 0) return b;
  if (b == 0) return a;
  if (a >= b)
    return gcd(a % b, b);
  else
    return gcd(a, b % a);
}
int pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % 1000000007;
    a = 1LL * a * a % 1000000007;
    b >>= 1;
  }
  return ans;
}
int pow(int a, long long b, int c) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % c;
    a = 1LL * a * a % c;
    b >>= 1;
  }
  return ans;
}
pair<int, int> a[100005];
int main() {
  int ca = 0, T, i, j, k, m, n, K;
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d", &m);
      int mx = 0;
      for (int i = 0; i < m; i++) scanf("%d", &k), k -= i, Max(mx, k);
      a[i] = {mx, m};
    }
    sort(a, a + n);
    int ans = 0;
    long long cur = 0;
    for (int i = 0; i < n; i++) {
      if (cur >= a[i].first) {
        cur += a[i].second;
      } else {
        ans += a[i].first - cur;
        cur = a[i].first + a[i].second;
      }
    }
    printf("%d\n", ans + 1);
  }
}
