We need to decrypt the Caesar cipher given an encrypted string S. We need to choose the shift n (0 to 25) such that when we decrypt (i.e., shift back by n) the original message is lexicographically minimum among all possible shifts. Then output that original message.
But careful: They say: "Uttu is sure that his beloved will send him a message which would be lexicographically minimum, when decrypted." That means: Among all possible original messages (i.e., all possible shifts that produce a valid original message), the original message that Shil actually sent is the lexicographically minimum one. So given the encrypted message, we need to find the shift n such that when we apply the decryption (i.e., shift each character back by n) we get the lexicographically smallest possible string among all shifts. Then output that string.
Note: Encryption is En(x) = (x + n) mod 26. So if original message is P, then encrypted is C_i = (P_i + n) mod 26. So given C, we can consider each possible n and compute P = (C - n) mod 26. Among those 26 strings, we need the lexicographically smallest one. That's it.
But there might be a nuance: Since the original message is lexicographically minimum, we assume that among all possible original messages, the one that Shil intended is the lexicographically smallest. So we choose the shift that yields the smallest decrypted string. If there are ties (multiple shifts produce same lexicographically smallest string), then any works? Actually if there are ties, the original message would be the same for those shifts? Wait, could different shifts produce the same decrypted string? Only if the encrypted string is such that subtracting different n gives same string? That would mean for all i, (C_i - n1) mod 26 = (C_i - n2) mod 26 => (n1 - n2) mod 26 = 0 => n1=n2. So distinct shifts produce distinct strings. So there is a unique decrypted string for each shift. So lexicographically minimum will be unique? Possibly not unique if two different strings are equal? They can't be equal because they come from different n, but could two different strings be equal lexicographically? Lexicographic order is total, so there will be a minimum string. It could be that two strings are identical? As argued, they can't be identical unless n1=n2. So unique.
Thus the problem reduces: For each S of length L, compute for each shift k from 0 to 25, the string T_k where T_k[i] = ((S[i] - 'a' - k) mod 26) + 'a'. Find k such that T_k is lexicographically minimum. Output T_k.
Constraints: T up to 1e6 messages, total chars up to 1e6. So overall input size is up to 1e6 characters, and we need to process efficiently. For each message, we might need to check all 26 shifts? But if message length can be up to 1e6, but total chars across all messages is 1e6, so worst-case one message length 1e6. Checking 26 shifts would mean generating all 26 strings of length up to 1e6, which is 26 million characters, which might be okay? 26e6 is about 26 MB, and we need to compare lexicographically. But we can determine the minimal shift without fully constructing all strings by comparing them in a smarter way. However, given total characters is 1e6, we could afford to generate 26 shifted versions? That would be 26 * 1e6 = 26e6 operations, which is fine in C++ (26 million). But we also need to handle T up to 1e6 messages, but total chars is 1e6, so many messages will be short, maybe even empty? Strings can be empty? |S| ≤ 1e6, could be zero? Usually non-empty. But if there are many very short messages, like length 1 each, then generating 26 shifted versions per message might be 26 * 1e6 = 26e6 operations again okay. So overall O(26 * total length) is okay. But we need to implement careful to avoid overhead for many small strings.
Alternatively, we can compute the minimal shift by comparing two shifts using the first character where they differ. Since only 26 shifts, we could compute the shifted strings on the fly and compare lexicographically.
Simplest approach: For each message, for each shift 0..25, compute the decrypted string (in a vector of chars) and keep track of the smallest lexicographically. But that would require storing all 26 strings or recomputing comparisons. Better: Use a custom comparison that builds the shifted string for each candidate while comparing.
But maybe there's a direct way to compute the minimal shift: The lexicographically smallest string is the one that minimizes the first character, and if ties, second, etc. So we need to find shift k such that the string (c_i - k mod 26) is minimal. This is essentially finding the shift that produces the smallest sequence.
We could think of the shift k as subtracting k modulo 26. The first character after shift: (c0 - k) mod 26. To minimize the string, we want the first character as small as possible (i.e., 'a'=0). So we would like to choose k such that (c0 - k) mod 26 = 0, i.e., k ≡ c0 (mod 26). So if we choose k = c0, then first character becomes 'a'. That yields the smallest possible first character. However, there could be multiple k that also give 'a'? Only k = c0 mod 26 gives that. So that would be the unique minimal first character. But wait, what if c0 is such that we can't achieve 'a'? Actually we can always achieve 'a' by setting k = c0. So the shift k = c0 gives first char 'a', which is the absolute smallest possible. So that seems to guarantee that the lexicographically smallest string among all shifts will have the first character 'a', because we can always achieve that. But could there be a case where two shifts both give 'a'? No. So the lexicographically smallest string must start with 'a'. Therefore the shift is uniquely determined: n = (S[0] - 'a')? Actually shift n is the encryption shift, i.e., we need to apply decryption shift? Wait, we are considering decryption shift k, where we subtract k from each encrypted char to get original. For the first character to be 'a', we need (S[0] - 'a' - k) mod 26 = 0 => k ≡ S[0] - 'a' (mod 26). So k = (S[0] - 'a'). So the decryption shift must equal the index of the first encrypted character. That yields original first character 'a'. Since 'a' is smallest possible, any other shift would give first character > 'a', so the lexicographically smallest string must be the one with k = S[0] - 'a'. So answer is simply shift by that amount? Let's test with sample: S = "phqghumeay". First character 'p' has index 15? Actually a=0, p=15? a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p => 15. So k=15. Decrypt: subtract 15 from each. p-15 = a (0). h-15? h=7, 7-15 = -8 mod 26 = 18 -> s? Actually 26-8=18 => s. So p->a, h->s? That yields "as..." The sample output is "asbrsfxplj". Let's compute: p h q g h u m e a y. Indices: 15,7,16,6,7,20,12,4,0,24. Subtract 15: (15-15=0=a), (7-15=-8 mod26=18=s), (16-15=1=b), (6-15=-9=17=r), (7-15=-8=18=s), (20-15=5=f), (12-15=-3=23=x), (4-15=-11=15=p), (0-15=-15=11=l), (24-15=9=j). So we get "asbrsfxplj", which matches sample. So indeed the rule works.
But is this always correct? Let's check edge cases. Suppose the first character is 'a' itself. Then k=0 gives first char 'a'. But also if k=26? Only 0-25. So only k=0 gives 'a'. So answer is k=0, i.e., decrypted string = original encrypted string (since no shift). That seems plausible: if first char is 'a', then the lexicographically smallest possible among all shifts? Could there be a shift that also gives 'a' as first character? No, only k=0. So the minimal string is the one with k=0. But is it indeed lexicographically smallest? Consider S = "ab". k=0 gives "ab". k=1 gives "za" (since a-1=z, b-1=a). Compare "ab" vs "za": "ab" is smaller because 'a' < 'z'. So yes. Consider S = "ba". k=1 (since first char 'b'=1) gives: b-1=a, a-1=z => "az". k=0 gives "ba". Compare "az" vs "ba": 'a' vs 'b', so "az" smaller. So k=1 yields first char 'a', which is minimal. So rule holds.
But is there any scenario where the minimal shift does not equal first character index? Let's think: The lexicographic comparison first compares first character. The smallest possible first character is 'a' (0). Is it always achievable? Yes, by k = S[0] - 'a'. That gives first character 'a'. So any other shift will give a first character with value >0. So the string with first character 'a' is strictly smaller than any string with first character > 'a'. Therefore the minimal string must be the one with first character 'a'. So shift is uniquely determined as above. Thus we don't need to compare further.
Therefore the answer is simply: For each character c in S, output ((c - 'a' - (S[0] - 'a')) mod 26) + 'a'. In other words, shift by subtracting the offset of first character.
But wait, we need to ensure that the decrypted message is lexicographically minimum among all possible original messages. But we assumed that the original message intended is lexicographically minimum. So we need to output that message. So indeed, the answer is as above. So the problem is trivial: just compute shift based on first character.
But is there any trick? Could there be a scenario where the first character is 'a' and k=0 gives first char 'a', but some other shift also yields first char 'a'? For that to happen, (S[0] - k) mod 26 = 0 => k = S[0] mod 26. Since S[0] is 0 for 'a', only k=0 works. So unique.
Thus the solution is O(N) per test case, simple.
But wait, the problem statement: "Uttu is sure that his beloved will send him a message which would be lexicographically minimum, when decrypted." This implies that among all possible original messages (i.e., all shifts), the one sent is the lexicographically minimum. So we need to find that one. So as argued, it's uniquely determined by the first character. So answer is straightforward.
But I must double-check if there is any nuance: The encryption is En(x) = (x + n) mod 26. So given ciphertext C, original P = (C - n) mod 26. For each possible n (0 to 25), we get a possible original. Among these, the lexicographically smallest is the one we assume was sent. So indeed the minimal original is the one with minimal first character, which is always 'a' achievable. So the shift n must be such that (C[0] - n) mod 26 = 0 => n = C[0] (mod 26). So n = C[0] - 'a'. Thus the decrypted message is obtained by subtracting n from all characters. So answer is simple.
Thus the solution: For each test case string s, compute d = s[0] - 'a'. Then output string t where each char c -> ((c - 'a' - d + 26) % 26) + 'a'.
But careful: s may be empty? Usually strings have at least one character? In constraints |S| ≤ 1e6, but could be zero? Possibly, but then there is no first character. If empty, then any shift yields empty string, which is lexicographically minimum (empty). So for empty string, answer is empty. But are there empty lines? Input likely contains non-empty strings, but we can handle empty gracefully. However given that characters are only lowercase, and T up to 1e6, maybe there can be empty strings? The problem statement says "string S", probably non-empty. But to be safe, we could treat empty as just output empty.
Now complexity: total characters ≤ 1e6, so O(total characters) is fine.
Implementation details: We need to read T up to 1e6 lines. That's many lines. Use fast I/O in C++. Use scanf or custom fast input. Because T can be large, but total characters is 1e6, so average length 1. So there will be many short strings. So reading line by line with getline might be slower. Use scanf with %s for each test case? But scanf will skip whitespace and read a word. Since strings consist of lowercase letters only, and there are no spaces, that's fine. Input sample shows "phqghumeay" on its own line. So we can read using scanf.
Alternatively, use getchar/unlocked. But with total 1e6 chars, it's fine.
We need to output each decrypted string.
We'll compute d = s[0] - 'a' if s not empty. Then iterate through s and compute transformed char.
We can do in-place if we read into a buffer and then modify. But careful: we need to output transformed, not modify original? We can compute and output char by char.
Since total output size also ≤ 1e6, it's fine.
Edge Cases:
- When d=0, we output original string (since subtracting 0). So no change.
- When d>0, we need to subtract d mod 26.
Implementation:
for each test case:
read string s (via scanf("%s", buf) or using C++ cin with sync off)
if s empty? If using scanf, it will read non-empty strings? If string could be empty, scanf won't read empty. So ignore.
int d = s[0] - 'a';
for each char c in s:
char out = ((c - 'a' - d + 26) % 26) + 'a';
putchar(out);
putchar('\n');
But we need to handle large number of outputs efficiently, maybe use puts or write in one go. We can transform in place and then printf("%s\n", buf). However, the transformation modifies the buffer, and we can then output. That avoids per-character putchar. But need to ensure buffer is writable and we compute each char. That's also fine.
We need to allocate buffer of size maybe 1e6+2? Since total characters per string could be up to 1e6, but total across test cases is 1e6, so we can allocate a buffer of size 1e6+2 and reuse it? But careful: we read each string into buffer. However, if we reuse the same buffer, it's fine as long as we add null terminator. So we can do:
char buf[1000002]; // but maximum per string may be 1e6, plus null.
But total input size is 1e6, but an individual string could be 1e6. So buffer size 1000002 is okay. Use scanf("%s", buf). Then compute d = buf[0] - 'a'; then for each i until buf[i]!='\0', transform: buf[i] = ((buf[i]-'a' - d + 26) % 26) + 'a'; Then printf("%s\n", buf).
This is efficient.
But we need to be careful about modulus operation cost for each char. Could precompute shifted alphabet? For given d, we could compute an array map[26] that maps original index to shifted index: map[i] = (i - d + 26) % 26; But we still need to compute for each char: buf[i] = map[buf[i]-'a'] + 'a'; That's similar cost.
Given total 1e6 chars, either is fine.
But note: we have many test cases, and for each we do O(L). So overall O(total L).
Now confirm: The problem sample is consistent. So solution seems trivial.
But is there any catch? Could the lexicographically minimum original message not be the one with first character 'a'? Wait, lexicographic order: first compare first character. 'a' is the smallest possible letter. Since we can always get first character 'a' by choosing n = C[0] - 'a', indeed the string with first character 'a' is strictly smaller than any string with first character > 'a'. So it's the minimum. So no other shift can beat that. Thus answer is unique.
So the problem reduces to applying a Caesar cipher with shift equal to the value of the first letter.
Thus solution is straightforward.
Now we should write solution accordingly.
But the problem statement mentions T up to 10^6 and total characters 10^6. So reading using fast I/O is needed. We'll use scanf.
Now check for memory: T up to 1e6 but we only process one string at a time.
Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
scanf("%d", &T);
char s[1000002]; // maybe allocate dynamic? but static large enough for max string size (10^6+1) is fine if global. However 1e6+2 on stack might cause stack overflow? Usually stack size is limited (few MB). 1e6 bytes is 1 MB, plus some overhead, may be okay but safe to allocate static global or use vector. Better to allocate with new or vector. But we can use vector s(1000002); Or simply use string from C++. Using std::string might be slower but still okay for 1e6 total. However reading with cin >> string for many small strings might be okay with sync off. But we can use scanf with char buffer.
But to avoid stack overflow, we can allocate the buffer dynamically on heap or as static global. Let's declare a static char buffer[1000002] outside main.
Alternatively, we can use std::string and do: string s; cin >> s; Then transform. But with T up to 1e6 and many small strings, cin may be slower than scanf, but with sync off it might be okay. However total characters 1e6, so overhead per string might dominate if we allocate many strings. Using scanf with a fixed buffer may be more efficient. But we can also use std::string and reserve.
Given constraints, it's fine either way.
I'll use scanf with buffer.
Pseudo:
static char buf[1000002];
scanf("%d", &T);
while (T--) {
scanf("%s", buf);
int d = buf[0] - 'a';
for (char *p = buf; *p; ++p) {
*p = ((*p - 'a' - d + 26) % 26) + 'a';
}
puts(buf);
}
But careful: If the string is empty (possible if we read empty line?), scanf will skip whitespace and not read an empty string. So we won't have empty case. Good.
But note: The total length of all strings ≤ 1e6, but each individual string could be up to 1e6. Our buffer size should be at least 1e6+1. That's okay.
Now potential pitfalls: Negative modulo in C++: % operator yields remainder, can be negative. So to get positive modulo we need to add 26 before mod, or adjust.
We can compute: int x = (c - 'a' - d) % 26; if (x < 0) x += 26; then char = x + 'a'. That may be slightly faster.
Or we can precompute a table for each d? But we don't need.
Now we must ensure that for each test case we don't exceed buffer length. Since we know total characters, but not per string, but we trust input fits.
Now check sample.
Works.
But is there any scenario where the shift we compute is not correct because the lexicographically minimum string might not have first character 'a'? Consider the possibility of an empty string? No.
But could there be multiple strings with first character 'a'? As argued, only one shift yields first char 'a' because the shift must satisfy (c0 - n) mod 26 = 0, giving n = c0 mod 26 uniquely. So exactly one shift gives first char 'a'. So that shift yields the minimum.
Thus solution stands.
Now think about the problem's intention: It may be a trick to realize that the shift is determined by first character. Many Caesar cipher problems require brute force, but here constraints are high so they expect this insight.
Thus answer is simply applying shift equal to first letter.
Now write final solution.
But note: There is a nuance: "they use the famous Caesar cipher to encrypt their messages, which mathematically explained is as follows: Encryption of a letter x by a shift n can be described mathematically as, En(x) = (x + n) mod 26." So encryption adds n. Then they say: "In sent message, the original letter will be replaced by encrypted letter." So ciphertext = original + n mod 26. Then Uttu wants to decrypt by choosing appropriate shift n that gives lexicographically minimum original. So as we did.
Yes.
Thus final solution: For each string, let shift = first character's index. Then output the string shifted back by that amount.
Implementation details:
- Use fast I/O: ios_base::sync_with_stdio(false); cin.tie(NULL); may be used if using cin/cout. But scanf/printf might be simpler.
But T up to 1e6, so printing with printf for each line is okay. But we need to output many lines, and using puts might be faster.
Let's use C style.
One more thing: The total number of characters in a test file ≤ 10^6, but T can be up to 10^6, so many strings could be empty? Actually if T=10^6, each string must be length at most 1, because total characters ≤ 1e6. So many strings of length 0 or 1. But reading using scanf("%s") will skip empty lines and read a string? It will read a sequence of non-whitespace characters. If there is an empty line, scanf will skip it? Since the input format: after T, next T lines contain a string S. If a line is empty, that would be a string of length 0. But scanf("%s") cannot read an empty string; it will skip whitespace and read the next non-whitespace token. So if the line is empty, it will actually read the next non-whitespace token from subsequent lines, messing up. So the input likely does not contain empty strings; each line will have at least one lowercase character. So it's safe.
Better to be robust: use fgets or getline, but then need to handle newline. But since total characters small, and T large, maybe there are lines with just newline? Possibly not.
Thus go ahead.
Now we can write final code.
But before finalizing, let's test a few cases.
Case 1: S = "a". Then shift = 0, output "a". Minimum among all shifts: shift 0 -> "a", shift 1 -> "z". "a" is smaller.
Case 2: S = "z". shift = 25. output: z-25 = 'a'. So output "a". Check other shifts: shift 0 gives "z", shift 1 gives "y", etc. "a" is smallest indeed.
Case 3: S = "ab". shift = 0 -> "ab". But first char is 'a', so shift = 0. Output "ab". That is lexicographically smallest? Check shift 1 -> "za". "ab" < "za". shift 2 -> "yz"? Actually compute: a->y? a-2=24=y, b-2=z? Wait: (0-2 mod26=24=y), (1-2=25=z) => "yz". "ab" < "yz". So indeed "ab" is smallest.
Case 4: S = "ba". shift = 1 -> "az". Output "az". Check shift 0 -> "ba". "az" < "ba". shift 2 -> "zy"? b-2=0? b=1,1-2=-1=25=z? Actually b-2=-1 mod26=25 -> z; a-2=-1? a=0,0-2=-2=24-> y? Wait: a-2=-2+26=24 -> y? Let's compute: b(1)-2=-1=25->z; a(0)-2=-2=24->y => "zy". "az" < "zy". So correct.
Thus all good.
Thus solution is to output the string where we subtract the value of the first character from each character.
Now implement.
But we must be careful with modulus. For each character c, we do: int x = (c - 'a' - d) % 26; if (x < 0) x += 26; then c = 'a' + x.
We can precompute for each d (0..25) a translation table: for each original index i (0..25), shifted index = (i - d + 26) % 26. But d varies per test case, so we'd need to recompute table each time. Since total characters is 1e6, computing per character is fine.
However, to avoid modulo operation for each char, we can use arithmetic: x = c - 'a' - d; if (x < 0) x += 26; else if (x >= 26) x -= 26; but since c - 'a' in [0,25] and d in [0,25], x is in [-25,25], so only need to add 26 if negative. So: int x = c - 'a' - d; if (x < 0) x += 26; char = 'a' + x; This is a bit faster.
Thus code:
for (int i = 0; buf[i]; i++) {
int x = buf[i] - 'a' - d;
if (x < 0) x += 26;
buf[i] = 'a' + x;
}
Now one nuance: When the string length is large (1e6), this loop is O(L). Good.
Now handle large T: we need to read T. Use scanf("%d", &T). Then while(T--) loop.
Potential issues: Input reading might be large, but total characters 1e6. So okay.
Now implement.
Testing with sample.
Now consider potential overflow? No.
Thus final answer.
But we must ensure to handle the case when the string is of length 0? Not needed.
Now produce final code.
But before final, let's double-check the problem statement: "Help Uttu to decrypt the message by choosing appropriate shift n, that will give lexicographically minimum original message, among all possible messages. Your task is to find the decrypted message for Uttu." Indeed.
Thus answer: shift = S[0] - 'a', then subtract that from each character.
Now writing solution in C++.
But I'll also write explanation in the answer.
Now final. ```cpp
#include
const int MAXL = 1000002; // max length per string + 2
char s[MAXL];
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%s", s);
int d = s[0] - 'a'; // the unique shift that makes first character 'a'
for (int i = 0; s[i]; ++i) {
int x = s[i] - 'a' - d;
if (x < 0) x += 26;
s[i] = 'a' + x;
}
puts(s);
}
return 0;
}