#include<iostream>
using namespace std;
const int MaxL = 55;
int T, n;
int L[3], P[3], Ai[MaxL], Di[MaxL], ti[3];
int visit_hv[3], xep[3];
int mint, rs = 0;
int score() {
int sum = 0;
for(int i = 0; i < n; i++) {
int maxs = 0;
for(int j = 0; j < 3; j++) {
if(Ai[i] <= ti[j] && Ai[i] + Di[i] >= ti[j] + L[xep[j]])
if(P[xep[j]] > maxs) maxs = P[xep[j]];
}
sum += maxs;
}
return sum;
}
void dequy(int gg){
if(gg == 3) {
int count = score();
if(count > rs) rs = count;
return;
}
int st = mint, en = 50;
if(gg != 0) st = ti[gg-1] + L[xep[gg-1]];
for(int i = st; i <= en; i ++) {
ti[gg] = i;
dequy(gg+1);
ti[gg] = 0;
}
}
void hoanvi(int ket) {
if(ket == 3) {
dequy(0);
return;
}
for(int i = 0; i < 3; i++) {
if(visit_hv[i] == 0) {
visit_hv[i] = 1;
xep[ket] = i;
hoanvi(ket+1);
visit_hv[i] = 0;
}
}
}
int main() {
//freopen("input.txt", "r", stdin);
cin >> T;
for(int tc = 1; tc <= T; tc++) {
cin >> n;
for(int i = 0; i < 3; i ++) {
ti[i] = 0;
visit_hv[i] = 0;
}
mint = 10000;
for(int i = 0; i < 3; i++) cin >> L[i];
for(int i = 0; i < 3; i++) cin >> P[i];
for(int i = 0; i < n; i++) {
cin >> Ai[i] >> Di[i];
if(Ai[i] < mint) mint = Ai[i];
}
rs = 0;
hoanvi(0);
cout << "Case #" << tc << endl << rs << endl;
};
return 0;
}