#include <iostream>
#include <utility>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
int cases;
cin >> cases;
for (int testCase = 1; testCase <= cases; testCase++)
{
int laundries, washingMachines, dryers, dryTime;
cin >> laundries >> washingMachines >> dryers >> dryTime;
vector<ll> washDurations(washingMachines);
vector<ll> laundryFinishTime(laundries);
priority_queue< pair<ll, int> > washFinishTimes;
for (int washingMachine = 0; washingMachine < washingMachines; washingMachine++)
{
ll washDuration;
cin >> washDuration;
washDurations[washingMachine] = washDuration;
auto washMachineFinishTime = make_pair(-washDuration, washingMachine);
washFinishTimes.push(washMachineFinishTime);
}
int dryersUnderUse = 0;
for (int laundry = 0; laundry < laundries; laundry++)
{
auto bestWashMachineFinishTime = washFinishTimes.top();
washFinishTimes.pop();
ll washFinishTime = -bestWashMachineFinishTime.first;
int washingMachine = bestWashMachineFinishTime.second;
ll nextWashFinishTime = washFinishTime + washDurations[washingMachine];
washFinishTimes.push(make_pair(-nextWashFinishTime, washingMachine));
laundryFinishTime[laundry] = washFinishTime + dryTime;
if (dryersUnderUse < dryers)
{
dryersUnderUse++;
}
else
{
int firstLaundryToFinishDrying = laundry - dryersUnderUse;
ll currentLaundryDryingStartTime = laundryFinishTime[firstLaundryToFinishDrying];
ll currentLaundryWaitTimeToStartDrying = currentLaundryDryingStartTime - washFinishTime;
if (currentLaundryWaitTimeToStartDrying > 0)
laundryFinishTime[laundry] += currentLaundryWaitTimeToStartDrying;
}
}
int lastLaundry = laundries - 1;
cout << "Case #" << testCase << ": " << laundryFinishTime[lastLaundry] << "\n";
}
}