time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given three integers x,yx,y and nn. Your task is to find the maximum integer kk such that 0≤k≤n0≤k≤n that kmodx=ykmodx=y, where modmod is modulo operation. Many programming languages use percent operator % to implement it.
In other words, with given x,yx,y and nn you need to find the maximum possible integer from 00 to nn that has the remainder yy modulo xx.
You have to answer tt independent test cases. It is guaranteed that such kk exists for each test case.
Input
The first line of the input contains one integer tt (1≤t≤5⋅1041≤t≤5⋅104) — the number of test cases. The next tt lines contain test cases.
The only line of the test case contains three integers x,yx,y and nn (2≤x≤109; 0≤y<x; y≤n≤1092≤x≤109; 0≤y<x; y≤n≤109).
It can be shown that such kk always exists under the given constraints.
Output
For each test case, print the answer — maximum non-negative integer kk such that 0≤k≤n0≤k≤n and kmodx=ykmodx=y. It is guaranteed that the answer always exists.
Example
input
Copy
7 7 5 12345 5 0 4 10 5 15 17 8 54321 499999993 9 1000000000 10 5 187 2 0 999999999
output
Copy
12339 0 15 54306 999999995 185 999999998
Note
In the first test case of the example, the answer is 12339=7⋅1762+512339=7⋅1762+5 (thus, 12339mod7=512339mod7=5). It is obvious that there is no greater integer not exceeding 1234512345 which has the remainder 55 modulo 77.
解題說明:水題,直接構造即可。
#include<stdio.h>
int main()
{
int t, x, y, n;
scanf("%d", &t);
while (t--)
{
scanf("%d%d%d", &x, &y, &n);
printf("%d\n", ((n - y) / x)*x + y);
}
return 0;
}