#include #include /** Since the skill point values ($a, b$) and durations ($x, y$) are constant, the optimal choice for a tired Charlie is always the same throughout the $n$ days. If it's better to take a break once, it's always better to take a break when tired. This simplifies the problem from a complex dynamic programming state into a comparison between two "pure" global strategies: Strategy A (The "One-Shot" Strategy): Charlie decides to never take a break. He works hard for the first $x$ days to gain $x \cdot a$ points. After that, he works tired for the remaining $n - x$ days, gaining $(n - x) \cdot b$ points. Strategy B (The "Cyclic" Strategy): Charlie decides to always take a break when he gets tired. This creates a repeating pattern: $x$ days of fresh work, followed by $y$ days of break. This cycle lasts $x+y$ days and yields $x \cdot a$ skill points. He repeats this cycle as many times as possible within the $n$ days and then uses the remaining days optimally. */ // Use long long for all variables to prevent integer overflow // as input values can be up to 10^9. using ll = long long; void solve() { ll n, a, b, x, y; std::cin >> n >> a >> b >> x >> y; // We can model this problem by considering two main global strategies // Charlie can follow for the entire duration. The optimal solution will // be the better of these two. // --- Strategy A: The "One-Shot" Strategy --- // Charlie works fresh for his first 'x' days and then works tired for all // the remaining days without ever taking a break. ll score_A; if (n > x) { // Works fresh for x days, then tired for the rest. score_A = x * a + (n - x) * b; } else { // Not enough days to get tired, works fresh for all n days. score_A = n * a; } // --- Strategy B: The "Cyclic" Strategy --- // Charlie decides that taking a break is always the best option when he // gets tired. This results in a repeating cycle of 'x' days of fresh work // followed by 'y' days of break. ll score_B = 0; // A full cycle consists of x days of work and y days of break. ll cycle_duration = x + y; ll skill_per_cycle = x * a; // Calculate how many full cycles can be completed within n days. ll num_cycles = n / cycle_duration; // Calculate the number of days remaining after the full cycles. ll remaining_days = n % cycle_duration; // The score from the full cycles is straightforward. score_B = num_cycles * skill_per_cycle; // For the remaining days, Charlie is fresh (as he just finished a break, // or is at the start). He follows the "One-Shot" strategy for these days. if (remaining_days > x) { // Works fresh for x days, then tired for the rest of the remaining days. score_B += x * a + (remaining_days - x) * b; } else { // Not enough remaining days to get tired, works fresh for all of them. score_B += remaining_days * a; } // The maximum skill points obtainable is the maximum of the scores from // the two strategies. Any "mixed" strategy (e.g., working tired for a while, // then resting) is suboptimal because it's always better to start the // high-earning 'a' point periods sooner rather than later, since a >= b. std::cout << std::max(score_A, score_B) << std::endl; } int main() { // Fast I/O std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int t; std::cin >> t; while (t--) { solve(); } return 0; }