We need to read multiple test cases. For each test case: A is a small integer (0 ≤ A ≤ 40000), B is a huge integer (up to 250 digits). Compute GCD(A, B). Since A is up to 40000, we can compute B mod A (or treat B as big integer) and then compute GCD of A and (B mod A) using standard Euclidean algorithm. However careful: A can be 0? According to problem: 0 ≤ A ≤ 40000 and A ≤ B < 10^250. If A=0, then GCD(0,B) = B (since B is non-negative). But check: Usually GCD(0,n) = n. However if both zero? But B >= A, so if A=0 then B could be 0? Possibly B may be 0 as well? B < 10^250, but can be 0? Usually B can be 0. But constraint says A ≤ B, so both can be zero. GCD(0,0) is undefined or typically defined as 0. In many programming problems they define GCD(0,0) = 0. We'll adopt: if A==0 then answer is B (as a big integer? But B is big up to 250 digits, but output as integer, so could be large. But GCD(0,B)=B, which could be up to 250 digits. So we must output the whole B. But note: The output for each test case is an integer representing the GCD of A and B. So if A=0, the GCD is B, which may be a large number up to 250 digits. So we need to output that big integer as is. However A is small, but answer may be large if A=0.
Similarly if A !=0, we compute B mod A (which is a remainder < A). Since A ≤ 40000, we can compute B mod A by processing the large number as a string and computing modulo. Then we compute gcd(A, remainder) using Euclidean algorithm. Then output that gcd, which will be ≤ A ≤ 40000, thus fits in int. But if remainder is 0, then gcd = A. So answer is at most 40000. However note: if A=0, answer is B (big). But also what if A is non-zero and B is big but we compute gcd via remainder. That's fine.
Edge Cases: A might be 0 and B is 0: then output 0.
So algorithm per test case:
- Read A (as int) and B (as string).
- If A == 0: output B (the string) (maybe trim leading zeros? Usually as integer representation, so we should output without leading zeros unless it's zero itself. If B is "0" then output "0".)
- Else:
- Compute R = B mod A (using big modulo: iterate through digits, compute (current *10 + digit) % A). But careful: A can be up to 40000, fits in int.
- Then compute gcd_val = gcd(A, R) (if R is 0 then gcd_val = A).
- Output gcd_val as integer.
But wait: The problem statement says: "Each line consists of two number A and B (0 ≤ A ≤ 40000 and A ≤ B < 10^250)." So B is always at least A. But that doesn't guarantee B>=A numerically? Actually B is a big integer up to 250 digits, but A is small. Since A ≤ B, B >= A. So if A > 0, B >= A, so B >= A. However B mod A yields remainder less than A, fine.
But also note: The Euclidean algorithm normally uses (a,b) with a>=b. But gcd(a,b) = gcd(a, b mod a) if a>0. Here we have small a and big b. So we compute b mod a, then compute gcd(a, b mod a). That's correct.
Implementation details:
- Read integer T (number of test cases).
- For each test case: read line. Could be separated by space. We'll read using cin/iostream or scanf. But careful: B may have leading zeros? Possibly but as integer representation, it might not have leading zeros. But if it does, modulo computation should handle leading zeros: it's just digits. For modulo we treat the string as decimal number. Leading zeros don't affect remainder: e.g., "00123" mod 5 = 123 mod 5 = 3? Actually 00123 is 123. So we can process normally, ignoring leading zeros? Actually if we process each digit, starting with remainder 0, then (010+0)%A = 0, then (010+0) again = 0, then (0*10+1)=1, etc. So it's fine. But for output when A=0 we output B as is. If B has leading zeros, should we output them? Usually integer representation does not include leading zeros. But input specification: B is a number, typically without leading zeros unless it's zero. But to be safe we can output the string as given (maybe after stripping leading zeros? But if B = "000", it's zero, we should output "0". In contest problems, they expect standard integer representation. So if A=0, we should convert B to an integer representation? But B can be up to 250 digits, we can just output as string, but we might need to remove leading zeros except when all zeros. So we can implement: if A==0: output normalized B: if B is all zeros, output "0", else output B after stripping leading zeros. But we need to be careful: The input may have leading zeros? The problem says "number" which usually implies no leading zeros. But it's safer to handle.
Alternatively, we could compute GCD using algorithm that handles big numbers directly: while(b not zero) { a = a % b; swap(a,b) } but here a is small initially; after first step, b becomes b%a which is small, so we can compute. Actually we can compute b mod a as described.
Time complexity: O(T * length(B)) which is fine.
Now confirm sample:
2
2 6 -> A=2, B=6, R=6%2=0, gcd(2,0)=2, output 2.
10 11 -> A=10, B=11, R=11%10=1, gcd(10,1)=1, output 1.
Works.
Edge cases: A=1 -> gcd=1 always.
A=40000, B= something big.
Another nuance: In C/C++, reading with cin>>int and then string will work: cin>>A>>B.
But careful: B may be on the same line after A. Yes.
Potential pitfalls: The input file may have blank lines? Not likely.
Now consider when A !=0 but B is huge. Compute modulo: For each character c in B string, do r = (r*10 + (c-'0')) % A.
But note: A can be up to 40000, and r10 + digit can be up to 4000010+9=400009, which fits in int. However r may be up to 39999, times 10 = 399990 plus 9 = 399999, still safe. So int is okay. But we may use long for safety.
After obtaining r, compute gcd(A, r). Use Euclidean algorithm (or C++ std::gcd if available in C++17). But we can implement simple.
Finally, output the result.
One more nuance: When A is 0, we need to output B (maybe huge). But also consider that the Euclidean algorithm would also work: gcd(0, B) = B. So we could handle uniformly: if A==0, answer = B (string). Or we can compute remainder? Actually if A==0, we can't compute mod because division by zero. So separate case.
Alternatively, we can swap if needed: if A==0, print B. else compute.
Also note: A may be 0 and B is huge, but we could also treat gcd(0,B) = B. So fine.
Potential scenario: A>0 but B is also small? Still fine.
Now we can produce code.
But wait: The problem mentions "makes it but with a little integer and another integer that has up to 250 digits." So we are to help Frank.
Thus solution: For each test case:
- read A (int) and B (string)
- if (A == 0) { output B (strip leading zeros) } else { compute r = 0; for each char d in B: r = (r*10 + (d-'0')) % A; then answer = gcd(A, r); output answer; }
But careful: If r == 0 then gcd(A, 0) = A.
Make sure to handle case where B is "0"? If A > 0, then r = 0, gcd(A,0)=A. But if A=0 and B=0, answer 0.
One more nuance: Input constraint: A ≤ B. So if A > 0, B is at least A, but could be exactly A. Then r = 0, answer = A. That's correct.
Implementation:
#include
#include
#include // for __gcd? Not needed.
using namespace std;
int gcd(int a, int b) {
while (b) {
int t = b;
b = a % b;
a = t;
}
return a;
}
int main() {
int T;
cin >> T;
while (T--) {
int A;
string B;
cin >> A >> B;
if (A == 0) {
// output B as integer without leading zeros
// Find first non-zero, if none output "0"
size_t i = 0;
while (i < B.length() && B[i] == '0') ++i;
if (i == B.length()) cout << "0\n";
else cout << B.substr(i) << "\n";
} else {
int r = 0;
for (char c : B) {
r = (r * 10 + (c - '0')) % A;
}
int ans = gcd(A, r);
cout << ans << "\n";
}
}
return 0;
}
But note: There is a potential overflow if A is large (40000) and r10 + digit may exceed int? As argued, it's safe (max < 4000010+9=400009, fits in 32-bit signed int. So fine.
Alternatively, we can use long long for safety.
But is there any case where B is empty? No.
Now consider the scenario when A is negative? Not allowed.
Thus this solution works.
But there's a nuance: The Euclidean algorithm for gcd when one argument is zero returns the other. In our gcd function we handle that as while(b) loop: if b=0, loop skips and returns a. So if r=0, gcd(A,0)=A. Good.
Thus answer.
But note: The problem might require handling large output when A=0. However, we might also compute GCD by swapping and using big integer for B, but we already did simpler.
Now test with sample.
Test with A=0, B="123": output "123".
Test with A=5, B="10": r = 10%5=0, ans = gcd(5,0)=5 -> output 5.
But is that correct? GCD(5,10)=5. Yes.
Test with A=5, B="12": r=12%5=2, gcd(5,2)=1 -> output 1.
Ok.
Edge: A=40000, B is a huge number that mod yields something. Fine.
Now check if A=0 and B has leading zeros: e.g., B="00078". Then we strip leading zeros to "78". That's proper.
Now we must consider that the input might contain extra spaces or newlines. Using cin>> works fine.
Now confirm if any tricky: There's also possibility that A can be up to 40000, but B can be as small as A. That's fine.
One more nuance: The problem statement says: "Frank explained its friend Felman the algorithm of Euclides to calculate the GCD of two numbers. Then Felman implements it algorithm ... and it proposes to Frank that makes it but with a little integer and another integer that has up to 250 digits." So Frank needs to compute gcd(A,B). In the implementation above we used modulo. That's essentially the Euclidean algorithm.
Now, we could also compute gcd by using big integer library but not needed.
Thus solution.
But I've seen similar problems: "GCD2" from SPOJ. Indeed there is a known problem "GCD2" where A is small, B is large string. The solution is exactly computing B mod A and then gcd.
Now let's ensure we handle case where A is zero and B is zero: we output "0". That's proper.
But what about when A is non-zero but B is zero? However constraint says A ≤ B, so if A >0 then B >= A >0, so B can't be zero. So B is at least A >0, so B >0. So we don't need to consider B=0 unless A=0.
Thus final code.
One more thing: The number of test cases is given first. So we read T. There might be spaces before or after lines. Use cin.
Alternatively, we could use scanf for int and char array for B. But string is easier.
Complexities: O(T * |B|) which is at most T * 250 ~ small.
Now, could there be leading spaces in B? No.
Now I'll produce final answer.
But before final, let's double-check the problem statement: "The first line of the input file contains a number representing the number of lines to follow. Each line consists of two number A and B (0 ≤ A ≤ 40000 and A ≤ B < 10^250)." So A and B are on each line. So our reading is correct.
Now, about the output: "Print for each pair (A,B) in the input one integer representing the GCD of A and B." So each on new line.
Thus final code.
I'll include a fast I/O for safety? Not necessary, but can.
Now generate final answer in the required format: Just code.
Thus answer: Provide C++ solution.
But we might also handle large output in case A=0. We already normalized.
Now I'll write the solution in a clear way.
Potential pitfalls: The gcd function using recursion may cause stack overflow for large test cases? But T is not huge, but recursion depth is O(log A). That's fine. But we can implement iterative.
Alright.
I'll produce final answer.
```cpp
#include
#include
using namespace std;
// Iterative Euclidean algorithm
int gcd(int a, int b) {
while (b) {
int t = b;
b = a % b;
a = t;
}
return a;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
int A;
string B;
cin >> A >> B;
if (A == 0) {
// GCD(0, B) = B, output without leading zeros
size_t i = 0;
while (i < B.size() && B[i] == '0') ++i;
if (i == B.size())
cout << "0\n";
else
cout << B.substr(i) << '\n';
} else {
// Compute B mod A
int r = 0;
for (char ch : B) {
r = (r * 10 + (ch - '0')) % A;
}
cout << gcd(A, r) << '\n';
}
}
return 0;
}