We need to process queries online: each query gives a y_j, we add y_j to array a. After each query, we can perform any number of operations: choose any element and add or subtract x (but cannot become negative). Equivalent to we can transform each element a_i to any non-negative integer congruent to a_i modulo x (since we can add/subtract multiples of x; must be non-negative, so we can reach any value >=0 that is ≡ a_i (mod x), by adjusting appropriately: start from a_i, add or subtract x repeatedly; but to ensure non-negative, we may need to add only; we can also subtract if it stays non-negative. Actually, we can only add or subtract x each time; we can do many steps. Starting from a_i, we can reach any number of the form a_i + kx, where k is integer (positive or negative), as long as that number is >=0. Since we can choose any sequence, we can reach exactly the set {a_i mod x} plus multiples of x, as long as the value is >=0. So each element gives us the ability to produce any non-negative integer that has the same remainder modulo x as a_i, provided we can reach that number (the number can be arbitrarily large by adding x repeatedly). But also we can subtract x to get smaller numbers with same remainder, but only if they stay non-negative. So the minimal non-negative number with that remainder is the remainder itself (r = a_i mod x). We can always reach r because we can subtract x repeatedly until we get to r (since a_i >= r, and a_i - kx = r for some k). So each element can produce exactly the set { r, r+x, r+2x, ... }, i.e., all numbers congruent to r mod x. So essentially, having an element with remainder r gives us the ability to "cover" all numbers that are ≡ r (mod x). Since we can use each element only once (we can transform it to some number; we need to assign each element to a distinct non-negative integer? Wait, we are trying to maximize MEX of the array after performing operations. But we can transform each element individually. The array after operations will have some values (maybe duplicates?) and MEX is the smallest non-negative integer not present. We can choose for each element a target value (non-negative, congruent to its remainder mod x) possibly multiple elements can be assigned the same value? But duplicates are allowed; MEX cares about presence. If we have duplicates, that doesn't help covering missing numbers. To maximize MEX, we want to cover as many small numbers as possible (starting from 0 upwards). Since we can assign each element to any number from its residue class, we essentially have a multiset of residues. We need to decide which numbers we can cover using these residues, each residue can be used to cover one number from its class (since each element can become one number). We can choose numbers to cover such that we maximize the smallest missing integer.
So the problem reduces to: we have a multiset of residues modulo x (each y_j gives r = y_j mod x). We can assign each element to a non-negative integer value that is congruent to its residue mod x (i.e., r + k*x for some integer k >= 0). Each element occupies one integer. We want to assign them to integers (non-negative) so that the set of occupied integers is some subset S of N0, and we want to maximize the MEX, i.e., the smallest non-negative integer not in S. Equivalent to find the largest m such that we can cover all numbers 0,1,...,m-1 using the elements (each used once). Since we can assign arbitrarily (within residue classes), we want to know the maximum m such that we can have a matching that covers [0, m-1] with given residues.
This is a classic problem: given residues modulo x, we want to know the maximum MEX. This is similar to "minimum non-negative integer that cannot be represented" when we can increment by x. There's known approach: keep track of counts of residues, and we can assign numbers like: for each value v, we need an element whose residue is v mod x. Since we can choose a large enough multiple, we can always assign a residue r to any number congruent to r. The only limitation is we have limited counts per residue. So we need to check if we can cover numbers 0,1,2,... sequentially.
We can think of this as we have x residue classes. For each residue class r (0 <= r < x), we can produce numbers r, r+x, r+2x, ... . To cover numbers 0,1,...,m-1, we need for each number v, an element from class v mod x. So for each residue class, we need to provide as many numbers as the count of v in [0, m-1] with v mod x = r. That count is approximately floor((m-1 - r)/x) + 1 if r < m else 0. More precisely, count of numbers in [0, m-1] with residue r is: if m <= r: 0; else: ((m-1 - r)//x) + 1. Let's denote needed_r(m) = ceil(m/x) maybe with offset. Actually, for each residue r, the set of numbers congruent to r less than m is { r, r+x, r+2x, ... <= m-1 }. The number of such numbers is: if m <= r: 0; else: floor((m-1 - r)/x) + 1 = ⌈(m - r)/x⌉, but careful: if m > r, then number = ((m-1 - r) // x) + 1. Equivalent to (m - r + x - 1) // x if m > r else 0? Because (m-1 - r)//x + 1 = (m- r -1)//x +1 = floor((m-r-1)/x)+1 = ceil((m-r)/x). Yes, needed_r(m) = max(0, ceil((m - r)/x)). So we need for each r: count_r (available residues) >= needed_r(m). Since we can only assign each element once.
Thus after each query, we have counts cnt[0..x-1]. We need to find the largest m such that for all r, cnt[r] >= ceil((m - r)/x) (with the max(0)). Actually m is unknown and we need to output the MEX which is the maximal m satisfying that condition.
Observation: The condition is monotonic in m: if m works, then all smaller m also work? Not necessarily because needed counts increase with m, and if we have enough counts for m, then for smaller m needed counts are less, so condition holds. So m is monotonic in the sense that the set of feasible m is up to some maximum. So we can binary search m after each query? But q up to 4e5, and x up to 4e5. We need O(log something) per query. Could we maintain a data structure to quickly compute MEX? There is known solution: maintain an array "rem" or "ans" where we track for each residue the smallest number that we can produce using that residue class that hasn't been used yet. Equivalent to we have a set of numbers that we can assign to each element. Since we want to cover from 0 upward, we can think of each residue r giving a "next available" number: the smallest number >= some base that we haven't assigned yet from that class. For each residue class, we can assign numbers in increasing order: first r, then r+x, then r+2x, etc. So each time we get a new element with residue r, we can assign it to the smallest not yet covered number from that residue class. Then the MEX will be the smallest number that hasn't been assigned yet, which we can maintain as a global variable.
Yes, this is typical: we can simulate a process where we have a counter for each residue class: the current "next" number we can assign from that class. Initially for each r, the smallest number is r. We'll maintain an array "ptr" of length x, where ptr[r] = the next value we can assign for an element with residue r (i.e., the smallest number not yet taken from that class). When we get a new element with residue r, we assign it to value v = ptr[r], then we increment ptr[r] by x (since next number in that class). However, note that some numbers might have already been assigned by other residues? Actually each number belongs to exactly one residue class (v mod x). So if we only assign numbers from each residue class in increasing order, the sets are disjoint across classes. Thus we can independently manage each residue class. The only thing: MEX is the smallest non-negative integer not assigned overall. If we assign numbers greedily by using each new element to fill the smallest possible number that we haven't yet covered from its residue class, then after each query, the set of assigned numbers is exactly the union of first cnt[r] numbers from each residue class (starting from r). Because each residue class we use the numbers in order. So this greedy assignment is optimal for maximizing MEX, because we always cover the smallest possible numbers from each residue, leaving larger numbers uncovered, which yields the smallest missing number as high as possible. So indeed the MEX after assigning all elements greedily is the answer. This is a classic approach used in many Codeforces problems (like MEX and increments, etc.)
Thus after each query we can update cnt[r] for that residue, and compute the new MEX. But we need to output the MEX after each query. We can maintain a global variable "mex" that is the current smallest number not yet assigned. Since we assign numbers in increasing order across classes, the set of assigned numbers will be all numbers from 0 up to some point, possibly with gaps? Actually because we assign numbers per residue, we may skip some numbers if a residue class hasn't been used enough to cover its numbers. For example, suppose x=3, residues: we have elements for residue 0 and 2 but none for residue 1. Then we assign numbers: from residue 0: 0,3,6,...; from residue 2: 2,5,8,...; residue 1: none, so number 1 is missing. So the set of assigned numbers is not necessarily contiguous from 0; it will have gaps if a residue class is missing numbers. The MEX is the smallest missing number. Under greedy assignment, the numbers assigned are exactly the multiset of numbers obtained by taking the smallest cnt[r] numbers from each residue class. So the overall assigned set might have holes. The MEX is the minimal number not in that set.
Now we need to compute MEX quickly after each addition. One way: maintain an array "used" for numbers up to some bound? But numbers can go up to large values (since cnt[r] can be large, up to q). However, the MEX will not exceed q + something? Actually, maximum MEX can be at most q + something? Because we have q elements, we can cover at most q distinct non-negative integers. So MEX <= q (since if we cover 0..q-1, then MEX = q). Actually if we cover all numbers from 0 to q-1, then MEX = q. So MEX <= q. Since q <= 4e5, we can afford to maintain a boolean array of size up to maybe q + something (like up to q + x). But note that the numbers we assign may exceed q. However, we only care about numbers up to the current MEX+? Actually we need to know the smallest missing number. Since we have only q elements, MEX is at most q. So we can maintain a boolean array "covered" of size, say, up to q + some margin (like q + x) to be safe, because when we assign a new number, if it is > q, it's irrelevant for MEX because MEX <= q. But careful: suppose we have x=1, then residues all 0, we assign numbers 0,1,2,... as we get elements. MEX will be equal to count of elements. So we need to cover numbers up to q-1, so we need to mark numbers up to q-1. If we assign a number greater than q, we can ignore it because it's > q, thus not affecting MEX. But we might assign numbers exactly q? If we have q elements, we might assign number q only if we have enough elements? Actually with x=1, each element can produce any number. To maximize MEX, we would assign 0,1,...,q-1, not q. Since we have only q elements, we cannot cover q because that would require q+1 elements to cover 0..q. So we will never need to assign a number > q? But is it possible that greedy assignment assigns a number > q? For residue r, ptr[r] starts at r. After cnt[r] assignments, the assigned numbers are r, r+x, ..., r+(cnt[r]-1)*x. The largest such number for any residue could be > q. But if cnt[r] is large, the numbers can exceed q. However, for MEX up to q, we only care about numbers <= q. So we can ignore assignments that exceed some threshold. But we need to know whether numbers <= q are covered. So we can maintain an array "covered" of size maybe up to q + max(x) to be safe? But we don't know which numbers will be assigned that are > q but may affect later when more elements come? Actually MEX can increase beyond current q as more elements are added, up to the total number of elements processed. So after j queries, q_j = j. MEX <= j. So we need to track coverage up to j. Since j increases gradually, we could maintain a dynamic structure that can mark numbers up to j. j goes up to 4e5, so we can maintain a boolean array of size maybe 4e5 + x + 5, and as j grows we may need to consider larger numbers. But we can also maintain the greedy approach differently: Instead of simulating assignment to numbers directly, we can compute MEX by considering the "minimum value not yet taken" across residue classes.
Standard solution: maintain an array "cnt" for residues, and maintain a variable "mex". Also maintain an array "have" for numbers? There is known solution: use a segment tree or set to track the smallest number not covered. The typical solution for problem "MEX Queries" (CF 1082C? Actually this problem is CF 1194E? No, it's "MEX and Increments"? Actually similar to "MEX and increments" but with modulo x. There's a known problem: "MEX problem with modulo x" cf #1475F? Wait there is Codeforces problem 1490F? Not exactly.
I recall a Codeforces problem: "Edu Round 103 problem D"? Actually problem "MEX Queries" 1732B? Let me think.
Alternatively, we can think in terms of the "minimum achievable" MEX after each query is the answer. There is known approach: For each residue r, we maintain the smallest number in that class not yet assigned, call it val[r] = r + k[r]*x, where k[r] is the count of elements of residue r used so far. Initially val[r] = r. When we get a new element with residue r, we will "reserve" val[r] and then increment k[r] -> val[r] += x. So we can keep an array "next" of length x, storing the current smallest assignable number for that residue.
Now, the set of reserved numbers S = { next[r] before increment for each element }. We want the MEX of S. Since S is a multiset? Actually each element reserves a distinct number; numbers can be repeated? No, because each residue class reserves distinct numbers (since we increment by x). Also different residue classes produce numbers that are distinct because their residues differ mod x. Actually two different residues r1 and r2 can produce the same number only if r1 ≡ r2 mod x, which is impossible if they are different residues modulo x. So all numbers reserved are distinct. So S is a set.
Thus after each query, we have added one new number to S (the value assigned from that residue). We need to output the smallest non-negative integer not in S.
We can maintain a boolean array "exists" of size up to something, marking when a number is added to S. Since numbers added can be up to large values (if a residue class gets many elements), but the MEX will be at most total elements. So we can maintain a data structure (like a set of missing numbers) but better: we can maintain a variable "cur" representing the current candidate for MEX. Since we only add numbers, the MEX can only increase or stay the same? Actually when we add a new number, the MEX can increase if we fill the current missing number, but it cannot decrease because we only add numbers, so the set S grows; the smallest missing integer could become larger if we fill the previous MEX, but never becomes smaller because we don't remove numbers. So MEX is non-decreasing.
Thus we can maintain a variable "mex" initially 0. After each insertion of a new number v, if v == mex, then we need to increase mex to the next smallest integer not in S. Since we only add numbers, we can simply while (S contains mex) mex++. So we need to check if mex is present. But we need to efficiently answer whether mex is in S. Since mex is at most total elements, we can maintain a boolean array "present" of size up to q + something. However, the numbers v we add might be larger than q; they won't affect mex until mex becomes that large, but mex <= total elements, so v > total elements currently is > mex? Actually v could be > current number of elements. For example, x=3, after first query with y=10, residue 1? Actually 10 mod 3 = 1, so val[1] = 1, assign 1. So v=1, that's <= j. As we get more elements, val[r] might become > j. For example, if we get many elements with residue 0, we assign 0,3,6,... The numbers 6 may appear when j=4, but 6 >4. That's okay; it's not going to be the mex (since mex <=4). However, eventually as j grows, mex may increase to 6. But we need to know if 6 is present when mex reaches 5? Actually we need to maintain presence for numbers up to maybe j, because mex <= j. So we can store presence for numbers up to some bound. Since j goes up to 4e5, we can allocate an array of size, say, q + x + 5 (or maybe q + max(x) because val[r] may be up to r + (cnt[r]-1)*x, and cnt[r] can be up to q. So the maximum number assigned could be up to (x-1)+(q-1)x = qx - 1? That's huge (up to 1.6e11) but we only care about numbers up to q. However, note: as j increases, mex may become larger than j? Actually mex <= j, because we have j numbers; we can cover at most j distinct numbers, so mex cannot exceed j. So at step j, we only need to know presence for numbers <= j. Since we assign numbers up to j? Not necessarily; we might assign numbers > j. But those numbers > j are irrelevant for mex at step j because mex <= j. However, as j increases, those numbers > j might become <= j later. For example, when j=4, we might assign number 6. At that moment j=4, mex <=4, so 6 is not needed. But later at j=7, 6 may become <=7, and we need to know if 6 is present. So we need to store the fact that 6 is present even though it's > current j, because later it will become relevant. But we can't store all numbers because they could be huge. However, note that we assign numbers from each residue in order. For each residue r, the numbers assigned are: r, r+x, r+2x, ... So they form an arithmetic progression. The set S is exactly the union of these arithmetic sequences up to the counts. So for each residue r, we have assigned numbers up to val[r] - x = r + (cnt[r]-1)*x. So among these numbers, some may be larger than current j but could become <= future j. Since j can go up to total q, we need to know for numbers up to q (final j) which are present. The maximum final q is 4e5. So we only care about numbers up to q_max = total number of queries (since mex final <= q_max). So we only need to track numbers up to q_max (maybe plus a bit more). Because any number > q_max will never be needed to determine mex, because mex will never exceed q_max. However, could mex exceed q_max? No, because at final we have only q_max numbers, so mex <= q_max. So indeed we only need to know presence for numbers in [0, q_max]. But the assigned numbers can be > q_max; we can ignore those. So we can cap our consideration to numbers <= q_max (or maybe q_max + something because after j queries, mex <= j <= q_max). So we can maintain a boolean array "present" of size q_max + 1 (or maybe q_max + x to be safe) to mark numbers that are assigned and are <= q_max. But what about numbers > q_max? They are irrelevant. However, we must be careful: when we assign a number v = next[r] before increment, if v > q_max, we can simply ignore it (not mark present) because it will never affect mex. But then later, if we later increase q_max? Actually q_max is fixed from start (given q). So we can preallocate array of size q + 5 maybe. Since q up to 4e5, that's fine.
But wait: q is up to 4e5, x up to 4e5, so q_max=4e5. So array of size 400001 is fine. However, consider the case where x is large (like 4e5) and we have few elements, but the assigned numbers might be large? For example, x=400000, first element y=0 gives residue 0, assign 0. Then next element y=400000? Actually y=400000 mod x =0, then we assign next number for residue 0 is 400000. That's > q (if q is small). But q is the total number of queries. In this scenario, q could be 2, so q_max=2. 400000 > 2, so we ignore it. At step 2, we have assigned numbers: 0 and 400000. The set S includes 0 and 400000. The mex is 1 because 1 is missing. Since we ignore 400000, we have marked only 0 as present. That's fine; mex is 1. So we don't need to mark numbers > q_max.
But what about later when we have more queries? q_max is the total number of queries (maximum j). At step j, the mex is at most j. So we could allocate array of size q (total queries) + some safety margin. However, the numbers assigned at step j could be up to (j-1)*x + (x-1) which could be > j. But we only care if that number <= j? Actually at step j, mex <= j, so we need to know presence for numbers <= j. So we could mark numbers only if they are <= j at the time they are assigned, but later when j increases, some previously assigned numbers that were > previous j may become <= current j. For example, x=3, q=7. At step 4, we assign number 6? Let's simulate. At step 4, j=4, assign numbers maybe 6? Actually with x=3, if we have many residues 0, we could get 6 at step? Suppose we have 3 residues 0: after first three elements of residue 0: assigned 0,3,6. At j=3, numbers assigned: 0,3,6. mex=1. At j=3, 6 >3, but mex=1. At j=4, we add another element maybe residue 1: assign 1. Now numbers: 0,3,6,1. Sorted:0,1,3,6. mex=2. 6 is still >4? j=4, but mex=2, still <6. At j=5, add residue 2: assign 2. Now numbers:0,1,2,3,6. mex=4? Actually check: 0,1,2,3 present, 4 missing, so mex=4. j=5, mex=4, still <=5. 6 is still >5? 6>5, but mex=4. At j=6, add another residue? suppose we add residue 0 again: assign 9? Wait next for residue 0 would be 9 (since 0,3,6 already taken). 9 >6, but mex now? j=6, current numbers:0,1,2,3,6,9? Actually need to compute: previous: cnt[0]=3 (0,3,6), cnt[1]=1 (1), cnt[2]=1 (2). At j=5, we had cnt[0]=3, cnt[1]=1, cnt[2]=1 -> S = {0,3,6,1,2}. mex=4. At j=6, add another element, say residue 0 again -> assign 9 (since next for residue 0 is 9). Now numbers: 0,1,2,3,6,9. mex=4 still. j=6, mex=4, 9>6 irrelevant. At j=7, add residue 0 again -> assign 12, still >7. mex remains 4? Actually we might add residues to fill 4. To get mex=5, we need a number 4 or 5? Actually mex=4 means 4 missing. To fill 4, we need an element with residue 4 mod x? For x=3, residue 1? Wait 4 mod 3 =1. So we need a residue 1 element to get 4 (since 1+3=4). So eventually we may assign 4 from residue 1. That's <= q=7. So the number 6 never becomes <= mex? Actually mex may eventually become 7? With enough elements, mex can become 7. Then we would need to know if 6 is present. 6 is present from earlier. So at j large enough (j>=6), we need to know that 6 is present. But 6 was assigned at an early step when j was small, and at that time we might have ignored marking it if we only marked numbers <= j (which at that time was small). But later when j increases, we need to know that 6 is present. So we cannot ignore numbers > current j; we need to mark them if they are <= final q. Since we don't know future j, but we know final q (the total number of queries) from the start. So we can allocate array of size q_max (total queries) + some margin, and mark any assigned number v if v <= q_max. Then later when j increases, we can query presence of numbers up to j (which are all <= q_max). So we need to mark all numbers v that are <= q_max, regardless of when they are assigned. Since q_max is given (the total number of queries q), we can pre-allocate an array present of size q+1 (or maybe q+x to be safe). But is it guaranteed that any number that could affect mex (i.e., number <= final q) will be assigned only if v <= q? Actually we may assign v > q but later that number might be <= q? No, if v > q, then v > q_max, so it will never be <= q_max, and thus never needed for mex. So we only need to mark numbers <= q_max. But note: mex can be at most q, so numbers > q are irrelevant. So yes, we can ignore numbers > q.
But we need to be careful: what if q is 4e5 and we assign a number exactly 400000? That's <= q if q=400000? Actually q is up to 4e5, so q could be 400000. Then number 400000 is <= q? If q=400000, then number 400000 is equal to q. However, mex can be q only if numbers 0..q-1 are all present; number q itself does not need to be present for mex to be q. mex is the smallest missing number; if all 0..q-1 are present, mex = q regardless of whether q is present. So we only need to care about numbers in [0, q-1] for mex determination? Actually mex is the smallest non-negative integer not present. So if we have numbers 0..q-1 present, mex = q even if q is also present. But if q is present, mex could still be something else if some smaller number missing. So presence of numbers >= q does not affect mex as long as all numbers < q are present? Wait, if all numbers 0..q-1 are present, mex = q, irrespective of q's presence. If q is missing, mex = q. If q is present, mex is still q? Actually check: if numbers 0,1,2,3 are present (q=4), and also 4 is present, then the set includes 0,1,2,3,4. The smallest missing is 5, so mex =5. So if all numbers 0..q-1 are present and also q is present, mex = q+1? Wait q is number of elements? Not exactly. Let's say we have j elements; the maximum mex possible is j+? Actually if we have j elements, we can cover at most j distinct numbers. So if we cover all numbers from 0 to j-1, then mex = j. If we also cover j, then we have j+1 distinct numbers (since we have at most j elements, we can't cover j+1 distinct numbers if we already cover 0..j-1 and j, that would be j+1 numbers, impossible because we only have j elements. So it's impossible to have all numbers 0..j-1 present and also j present because that would require at least j+1 distinct numbers. Therefore, with j elements, it's impossible to have both 0..j-1 and j present. So at step j, the maximum mex is j (if we cover 0..j-1). So numbers > j are irrelevant for mex, and number j cannot be present if we have covered 0..j-1. So we only need to track numbers up to j for mex at step j. Since j increases, we need to track numbers up to final q. So we can safely ignore numbers > q, because they will never be <= j for any j <= q, and thus never affect mex. However, there is a nuance: suppose we assign number q at some step when j < q. Later j becomes q, and number q might be <= j? At j=q, number q is <= q. But as argued, if we have q elements, it's possible that number q is present? That would mean we have q distinct numbers including possibly numbers > q? But if we have q elements and number q is present, then the set of numbers includes q and some others. Could we still have all numbers 0..q-1 present? That would require q+1 distinct numbers, impossible. So at final step j=q, if mex is q (maximum), then number q cannot be present. But could number q be present and affect mex to be less than q? Possibly, if some smaller number missing, mex would be that missing number. But even if number q is present, it doesn't affect mex unless all numbers smaller than q are present; but then mex would be q (since q missing? Actually if all 0..q-1 present, and q is present, then the set includes 0..q, so smallest missing is q+1, but we have only q elements, impossible to have q+1 distinct numbers. So it's impossible for all 0..q-1 to be present and also q present. So either q is not present, or some smaller number is missing. In either case, the mex is determined by the smallest missing number among 0..q. So we need to know presence of numbers up to q. So we need to mark numbers up to q. So it's safe to allocate array of size q+1.
But careful: q is the total number of queries. However, we might assign a number equal to q (which is <= q). For example, with x=1, after q elements, we might assign numbers 0..q-1, not q. But could we assign q? Possibly if we choose to assign a number > q-1? But greedy assignment always assigns the smallest available number from each residue class. For residue 0 with x=1, we assign 0,1,2,... So after q elements, we assign 0..q-1. So we never assign q. For other x, we might assign numbers > q? For instance, x=3, after many elements, we might assign number 6 when q=5? That's >5. But we said we ignore > q. But what about number exactly equal to q? Could greedy assign a number = q? For example, x=2, q=4. Suppose we have residues: 0,1,0,1. Then assignments: r0:0, r1:1, r0:2, r1:3. So numbers 0,1,2,3. q=4, no number 4. Another: residues: 2,2,2,2 with x=3. Then assignments: r=2:2, next 5, next 8, next 11. None equals 4. So it's unlikely to assign exactly q unless q ≡ r (mod x) for some r and the count for that residue is such that the assigned number is q. Is it possible? Yes, e.g., x=5, q=7. Suppose we get many of residue 2: numbers: 2,7,12,... So 7 could be assigned. That's <= q? q=7, 7 is equal to q. So we might assign number 7 at some step j (maybe j=5). Then later j increases to 7. At j=7, number 7 is present. Is that possible while still having mex <=7? Since we have only 7 elements, if 7 is present, we cannot have all numbers 0..6 present, but we might still have mex smaller. So we need to track presence of number 7. So we need to mark numbers up to q inclusive, because they could affect mex when j is large. However, mex is the smallest missing number; if 7 is present but some smaller number is missing, mex is that smaller number, still we need to know that 7 is present? Not for mex, but we need to know that 7 is present only if we are checking whether mex is 7? Actually to compute mex, we need to find the smallest integer not present. So we need to know presence of all numbers from 0 upwards until we find a missing one. If we know presence for numbers up to q, then the mex will be somewhere between 0 and q+1. Since with q elements, the maximum possible mex is q, mex can't be q+1 because that would require q+1 distinct numbers? Actually if we have q elements and we cover 0..q-1, mex = q. If we also cover q, then we would have q+1 distinct numbers, impossible. So mex can be at most q. So to find mex, we only need to check numbers 0..q. If all 0..q are present, then mex = q+1? But that's impossible as argued. So in practice, mex will be <= q, and we only need to know presence for 0..q. So array of size q+1 is sufficient.
But wait: suppose we have q elements, and we have numbers: 0,1,2,3,5,6,7. Here q=7, mex=4. That's fine. We need to know that 4 is missing. So we need presence for numbers up to at least 4, which is <= q. So array size q+1 works.
Thus, we can allocate an array "present" of size q+1 (or q+2 to be safe) initially false. Also maintain array "next" of size x (0-indexed) with next[r] = r. Also maintain current mex variable = 0.
At each query y, compute r = y % x. Let v = next[r] be the number assigned for this residue. Then if v <= q (since q is total queries, but we are at step j, and q is the total number of queries, which is known in advance? Actually we know q from input, so we can pre-allocate with q+5. However, note: the assigned number v could be > q. If v > q, we can ignore it because it will never affect mex (since mex <= q). But we still need to update next[r] += x for future assignments, because future assignments from the same residue might produce numbers <= q. So we should always update next[r] += x regardless of v. But we only mark present[v] = true if v <= q.
After marking present[v] true (if v <= q), we then need to update mex if v == mex. Since we added a number that could fill the current mex. So while present[mex] is true, mex++.
But caution: what if v is less than mex? That could happen? Since we always assign the smallest unused number from each residue, and we start from r. Over time, the numbers assigned increase. Initially mex=0. If we assign number 0, mex becomes 1. If later we assign number 1, mex becomes 2, etc. However, it's possible that we assign a number that is less than current mex? For example, suppose we have residues: x=3. After some steps, we have assigned numbers: 0,1,2,3 (mex=4). Then we get a new element with residue 0. The next number for residue 0 is 6? Wait residue 0: assigned 0, then 3, then 6,... So after 0 and 3 assigned, next is 6. So it's > mex. So assignments are non-decreasing per residue, but overall they may be out of order? Actually each residue's next number is strictly increasing. So after some steps, the smallest unassigned number overall might be larger than some later assigned numbers? For instance, if we have x=4, and we have residues: 1,2,3. Assign: 1,2,3. mex=0? Wait mex=0 because 0 missing. Then we get residue 0: assign 0. That's less than current mex? At that point current mex? After assigning 1,2,3, mex=0. Then assign 0: v=0, which is less than mex? Actually mex is 0, so 0 is not less than mex (equal). So that's fine. After assigning 0, mex becomes 1. So v can be equal to mex or greater, but not less than mex because if v < mex, that would mean that number was missing earlier, but we already had mex as the smallest missing. If v < mex, that means that number was missing earlier? But if it was missing, then mex would be <= v? Actually mex is the smallest missing number. So if v < mex, then v is missing, so mex would be <= v. Contradiction. So v cannot be less than mex. So v is always >= mex. So we only need to check if v == mex, then while loop.
But is it always true that v >= mex? Consider scenario: current set S has some gaps. The smallest missing is mex. For a number less than mex, it is already present. Our assignment algorithm always picks the smallest number not yet used from that residue class. Since the residue class numbers are arithmetic progression, the numbers assigned from that class are increasing. For a given residue r, the numbers assigned are r, r+x, r+2x,... So these numbers are all >= r. Some of them could be less than mex? Suppose mex=5, and we have residue 2, and we haven't used any element of residue 2 yet. Then next[2] = 2, which is <5. But 2 is less than mex. However, is it possible that we haven't used residue 2 yet, and yet 2 is not present? That would mean that number 2 is missing, so mex would be <=2. So if mex=5, then numbers 0,1,2,3,4 must be present. In particular, number 2 must be present. So if residue 2 hasn't been used yet, how could number 2 be present? It could be present from another residue? No, each number is uniquely determined by its residue. Number 2 has residue 2 mod x, so only elements with residue 2 can produce 2 (or other numbers congruent to 2 mod x). So to have 2 present, we must have assigned an element with residue 2 to 2 (or to 2 + k*x? Actually to get 2, we need an element with residue 2, because numbers with same residue mod x are only from that residue. So if residue 2 hasn't been used yet, 2 cannot be present. Therefore, if mex=5, then 2 cannot be missing, so 2 must be present. But if residue 2 hasn't been used, how can 2 be present? It cannot. So the scenario where mex=5 and residue 2 unused is impossible. Thus, if a residue class r has not been used at all, then number r is missing, so mex <= r. Therefore, mex cannot be greater than the smallest residue class that hasn't been used. More generally, the mex is limited by the smallest number not covered, which is essentially the minimum over residues of the smallest number not yet taken from that residue? Actually the greedy assignment ensures that for each residue r, we have taken the first cnt[r] numbers: r, r+x, ..., r+(cnt[r]-1)x. So the set of numbers taken is exactly these. The mex is the smallest integer not in that set. For each residue r, the numbers taken from that class are an arithmetic progression starting at r with step x. The complement are numbers that are not covered because either that residue class hasn't taken enough numbers to reach that number, or another residue class? Actually each number v belongs to residue v mod x = r. So v is covered if and only if cnt[r] > (v - r)/x and that quotient is integer and >=0. So v is covered if cnt[r] > k where k = (v - r)/x is integer and >=0, i.e., v = r + kx and k < cnt[r]. So for each residue, the set of covered numbers is exactly the first cnt[r] numbers in that progression. Therefore, the overall covered set is union of these. So the mex is the smallest v such that floor((v - r)/x) >= cnt[r] (i.e., not covered) for its residue r = v mod x. That is, for each residue r, the smallest uncovered number is r + cnt[r]*x. So the mex is min over residues r of (r + cnt[r]*x). Because for each residue, the numbers covered are r, r+x, ..., r+(cnt[r]-1)*x; the next number is r+cnt[r]*x, which is the smallest number in that residue not covered. And since every number belongs to some residue, the overall smallest missing number is the minimum of those next numbers across residues. Indeed, consider any number v; let r = v mod x; v is covered if v < r + cnt[r]*x (since it's among the first cnt[r] numbers). So v is uncovered if v >= r + cnt[r]*x. The smallest uncovered number overall is the minimum over residues of r + cnt[r]*x. So the mex equals min_{r=0..x-1} (r + cnt[r]*x). This is a key insight.
Check with examples: x=3, cnt[0]=1 (have number 0), cnt[1]=1 (have number 1), cnt[2]=1 (have number 2). Then r+cnt[r]x: 0+13=3, 1+13=4, 2+13=5. min=3 -> mex=3, correct. If cnt[0]=2 (numbers 0 and 3), cnt[1]=1 (1), cnt[2]=1 (2). Then values: 0+2*3=6, 1+3=4, 2+3=5 -> min=4 -> mex=4, correct. That matches examples: after 5th query in example 1, they had mex=4.
Thus, the MEX after each query can be computed simply as min_{r} (r + cnt[r] * x). Where cnt[r] is the number of elements with residue r.
But careful: Is this formula always valid? Let's test with a scenario: x=4, cnt[0]=0, cnt[1]=1 (number 1), cnt[2]=0, cnt[3]=0. Then r+cnt[r]x: 0+0=0, 1+14=5, 2+0=2, 3+0=3. min = 0 -> mex=0? But is mex 0? Our set: only number 1 is present. The smallest missing is 0, yes mex=0. So formula works. Another: cnt[0]=1 (0), cnt[1]=0 -> values: 0+4=4? Wait cnt[0]=1 => 0+1*4=4? Actually r + cnt[r]x: 0+14=4, 1+0=1 -> min=1 -> mex=1. Set: {0}. Smallest missing is 1. Works.
So indeed, the mex is simply min_{r} (r + cnt[r] * x). That's a much simpler characterization: Because for each residue, we are effectively building the numbers in that class from the smallest upward. The mex is the smallest number not yet built, which is the smallest over all residues of the next number that would be built.
Thus, we don't need to simulate assignments to numbers or maintain a present array. We just need to maintain cnt[r] for each residue, and compute the minimum of r + cnt[r]*x.
After each query, we increment cnt[y % x] by 1, and then compute the min. However, naive min over x residues each query would be O(x) per query, which is O(q*x) up to 1.6e11, too high. Need faster.
We need to support x up to 4e5, q up to 4e5, so total operations up to ~8e5, we can afford O(log x) per query or amortized O(1). Since we are only incrementing one cnt per query, we need to update the minimum efficiently.
Observation: The values r + cnt[r]x are like for each residue r, we have a value that increases by x each time we add an element with that residue. Initially cnt[r]=0, so initial value = r (since r + 0x = r). So initially the minimum mex is min_{r} r = 0 (since r=0 is present). Actually r ranges 0..x-1, so min initial is 0. Then as we increment cnt[r], that residue's value increases by x. So we need to maintain the minimum of these x values, where each update increments one value by x.
We can maintain a data structure that supports: increase a value by x, and query global minimum. Since x is constant per residue, and we have up to 4e5 residues, we can use a segment tree or a Fenwick tree? Actually we can maintain a min-heap (priority queue) of the current values (r + cnt[r]*x). When we increment cnt[r], the new value becomes old value + x. But updating a value in a heap is not straightforward (we could push a new value and keep track of current values). Since each increment only adds x, we can push the new value into the heap, and when we pop we need to ensure it's the current value. Approach: For each residue, we maintain its current value. When we increment cnt[r], we compute its new value and push that new value into a min-heap. Then when we query the minimum, we pop from the heap any entries that do not correspond to the current value for that residue (i.e., outdated values). Then the top of the heap will be the current minimum. This is typical lazy deletion. Since each query adds one element, we will push one new value per query. So total pushes = q. Each pop may discard outdated entries; each outdated entry is popped at most once. So total heap operations O((q + outdated) log x). Since outdated entries are at most the number of pushes, total O(q log q) fine.
But we need to know the current value for each residue to detect outdated entries. We'll maintain an array val[r] = r + cnt[r]*x. Initially val[r] = r. We'll push all initial values into heap? Or we can push them lazily. Since x up to 4e5, we can push all initial x values into heap (size 4e5). Then for each query, we update val[r] += x, and push the new val[r] into heap. Then to get min, we peek at heap top; if the top's value != val[residue] (we need to know which residue it corresponds to), we pop it and continue. We can store pairs (value, residue) in the heap. So we can check if heap.top().value == val[heap.top().residue]; if not, pop.
We also need to consider that the mex is the minimum of these values. Indeed, mex = min_r val[r].
Let's test with example: x=3, initial val=[0,1,2], min=0 -> mex=0? But after 0 queries? Actually before any queries, the array is empty, but we consider after each query. Our cnt are initially all 0. After first query y=0 (r=0), we increment cnt[0] from 0 to 1, so val[0] becomes 0+1*3=3. Now values: val[0]=3, val[1]=1, val[2]=2. min=1 -> mex=1. Correct. After second y=1, cnt[1]=1, val[1]=1+3=4. Now values: 3,4,2 -> min=2 -> mex=2. After third y=2, val[2]=2+3=5, values:3,4,5 -> min=3 -> mex=3. After fourth y=2, cnt[2] becomes 2, val[2]=5+3=8? Wait previous val[2] after third was 5, increment by x gives 8. Now values:3,4,8 -> min=3 -> mex=3. After fifth y=0, cnt[0]=2, val[0]=3+3=6, values:6,4,8 -> min=4 -> mex=4. After sixth y=0, cnt[0]=3, val[0]=6+3=9, values:9,4,8 -> min=4 -> mex=4. After seventh y=10 (10%3=1), cnt[1]=2, val[1]=4+3=7, values:9,7,8 -> min=7? Actually min is 7? But expected mex output is 7. Yes, min=7. So matches.
Second example: x=3, queries: 1,2,1,2. Initial vals: [0,1,2]. After 1: y=1 -> r=1, val[1]=1+3=4 -> vals:0,4,2 -> min=0 -> mex=0? But expected output first line: 0. Wait after first query output is 0, yes. After second: y=2 -> r=2, val[2]=2+3=5 -> vals:0,4,5 -> min=0 -> mex=0. After third: y=1 -> r=1, val[1]=4+3=7 -> vals:0,7,5 -> min=0 -> mex=0. After fourth: y=2 -> r=2, val[2]=5+3=8 -> vals:0,7,8 -> min=0 -> mex=0. Output: 0,0,0,0. Good.
Thus the solution: maintain cnt array of size x (initial zeros), and val array = r + cnt[r]*x, but we can compute on the fly: val[r] = r + cnt[r]*x. Use a min-heap of (val[r], r). Initially, we can push (r, r) for each r in 0..x-1 (since cnt=0). Then for each query y: r = y % x; cnt[r]++; val[r] += x; push (val[r], r) into heap. Then we need to get current min: while heap.top().first != val[heap.top().second] (i.e., outdated), pop. Then output heap.top().first (which is the minimum val). That's mex.
Complexities: O((x + q) log x) ~ 8e5 log 4e5, fine.
Edge Cases: x can be up to 4e5, so initial heap with x entries is fine. q up to 4e5, pushes q entries, total heap size x+q ~ 8e5, fine.
One nuance: The mex computed as min_r (r + cnt[r]*x) can be larger than the number of elements? But as argued, mex <= total elements (q). Indeed, the formula yields a value that might exceed q? Let's check: after j queries, sum cnt[r] = j. The minimum of r + cnt[r]*x. Since each cnt[r] >=0, the minimum is at most something? For example, x=1, then val[r] (only r=0) = 0 + cnt[0]*1 = cnt[0] = j. So mex = j, which is <= j. For x >1, r + cnt[r]x can be greater than j? Possibly: if counts are skewed, e.g., x=100, j=5, all elements residue 99: cnt[99]=5, val[99] = 99 + 5100 = 599, which is >5. But does that mean mex=599? That seems too high relative to number of elements (5). But is it possible to have mex=599 with only 5 elements? Let's test: with residues all 99, we can generate numbers: 99,199,299,399,499. The set S = {99,199,299,399,499}. The smallest non-negative integer not in S is 0. So mex should be 0. Wait our formula says min over r: for r=0..99. Since cnt[0]=0, val[0]=0. So min is 0. So mex=0. So the minimum is taken over all residues, including those with cnt=0. So the min will be the smallest r for which cnt[r]=0? Actually if any residue has cnt=0, then val[r] = r, which could be small. For residues with cnt>0, val[r] >= r + x, which is at least x (if r=0, then x; if r>0, > r). So the overall minimum will be the minimum over all residues of r if any cnt[r]=0. The only way mex becomes large is if for every residue r, cnt[r] > 0, i.e., we have at least one element in each residue class. Then the minimum is over r + cnt[r]*x, which could be as high