#C8108. Membership Fee Calculation
Membership Fee Calculation
Membership Fee Calculation
You are given a fee structure for a membership based on the number of books a member intends to borrow. The rules are as follows:
- The first K books are free.
- The next M books cost P dollars each.
- Any book beyond the free and discounted quota costs Q dollars each.
The total fee for borrowing N books can be computed by the formula:
[ \text{fee}(N) = \begin{cases} 0, & \text{if } N \leq K, \ (N - K) \cdot P, & \text{if } K < N \leq K + M, \ M \cdot P + (N - K - M) \cdot Q, & \text{if } N > K + M. \end{cases} ]
Your task is to implement a program that reads several test cases from standard input and outputs the corresponding membership fee for each case.
inputFormat
The first line contains an integer T, representing the number of test cases. Each of the next T lines contains five space-separated integers:
- N: Total number of books the member wants to borrow.
- K: Number of books that are free.
- M: Maximum number of books that cost P dollars each after the free quota.
- P: Cost per book for up to M books.
- Q: Cost per book for any additional books beyond the preceding conditions.
Input is read from standard input.
outputFormat
For each test case, output a single line containing the total membership fee calculated according to the fee structure described above. Output is written to standard output.
## sample2
10 5 3 2 5
7 5 3 2 5
16
4
</p>