#include <bits/stdc++.h>
using namespace std;
char buf[1 << 23], *p1 = buf, *p2 = buf;
int read() {
  static int res;
  static char op, f;
  for (f = 0,
      op = (p1 == p2 &&
                    (p2 = (p1 = buf) + fread(buf, 1, 1 << 23, stdin), p1 == p2)
                ? EOF
                : *p1++);
       op > '9' || op < '0';
       op = (p1 == p2 &&
                     (p2 = (p1 = buf) + fread(buf, 1, 1 << 23, stdin), p1 == p2)
                 ? EOF
                 : *p1++))
    f |= (op == '-');
  for (res = 0; op >= '0' && op <= '9';
       op = (p1 == p2 &&
                     (p2 = (p1 = buf) + fread(buf, 1, 1 << 23, stdin), p1 == p2)
                 ? EOF
                 : *p1++))
    res = res * 10 + (op ^ 48);
  return f ? -res : res;
}
int cnt[100010], id[100010], maxn[100010], n;
bool cmp(int x, int y) { return maxn[x] < maxn[y]; }
void work() {
  n = read();
  for (int i = 1, j; i <= n; ++i) {
    for (id[i] = i, cnt[i] = read(), maxn[i] = 0, j = 0; j < cnt[i]; ++j)
      maxn[i] = max(maxn[i], read() - j);
  }
  sort(id + 1, id + n + 1, cmp);
  int ans = 0;
  for (int i = 1, sum = 0; i <= n; ++i)
    ans = max(ans, maxn[id[i]] - sum + 1), sum += cnt[id[i]];
  printf("%d\n", ans);
}
signed main(int argc, char **argv) {
  int t = read();
  while (t--) work();
  return 0;
}
