#include <iostream>
#include <cstring>
using namespace std;
int baoshi[10500]; //数组要开的大一点儿不然一直wa;
int main(){
int t, n, a, b, i, cnt, count, k = 1;
cin >> t;
while (t--){
count = 0;
memset(baoshi, 0, sizeof(baoshi));
cin >> n;
for (int j = 0; j < n; j++){
cin >> a >> b;
baoshi[a] += b;
}
for (i = 1; i <= 10500; i++){ // 巧妙合成
baoshi[i + 1] = baoshi[i + 1] + baoshi[i] / 3;
baoshi[i] %= 3;
cnt = i + 1;
while (baoshi[cnt] >= 3){。
baoshi[cnt + 1] = baoshi[cnt + 1] + baoshi[cnt] / 3;
baoshi[cnt] %= 3;
cnt++;
}
if (baoshi[i] != 0)
count += baoshi[i];
}
cout << "Case #" << k << ":" << endl << count << endl;
k++;
}
return 0;
}