Astrophysicists
In many, many years, far, far away, there will be a launch of the first flight to Mars. To celebrate the success, $$$n$$$ astrophysicists working on the project will be given bonuses of a total value of $$$k$$$ gold coins.You have to distribute the money among the astrophysicists, and to make it easier, you have to assign bonuses in silver coins. Each gold coin is worth $$$g$$$ silver coins, so you have to distribute all $$$k \cdot g$$$ silver coins among $$$n$$$ people.Unfortunately, the company has some financial troubles right now. Therefore, instead of paying the number of silver coins written on the bonus, they decided to round this amount to the nearest integer number of gold coins.The rounding procedure is as follows. If an astrophysicist has bonus equal to $$$x$$$ silver coins, and we denote $$$r = x \bmod g$$$, then:   If $$$r \geq \lceil \frac{g}{2} \rceil$$$, the astrophysicist receives $$$x + (g - r)$$$ silver coins;  Otherwise, an astrophysicists receives $$$x - r$$$ silver coins.  Note that due to rounding, the total sum of actually paid money is not, in general, equal to $$$k \cdot g$$$ silver coins. The operation $$$a \bmod b$$$ denotes the remainder of the division of $$$a$$$ by $$$b$$$. Sum of values before rounding has to be equal to $$$k \cdot g$$$ silver coins, but some workers can be assigned $$$0$$$ silver coins.You aim to distribute the bonuses so that the company saves as many silver coins due to rounding as possible. Please note that there is always a distribution in which the company spends no more than $$$k \cdot g$$$ silver coins.
In the first test case, one of the optimal assignments could be the following:  First person: $$$x = 30$$$ silver coins: company pays $$$0$$$, saves $$$30$$$ silver coins,  Second person: $$$x = 140$$$ silver coins: company pays $$$100$$$, saves $$$40$$$ silver coins,  Third person: $$$x = 130$$$ silver coins: company pays $$$100$$$, saves $$$30$$$ silver coins. In the second test case, we could have the following assignment:   First person: $$$x = 8$$$ silver coins: company pays $$$14$$$, spends extra $$$6$$$ silver coins,  Second person: $$$x = 6$$$ silver coins: company pays $$$0$$$, saves $$$6$$$ silver coins. If the bonuses are assigned to $$$7$$$ silver coins for both astrophysicists, then the company would have to pay an additional gold coin to cover the bonuses.
In the first line of input, there is one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) denoting the number of test cases.Each of the following $$$t$$$ lines describes one test case and contains three integers $$$n$$$, $$$k$$$, $$$g$$$ ($$$1 \le n \le 10^9$$$, $$$0 \le k \le 10^9$$$, $$$2 \le g \le 10^9$$$) — respectively the number of astrophysicists in the company, total number of gold coins to assign and the number of silver coins that one gold coin corresponds to.
In a separate line for each test case, output a single integer — the maximum number of silver coins that could be saved due to rounding.
import sys

def calculate_result(n, k, g):
    if k * g - ((g + 1) // 2 - 1) * n < 0:
        return k * g
    else:
        return k * g - (k * g - ((g + 1) // 2 - 1) * n + g - 1) // g * g

def main():
    test_cases = int(sys.stdin.readline())
    results = []
    for _ in range(test_cases):
        (n, k, g) = map(int, sys.stdin.readline().split())
        result = calculate_result(n, k, g)
        results.append(result)
    for result in results:
        print(result)
if __name__ == '__main__':
    main()
import sys
from math import ceil

def solve(n, k, g):
    tot = k * g
    inc = ceil(g / 2) - 1
    max_saved = inc * n
    if max_saved >= tot:
        return tot
    tot -= max_saved
    tot %= g
    saved = (n - 1) * inc
    rem = inc + tot
    if rem >= ceil(g / 2):
        saved -= g - rem
    else:
        saved += rem
    return saved

def main():
    lines = sys.stdin.read().split('\n')
    for l in lines[1:]:
        if len(l) == 0:
            return
        (n, k, g) = [int(i) for i in l.split(' ')]
        print(solve(n, k, g))
main()
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (n, k, g) = readInts()
    if n == 1:
        return 0
    r = (g - 1) // 2
    T = k * g
    if r * n >= T:
        return T
    else:
        T -= n * r
        r1 = (r + T % g) % g
        r2 = (2 * r + T % g) % g
        res1 = r1 + (n - 1) * r if r1 <= r else (n - 1) * r - (g - r1)
        res2 = r2 + (n - 2) * r if r2 <= r else (n - 2) * r - (g - r2)
        return max(res1, res2, 0)
for _ in range(int(input())):
    print(solve())