#include <iostream>
#include <fstream>
#define MIN 999999999
using namespace std;
int n, p;
int arrHV[5];
int arrNP[5];
int visitedHV[5];
int visitedNP[5];
int arrGhe[61];
int visitedGhe[61];
int Min;
typedef struct Cua {
int vt, sk;
};
Cua arrCua[5];
int SapXepTrai(int VT, int SK) {
int KC = 0;
int L = VT - 1;
int R = VT + 1;
if (visitedGhe[VT] == 0) {
visitedGhe[VT] = 1;
SK--;
KC += 1;
}
while (SK > 0) {
while (L >= 1) {
if (visitedGhe[L] == 1)
L--;
else
break;
}
while (R <= n) {
if (visitedGhe[R] == 1)
R++;
else
break;
}
if (R > n && L >= 1) {
while (SK > 0) {
if (visitedGhe[L] == 0) {
visitedGhe[L] = 1;
SK--;
KC += (VT - L + 1);
}
else if (visitedGhe[L] == 1)
L--;
}
}
if (L <= 0 && R <= n) {
while (SK > 0) {
if (visitedGhe[R] == 0) {
visitedGhe[R] = 1;
SK--;
KC += (R - VT + 1);
}
else if (visitedGhe[R] == 1)
R++;
}
}
while (L >= 1 && R <= n && SK > 0 && visitedGhe[L] == 0 && visitedGhe[R] == 0) {
if ((VT - L + 1) < (R - VT + 1)) {
visitedGhe[L] = 1;
SK--;
KC += (VT - L + 1);
L--;
}
else if ((VT - L + 1) > (R - VT + 1)) {
visitedGhe[R] = 1;
SK--;
KC += (R - VT + 1);
R++;
}
else if ((VT - L + 1) == (R - VT + 1)) {
visitedGhe[L] = 1;
SK--;
KC += (VT - L + 1);
L--;
}
}
}
return KC;
}
int SapXepPhai(int VT, int SK) {
int KC = 0;
int L = VT - 1;
int R = VT + 1;
if (visitedGhe[VT] == 0) {
visitedGhe[VT] = 1;
SK--;
KC += 1;
}
while (SK > 0) {
while (L >= 1) {
if (visitedGhe[L] == 1)
L--;
else
break;
}
while (R <= n) {
if (visitedGhe[R] == 1)
R++;
else
break;
}
if (L <= 0 && R <= n) {
while (SK > 0) {
if (visitedGhe[R] == 0) {
visitedGhe[R] = 1;
SK--;
KC += (R - VT + 1);
}
else if (visitedGhe[R] == 1)
R++;
}
}
if (R > n && L >= 1) {
while (SK > 0) {
if (visitedGhe[L] == 0) {
visitedGhe[L] = 1;
SK--;
KC += (VT - L + 1);
}
else if (visitedGhe[L] == 1)
L--;
}
}
while (L >= 1 && R <= n && SK > 0 && visitedGhe[L] == 0 && visitedGhe[R] == 0) {
if ((VT - L + 1) < (R - VT + 1)) {
visitedGhe[L] = 1;
SK--;
KC += (VT - L + 1);
L--;
}
else if ((VT - L + 1) > (R - VT + 1)) {
visitedGhe[R] = 1;
SK--;
KC += (R - VT + 1);
R++;
}
else if ((VT - L + 1) == (R - VT + 1)) {
visitedGhe[R] = 1;
SK--;
KC += (R - VT + 1);
R++;
}
}
}
return KC;
}
void NhiPhan(int k) {
if (k == 4) {
for (int i = 1; i <= 3; i++) {
cout << arrHV[i] << " ";
}
cout << endl;
return;
}
for (int i = 0; i < 2; i++) {
arrHV[k] = i;
NhiPhan(k + 1);
}
}
void reset() {
for (int i = 1; i <= n; i++)
visitedGhe[i] = 0;
}
void HoanVi(int k) {
if (k == 4) {
int VT1 = arrCua[arrHV[1]].vt;
int SK1 = arrCua[arrHV[1]].sk;
int VT2 = arrCua[arrHV[2]].vt;
int SK2 = arrCua[arrHV[2]].sk;
int VT3 = arrCua[arrHV[3]].vt;
int SK3 = arrCua[arrHV[3]].sk;
reset();
Min = min(Min, SapXepTrai(VT1, SK1) + SapXepTrai(VT2, SK2) + SapXepTrai(VT3, SK3));
reset();
Min = min(Min, SapXepTrai(VT1, SK1) + SapXepTrai(VT2, SK2) + SapXepPhai(VT3, SK3));
reset();
Min = min(Min, SapXepTrai(VT1, SK1) + SapXepPhai(VT2, SK2) + SapXepTrai(VT3, SK3));
reset();
Min = min(Min, SapXepTrai(VT1, SK1) + SapXepPhai(VT2, SK2) + SapXepPhai(VT3, SK3));
reset();
Min = min(Min, SapXepPhai(VT1, SK1) + SapXepTrai(VT2, SK2) + SapXepTrai(VT3, SK3));
reset();
Min = min(Min, SapXepPhai(VT1, SK1) + SapXepTrai(VT2, SK2) + SapXepPhai(VT3, SK3));
reset();
Min = min(Min, SapXepPhai(VT1, SK1) + SapXepPhai(VT2, SK2) + SapXepTrai(VT3, SK3));
reset();
Min = min(Min, SapXepPhai(VT1, SK1) + SapXepPhai(VT2, SK2) + SapXepPhai(VT3, SK3));
}
for (int i = 1; i <= 3; i++) {
if (visitedHV[i] == false) {
visitedHV[i] = true;
arrHV[k] = i;
HoanVi(k + 1);
visitedHV[i] = false;
}
}
}
int main() {
ifstream input("input.txt");
fstream output;
output.open("output.txt");
int t;
input >> t;
for (int tc = 1; tc <= t; tc++) {
input >> n;
for (int i = 1; i <= 3; i++) {
input >> arrCua[i].vt >> arrCua[i].sk;
}
Min = MIN;
HoanVi(1);
output << "Case #" << tc << endl;
output << Min << endl;
}
return 0;
}