#include <bits/stdc++.h>

using namespace std;

int main() {
    int T, n, a, b, c, d, e, f, g;
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        int ans = 0;
        for (int i = 0; i < n; i++) {
            scanf("%d%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f, &g);
            if (a - d - f + g < 0) continue;
            if (b - d - e + g < 0) continue;
            if (c - f - e + g < 0) continue;
            if (g > e) continue;
            if (g > f) continue;
            if (g > d) continue;
            if (d > a) continue;
            if (d > b) continue;
            if (e > b) continue;
            if (e > c) continue;
            if (f > a) continue;
            if (f > c) continue;
            ans = max(ans, a + b + c - d - f - e + g);
        }
        printf("%d\n", ans);
    }
    return 0;
}