We need to parse problem: For each query (l, r), we consider the substring t = s[l..r]. Need to determine if there exists a good subsequence of the whole string s that equals t. Good subsequence means: not contiguous (i.e., the chosen indices are not all consecutive? Actually definition: "A subsequence is said to be good if it is not contiguous and has length ≥ 2." So it means that the subsequence is not a contiguous substring? Usually "contiguous" means that the indices of the chosen characters are consecutive integers. So good subsequence must have at least one gap: there exists at least one index that is skipped between two consecutive selected indices. And length at least 2.
We need to check if there exists some subsequence of s (possibly using indices outside [l, r] as well) that forms exactly the same string t, but the subsequence is good (i.e., non-contiguous). And importantly, the subsequence is not necessarily the same as the substring itself. The substring s[l..r] is a contiguous subsequence, but it might be good? But it is contiguous, so it is not good. So we cannot just use that substring. Need some other selection of indices that yields same characters as t, but with a gap.
So essentially: Given t of length L = r-l+1 (>=2 because l<r). Does there exist indices i1 < i2 < ... < iL such that s[i_k] = t[k] for each k, and the indices are not all consecutive (i.e., there exists at least one k such that i_{k+1} > i_k + 1). That is the condition for "not contiguous". Also length >=2 is already satisfied.
We want existence. Constraints: n <= 100, q <= 100, t <= 100 test cases. So brute force per query is possible: For each query, we can try to find a subsequence equal to t that is non-contiguous. However naive approach may be exponential. But with n small, we can use DP or greedy? Since we need to check existence, maybe simpler characterization.
Observation: The substring t itself is contiguous from l to r. If we can find any other occurrence of t as a subsequence (not necessarily substring) that is not contiguous, that would work. But also we could potentially use some characters from within the substring? Wait the subsequence indices must be in increasing order. It could include characters from within the substring region as well as outside, as long as the resulting string equals t.
We need to check if there exists a subsequence equal to t that is not contiguous.
Let's think: The easiest way to get a non-contiguous subsequence equal to t is to find at least one character in t that can be matched by a character in s that is not from the natural contiguous positions but somewhere else, while preserving order.
Another perspective: The contiguous substring s[l..r] gives one specific subsequence (contiguous). We need to see if we can "break" the contiguity while still matching t. That is, can we match t using some indices where at least one jump skips an index? That is, there exists an index such that we do not pick all characters consecutively.
Simplify: Since t is taken from s, we can consider matching t as a subsequence using the original positions of characters. The trivial contiguous match uses indices l, l+1, ..., r. To be non-contiguous, we need a different set of indices, which must differ from the contiguous one in at least one position: either we replace some character(s) with same character from other positions (earlier or later), or we skip some character? But we need exact t; we must pick exactly L characters. If we skip a character in the original substring, we would be missing a character from t unless we pick it from elsewhere. Alternatively, we could pick some characters from outside and some from inside, but the order must match t.
Key: Since s itself contains t as a substring, we have at least one occurrence. But we need a non-contiguous occurrence.
Let L>=2.
We need to check if there exists a subsequence equal to t that is not exactly the indices [l, r]. That is, there exists indices i1<...<iL such that s[i_k]=t[k] and the indices are not all consecutive.
If the string s contains t as a subsequence in a non-contiguous way, then answer YES.
We need to answer per query.
Given small constraints, we could for each query, try to find any subsequence that matches t and is not contiguous. But perhaps we can derive simple conditions.
Let's consider t = s[l..r]. If we can find at least one character in t (say at position j in t) that appears elsewhere in s (before l? after r?) such that we can still match the rest in order, then we can construct a non-contiguous subsequence.
Alternatively, maybe we can always do something if the first character of t appears somewhere before l (or the last character appears somewhere after r), because then we can replace that occurrence and still match the rest using contiguous positions for the rest? But careful: if we replace the first character with an occurrence earlier, we need to ensure we can still match the rest from some indices after that earlier occurrence and before the next needed characters. But we could use the contiguous positions for the rest except that we skip the original first character. However, the contiguous positions start at l. If we pick first character from an earlier index p < l, then we need to match the second character (t[2]) at some index > p. Could we use l (original first character) for the second character? But t[2] equals s[l+1]? Wait t[2] is s[l+1] (since t = s[l..r]). If we pick first character from p < l, we could then pick second character from l+1? That is allowed if l+1 > p. Then we need third character from l+2, etc. But note: The second character we would be using s[l+1] which is originally the second character of t. However, after picking first from p, we still need to pick L-1 characters to form t. The natural contiguous indices from l+1 to r would provide exactly t[2..L] in order, but we must ensure that we can pick them after p. Since p < l, and l+1 > l > p, so we can indeed pick s[l+1], s[l+2], ..., s[r] in order. So the subsequence would be: p, l+1, l+2, ..., r. But wait: This yields the string: s[p] (which equals s[l] because t[1]=s[l])? Not necessarily: We need s[p] to equal t[1] = s[l]. So we need an occurrence of the character s[l] at some position p < l. Similarly, we can check if s[l] appears anywhere before l. If yes, then the subsequence p, l+1,...,r is valid? Check length: L (p, then L-1 indices from l+1 to r) gives total L characters. The string: s[p] (should equal s[l]), then s[l+1]...s[r] gives t[2..L] as needed. So it forms t. Is this subsequence good? It is not contiguous if there is a gap between p and l+1? Actually the indices are p, l+1, l+2, ..., r. Are these consecutive? The gap from p to l+1: p < l, so l+1 >= l+1, difference at least 2? Actually could be that p = l-1? Then indices would be l-1, l+1, l+2,..., r. That is not contiguous because we have a jump: from l-1 to l+1 skips l. So there is at least one skipped index (l). So it's non-contiguous. If p is exactly l-1, then we have a gap of 2 (l is missing). So definitely not contiguous. Even if p is any earlier, there will be a gap. So it's good.
But what about the case where p is l-1 and then l+1, l+2,... r: Is that allowed? Yes. So condition: if s[l] occurs somewhere before l (i.e., an index p < l with s[p] = s[l]), then answer YES.
Similarly, could we use last character? If s[r] occurs somewhere after r (index q > r with s[q] = s[r]), then we could take the first L-1 characters from l to r-1 contiguously, and last character from q > r. The subsequence: l, l+1, ..., r-1, q. This is also non-contiguous because there is a gap between r-1 and q (skipping r at least). Check: Is it necessarily non-contiguous? The indices are consecutive from l to r-1, then jump to q > r. Since q > r and r-1 is immediate predecessor, the difference between r-1 and q is at least 2 (since q >= r+1). So there is a gap (r is missing). So it's good. So if s[r] appears after r, answer YES.
But are there other possibilities? What if s[l] appears after l but before r? That would be inside the substring. Could we use that to create a non-contiguous subsequence? Possibly we could pick some characters from inside but skip some others. For instance, suppose we have t = "010" and s = "001000". They gave example where s[2..4]="010" and they found good subsequence using s1, s3, s5 = "010"? Actually s1='0', s3='1', s5='0' gives "010". That is using s1 (position 1) which is before l=2 for the first character (0), s3 (position 3) for second (1) which is the same as original second? Actually original substring indices: 2:'0',3:'1',4:'0'. In their subsequence, they used indices 1,3,5. This is using first character from before l, second from within (position 3 is original second? original second of substring is index 3? Actually substring 2-4: s2='0', s3='1', s4='0'. So second character (t[2]) is '1' at index 3. They used index 3 for that. Third character '0' from index 5 (after r). So that's combining both sides. It is covered by either condition? We had condition for first character before l (index1) gave YES. So condition is satisfied.
But what about cases where first character does not appear before l and last character does not appear after r? Could there still be a good subsequence? Possibly if some intermediate character can be taken from outside while using the rest contiguously? For example, consider s = "010", n=3, query l=1,r=3 (t="010"). Here first character '0' appears at positions 1 and 3. s[1] is '0', but that's the first character itself; we need before l? Actually l=1, so there is no index <1. So no earlier occurrence. Last character '0' appears at position 3, but that's r itself; we need after r? r=3, n=3, no later occurrence. However, could there be a good subsequence? Let's test: t="010". Can we find a non-contiguous subsequence equal to "010"? The whole string is "010". Possible subsequences: "01" (indices 1,2) length 2 but not contiguous? Actually indices 1,2 are contiguous, so not good because good requires not contiguous? Actually length>=2 and not contiguous. "01" from indices 1,2 is contiguous, so not good. "00" from indices 1,3: that's "00", not t. "10" from indices 2,3: contiguous. "010" from indices 1,2,3: contiguous. So no good subsequence equal to "010". So answer would be NO. So conditions seem to hold.
What about a case where first and last characters are not found outside but some middle character appears outside? e.g., s = "10101", n=5, query l=2,r=4 => t="010". Check: s[2..4]="010". First character '0' appears at positions? s[2] is '0', s[4] is '0'. Earlier than l=2? position 1 is '1', so no earlier '0'. Last character '0' at r=4, later after r? position5 is '1', so no later '0'. However there is another '0' at index 4? Actually that's the same. But we have another '0' at index? s[2] is '0', s[4] is '0'. That's the two zeros of t. But can we form a non-contiguous subsequence? Let's attempt: We need "010". We could take first char from index2 (must be '0'), but we want non-contiguous. Could we take first char from index2, second char from index? second char needs '1'. The '1's in s: at indices 1,3,5. Could we take second char from index3 (which is inside the substring originally) but that would be the original second char? Actually original second char is at index3? Wait t[2] is s[3]? Because l=2, so t indices: 2->'0',3->'1',4->'0'. So t[2]='1' at index3. We could pick second char from index5 (after r) maybe? index5 is '1', that is after r=4. So we could try: first char from index2, second from index5, third char must be '0' after index5? But there is no index>5. So fails. What about first char from index2, second from index1? Cannot because order: indices must increase, index1 <2. So not allowed. Could we take first char from index4? That would be '0' but then we need '1' after index4: index5 is '1', then need '0' after index5: none. So fails. Could we take first char from index2, second from index3 (original), third from index? need '0' after index3: index4 is '0', but that would give indices 2,3,4 which is contiguous. Not good. If we skip something: 2,3,5? But s5 is '1', not '0'. 2,4,5? That gives "001"? Actually s2='0', s4='0', s5='1' gives "001". Not match. 2,5,? need third '0' after 5 none. 3,4,5? gives "101"? No. So seems impossible. But what about using index? Maybe first char from index2, second from index? Actually we could use first char from index4? That would be '0' but we need first char '0' anyway, but then need '1' after index4: index5 is '1', then need '0' after index5: none. So no.
What if we try to pick first char from index? There is also '0' at index? Only indices 2 and 4. So first char could be from index2 or 4. If from index2, then second must be after 2, third after that. Options: second from index3 (1), third from index4 (0) -> contiguous. second from index3, third from somewhere after 4? none. second from index5 (1), third? none. second from index? other 1's: index1 is before. So only contiguous works. If first from index4, second from index5 (1), third none. So no non-contiguous. So answer NO. That matches condition: no earlier s[l] (index2 char '0') before l, no later s[r] (index4 char '0') after r. So condition gives NO.
Now consider a case where neither first nor last character appears outside, but some middle character appears outside and we could adjust to make non-contiguous? e.g., s = "111", n=3, query l=1,r=3 => t="111". s[l]= '1', earlier? none. s[r]= '1', later none. But could we find good subsequence? Need "111". All characters are '1'. Could we take indices 1,2,3 contiguous - not good. Could we take 1,3? That's length 2, yields "11", not "111". Could we take 1,2? "11". So no. So NO.
But consider s = "11011", n=5, query l=2,r=4 => t="101". s[l]= '1' at index2, earlier index1 is '1' -> that's before l! So condition satisfied, YES. That works. Use p=1, then indices 1,3,4? Wait need exactly 3 chars: first from p=1 (1), then second? t[2]='0', we need a '0' after p=1. In the substring, the '0' is at index3? Actually t="101": s[2]='1', s[3]='0', s[4]='1'. So second char is at index3. We can pick index3 for '0', then third char '1' at index4. So subsequence 1,3,4 yields "101". Check: indices 1,3,4: 1->'1',3->'0',4->'1' => "101". Are these contiguous? 1 to 3 gap (skip index2), so not contiguous. Good. So condition holds.
Now consider scenario where first and last characters are both not available outside, but a middle character appears outside and we could use that with some rearrangement? For instance, t = "aba"? Actually binary only, so characters '0' and '1'. So middle character could be 0 or 1. Suppose we have s = "00100", n=5, query l=2,r=4 => t="010". s[l]= '0' at index2, earlier index1 is '0'! That is before l. So condition satisfied. So to avoid condition, we need first char not before l and last char not after r. That implies that the character at position l is either '0' and there is no '0' before l, or '1' and no '1' before l. Similarly for r. But there could still be a middle char that appears elsewhere and we could possibly use it to create a non-contiguous subsequence by replacing that middle char with an occurrence outside, while still using the original first and last? Let's construct a counterexample.
We need: s[l..r] = t. s[l] not found before l. s[r] not found after r. Yet there exists a non-contiguous subsequence equal to t.
We need to find a set of indices not equal to l,...,r that produce t, with a gap.
Because we cannot change first or last characters from outside, we might need to change them from inside? But they are already at l and r. Could we use a different occurrence of first character that is after l but before r? But then the first character would be taken from inside, say at index p where l < p <= r, and p is not l. Then we need to match the rest of t. But if we take first character from p > l, then we need to match t[2..L] after p. However, the original substring from l to r includes t[1] at l, but we are skipping it. We could try to use the part from p to r to match the remaining characters. But we need to match t exactly. If we take first character from p (which equals s[l]), then the substring from p to r might be something like s[p..r]. Since s[l..r] = t, s[p..r] is a suffix of t, starting at index (p-l+1) of t. To get the full t, we need to prefix with s[l..p-1]? But we can't because we already used p as first character. Actually we need to produce t = t[1] t[2] ... t[L]. If we take first character from p (which is t[k] where k = p-l+1? Because s[p] = t[p-l+1] maybe). But we need first character to equal t[1]. For that to hold, we need s[p] = t[1] = s[l]. So that is possible if there is another occurrence of the same character as s[l] within (l+1 .. r). That is, s[l] appears again later within the substring. Similarly, we could then try to match the remaining characters. But we need to match all L characters. If we take first character from p (p>l), then we have used one character that corresponds to t[1]. Now we need to match t[2..L] using indices > p. The substring from p+1 to r provides s[p+1..r] which corresponds to t[p-l+2 .. L]. But we need t[2..L] which is longer: it includes t[2..(p-l+1)] as well. So we are missing those characters. Could we get them from before p? No, indices must increase. So if we take first character from a later position p > l, we cannot recover the missing characters t[2..p-l] because they would have to appear before p, but we already passed them. So the only way to get a full match is if the missing part is empty, i.e., p=l. So the first character must be taken from index <= l to cover t[1]. Similarly, the last character must be taken from index >= r to cover t[L]? Actually by symmetry, if we try to take last character from earlier than r, we would miss trailing characters. So for a subsequence to match t exactly, the first selected index must be at most l? Not necessarily: consider t = "00", s = "00". If we take first from index2 (second zero) and second from somewhere after? But need two zeros; after index2 we have none. So cannot. In general, for a subsequence to produce t, the earliest index we can choose for t[1] must be some position where s[i]=t[1]. That i could be > l, but then we need to find t[2..L] after i. Since the original substring from l to r contains t, but if i > l, then the part t[1..(i-l)] is skipped and can't be recovered. So unless those skipped characters are redundant? They are part of t, so we need them. The only way to still match is if there are other occurrences of those skipped characters after i, but they would be in different order potentially? But the order must be preserved. For example, suppose t = "010", and there is another occurrence of '0' at position p>l, and also there is a '1' after p that corresponds to t[2]? But t[2] is '1'. But we also need t[1]? Actually if we start with '0' at p, we need after that '1' and then '0'. That would produce the string "0"+"1"+"0", which is exactly t. But we also need to have t[1] being '0', and we have that. But we are missing the first '0' that originally was at l? Wait t is "010". So if we take first '0' at p (which is > l), then we need to match the rest: after p, we need '1' then '0'. But the original substring had at l: '0', l+1: '1', l+2: '0'. If p > l, say p = l+2 (which is the last '0' of the substring), then after p we have no characters to match '1' and '0'? Actually we need '1' then '0' after p. But after p (l+2) maybe there is no '1'? So cannot. If p = l+1? But l+1 is '1', not '0'. So not. So we would need a '0' later than l that also has after it a '1' and another '0'. Could that happen? Possibly if there is another occurrence of "010" later in the string. But that would mean that there is a different occurrence of t as a substring starting at some index > l. But then we could use that substring as the subsequence, which would be contiguous (since it's a substring) - but if it's contiguous at that later location, then the subsequence would be contiguous (indices consecutive) and thus not good. However we might break contiguity by using mixed parts? But we are trying to construct a non-contiguous one using only later positions? That seems unlikely because if we take all characters from a contiguous block, it's contiguous. To be non-contiguous, we must have at least one gap. So we need to mix indices from different parts.
Let's consider if we can use the first character from l (since we can't get it before) and the last character from r (since can't get after) but we could replace a middle character with an occurrence outside to break contiguity. For example, suppose we have t = "010", s[l] = '0' with no earlier '0', s[r] = '0' with no later '0', but there is a '1' somewhere outside the range (either before l or after r) that we could use for the middle character? But then we would need to still have first and last characters from within? Let's see: If we take first from l, middle from some outside index q (which could be before l? But then order would be q < l, cannot because we already have first from l and indices must increase; if we want to use q for middle, we need q > first and < last. So if q is before l, it would be before first, cannot be used as middle. So q must be between l and r or after r? Actually after first (l) and before last (r). So q must be in (l, r) to be middle. But if q is outside (l,r), say after r, then after taking first from l, we then need to take middle after l, but if q > r, then we would pick middle after r, and then we would need last after middle, but last is at r which is before q, so can't because order: indices must increase. So if we take middle from after r, we cannot then take last from r because r < q. So we would need to also take last from after q. But then last would be after r. That would require s[r] appears after r, which we assumed false. So not possible. Similarly, if we take middle from before l, order impossible. So the only way to break contiguity while keeping first and last fixed is to pick some character from within (l, r) but not from the original position? Actually original contiguous sequence uses indices l, l+1, ..., r. If we keep first and last as l and r, we can replace some interior index with another occurrence of the same character that is also between l and r but not at the exact position, or possibly from outside? As argued, outside can't be used without violating order. So we need another occurrence of that interior character within (l, r). Then we could pick first l, then for the second character (if we are replacing it) we could pick a different index j in (l, r) with s[j] = t[2], but j != l+1. However, we still need to pick the third character, etc. The sequence must be strictly increasing. If we change one index, we may need to adjust others to maintain increasing order and matching characters.
For example, consider s = "01010", n=5, query l=2,r=4 -> t="101". s[l]= '1' (index2). Is there a '1' before index2? index1 is '0', so no. s[r]= '0' (index4). Is there a '0' after index4? index5 is '0'? Actually s[5] = '0'? s="01010": indices:1:'0',2:'1',3:'0',4:'1',5:'0'? Wait given "01010": it's 0,1,0,1,0. So index4 is '1'? Let's re-evaluate: "01010": positions: 1:'0',2:'1',3:'0',4:'1',5:'0'. So for l=2,r=4, t = s[2..4] = "101". So s[l]= '1' at index2, earlier index1 is '0' -> not same. s[r]= '1' at index4, later index5 is '0' -> not same. So neither condition holds. But can we find a good subsequence equal to "101"? Let's attempt. The whole string: positions:1:0,2:1,3:0,4:1,5:0. We need subsequence "101". Options: could use indices 2,3,5? That gives 1,0,0 = "100" not match. 2,4,5 gives 1,1,0 = "110". 2,3,4 contiguous, not good. 1,2,4 gives 0,1,1 = "011". 1,3,4 gives 0,0,1 = "001". 1,2,5 gives 0,1,0 = "010". 1,4,5 gives 0,1,0 = "010". 2,4,? need third after4:5 is 0, so 2,4,5 = "110". 3,4,5 gives 0,1,0 = "010". So none give "101". What about using index? 2,3,? can't get 1 after3 except 4 which gives "101" but contiguous. 2,5,? need two more? Actually length 3, so need exactly three. 4,? can't because need first char 1 from before? So seems NO. So condition holds.
But is there a scenario where interior character duplication allows a non-contiguous subsequence while first and last characters are unique on the sides? Let's try to construct such a scenario.
We need t = s[l..r]. Suppose inside the substring there is a character that appears at two different positions. For example, t = "1001". s[l..r] = "1001". The characters: s[l]='1', s[l+1]='0', s[l+2]='0', s[l+3]='1'? Actually "1001" has indices: l:1, l+1:0, l+2:0, l+3:1. So there are two zeros in the middle, both '0'. And the first and last are '1's. Suppose outside the range, there is no '1' before l or after r, but the zeros appear twice inside. Could we form a non-contiguous subsequence? The contiguous is l,l+1,l+2,l+3. To break contiguity, we could perhaps use the first '1' at l, then for the second character (which should be '0'), we could skip the first zero and take the second zero at l+2, then take the third character? But we need four characters: positions: l (1), then we need '0' after l: we could take l+2 (0), then we need another '0' after that: but the next zero would be? Actually we need sequence "1 0 0 1". After taking l and l+2, we still need "0 1"? Wait we have used: first char (1), second char (0) at l+2. Now we need third char to be '0' and fourth char '1'. But after l+2, we have l+3 which is '1', not '0'. So we cannot get the third '0'. What if we take l (1), then l+1 (0) then l+3 (1) but we are missing a '0'? That would be "101". Not enough. Or l, l+2, l+3? That gives "1 0 1" missing a '0'. To get four characters, we could also try to use l+1 and l+3 along with something else. Could we start with the second occurrence of '1'? There is only '1' at l and l+3. If we try first char from l+3? Not possible because need later characters for rest. So maybe we could use the duplication to create a gap by taking some characters from outside? But we assumed outside doesn't have first or last. But maybe there is a '0' outside that we could use for one of the middle characters. Let's consider s = "01001", with l=2,r=5? s:1:0,2:1,3:0,4:0,5:1 -> t="1001". Here s[l]=1 at index2, no earlier '1'? index1 is 0, so no. s[r]=1 at index5, later none (n=5). But there is a '0' outside at index1 and index3,4 inside. Could we form a good subsequence? Try to find "1001". Options: 2,3,4,5 contiguous not good. 2,3,4,? need 1 after4 none. 2,3,5 gives "101". 2,4,5 gives "101". 1,2,4,5 gives 0,1,0,1="0101". 1,2,3,5 gives 0,1,0,1="0101". 1,3,4,5 gives 0,0,0,1="0001". 2,3,? can't get four. 1,2,3,4 gives "0100". So none. What about 2,4,? need two after 4? 5 is 1, but need '0' after? Actually need after 2: we need "001"? Wait "1001": after first 1, we need 0,0,1. So if we pick 2 as first, we need three after: 0,0,1. We could pick 3 (0), 4 (0), 5 (1) contiguous. To break contiguity, we could pick 2,3,5? That gives 1,0,1 missing a 0. 2,4,5 gives 1,0,1 missing a 0. 2,3,4,? no. So no.
What about using first from somewhere else? The only other 1 is at index5, but if we start at 5, we need "001" after, none. So no.
Thus seems not.
Maybe a scenario with length 2: t length=2. For a subsequence of length 2 to be good, it must be non-contiguous, i.e., indices not consecutive. So we need two occurrences of the same two-character string in s such that they are not adjacent. For query substring s[l..r] of length 2, we ask if there exists a pair i<j, with j>i+1, such that s[i]=s[l] and s[j]=s[r] and (i,j) not equal to (l, r) maybe? Actually it can be equal? If (i,j) = (l,r) then they are consecutive? Since l<r and r=l+1 (because length 2 and l<r implies r=l+1 because indices are integer and l<r, but could there be gap? Actually substring length 2 means r = l+1 because contiguous. So (l,r) are consecutive. So that subsequence is contiguous, not good. So we need a non-consecutive pair (i,j) with j-i >=2 that yields same two characters. So condition: there exists i < j with j-i >=2, s[i]=s[l], s[j]=s[r]. Additionally, we need to ensure that (i,j) is a valid subsequence. This is similar to: either there is an occurrence of s[l] before l with any later occurrence of s[r] after that (could be after l, but must be after i). Or there is an occurrence of s[r] after r with an earlier s[l] before that? But careful: If we use i from before l, we can use j from after i, possibly r or later? But j must be > i. To be non-contiguous, we need j-i >=2. If i is before l, and j = r, then j-i >=? Since i <= l-1 and j=r = l+1, then j-i >= (l+1) - (l-1) = 2, so non-contiguous if i <= l-1? Actually if i = l-1, j = l+1 gives gap of exactly 2, non-contiguous. If i = l-2, even larger. So condition: exists i < l such that s[i]=s[l] and there exists any j > i with s[j]=s[r]? But j must be after i. Could be j = r? But j = r is allowed and then indices i and r are not consecutive because there is at least one index between (i+1 to r-1 includes l maybe). However, is there any issue that j must be > i and also we need exactly two characters? So if we pick i from before l, we could pick j = r (which is the original last character). That yields subsequence (i, r). Check: s[i] = s[l] = t[1], s[r] = t[2]. So it matches t. And it's non-contiguous because between i and r there is at least index l? Actually if i = l-1 and r = l+1, then indices are consecutive? l-1 and l+1: they are not consecutive because there is index l between them, but are they considered "contiguous"? The definition: "A subsequence is said to be good if it is not contiguous and has length ≥ 2." Usually contiguous means that the chosen indices are consecutive integers. So for two indices to be contiguous, they must be i and i+1. If i and j have j = i+1, they are consecutive. If j > i+1, they are not consecutive. So indeed i and r with r = i+2? Actually if i = l-1 and r = l+1, then i and r have difference 2, so not consecutive. So it's good. So condition: existence of i < l with s[i] = s[l] yields a good subsequence for length 2 (by taking i and r). Similarly, existence of j > r with s[j] = s[r] yields good subsequence (by taking l and j). Also, could there be a case where we take i from after l but before r? That would be i > l, then j must be > i. To be non-contiguous, we need j-i >=2. But if we take i = l+? then j could be r? But r = l+1 for length2, so i > l => i >= l+1? Actually r = l+1, so if i > l, the only possible j > i is > l+1, but r = l+1 is not > i if i>=l+1. So j would have to be > l+1, which would be after r. But then we need s[j]=s[r]. That would require a later occurrence of s[r] after r. So that's covered by the second condition (s[r] after r). What about i from inside but j from inside with a gap? Not possible because there are only two positions inside the substring: l and r=l+1. So any i,j both inside must be (l,r) which are consecutive. So only possibilities are i before l with j=r, or i=l with j after r. So conditions for length 2 are exactly: s[l] appears before l OR s[r] appears after r.
Now for length > 2, we have seen that conditions s[l] appears before l or s[r] appears after r are sufficient. But are they necessary? Let's try to find a counterexample where answer is YES but neither s[l] before l nor s[r] after r holds.
We need t = s[l..r] (L>=3). Suppose there is no occurrence of s[l] before l, and no occurrence of s[r] after r. Yet there exists a good (non-contiguous) subsequence equal to t.
This would require that we construct a subsequence that matches t, but its first index is not before l (because s[l] doesn't appear before l, and if we used a first index before l, that would be an occurrence of s[l] before l, contradiction). So first index must be >= l. Since s[l] does not appear before l, any index i with s[i]=s[l] must be either l itself or > l. But if we use first index > l, we argued it would be difficult because we miss initial part of t. However, maybe we could use first index = l (so first character from original position), but then we need to break contiguity elsewhere. Similarly, last index must be <= r because s[r] not after r, so last index must be <= r, and since s[r] occurs at r, maybe also at some earlier position? But if we use last index < r, then we would need to supply trailing part of t from earlier? That seems impossible because we need the last character after previous ones. Let's reason more formally.
We consider any subsequence (i1, i2, ..., iL) matching t. Since s[i1] = t[1] = s[l]. If there is no occurrence of this character before l, then i1 cannot be < l. So i1 >= l. Similarly, s[iL] = t[L] = s[r]. If there is no occurrence of this character after r, then iL <= r. So we have l <= i1 < i2 < ... < iL <= r.
Thus all indices are within [l, r]. And the subsequence must be exactly the characters of the substring t (which is s[l..r]). Since all indices lie within [l,r] and the string formed is exactly s[l..r], is it possible for the selected indices to be not exactly the full set l,...,r? Could they be a proper subset? But they must have length L, and there are exactly L indices in [l,r]. So the only way to have L distinct indices in [l,r] is to take all of them? Actually [l,r] contains exactly (r-l+1) = L positions. If we select L distinct indices from [l,r], we must select every index exactly once because there are exactly L positions. So the only possible L-length increasing sequence of indices within [l,r] is exactly (l, l+1, ..., r) in order. Because to have L distinct increasing integers all within an interval of length L (i.e., from l to r inclusive), the only possibility is to take all integers l, l+1, ..., r. Proof: The smallest possible set of L distinct integers in [l,r] is {l, l+1, ..., l+L-1} which equals [l,r]; any other set would have a gap, but the maximum size of a set within [l,r] is L, and the only set of size L is the whole interval. Indeed, if we have L distinct integers all between l and r inclusive, and the interval length is exactly L-1 (since r - l = L-1), the only way to fit L integers is to take every integer. So indeed, the indices must be exactly {l, l+1, ..., r} in increasing order.
Therefore, if i1 >= l and iL <= r, given L = r-l+1, we necessarily have i1 = l, i2 = l+1, ..., iL = r. So the subsequence is exactly the contiguous substring, which is not good (contiguous). Therefore, for a good subsequence to exist, we must avoid that both i1 >= l and iL <= r. That is, we must either have i1 < l or iL > r (or both). But i1 < l implies there is an occurrence of s[l] before l. iL > r implies there is an occurrence of s[r] after r. Therefore, the necessary and sufficient condition for existence of a good subsequence is:
- Either there exists an index p < l such that s[p] = s[l],
- Or there exists an index q > r such that s[q] = s[r].
And this condition is also sufficient, as we have constructive proofs: if p < l exists, use p as first character, then for the remaining L-1 characters use l+1, l+2, ..., r (i.e., skip the original first character). This yields a valid subsequence because s[p]=s[l] and s[l+1..r] = t[2..L]. The subsequence indices: p, l+1, l+2, ..., r. These are strictly increasing because p < l < l+1. And there is a gap: between p and l+1, at least one index (l) is skipped. So it's good. Similarly, if q > r exists, use l, l+1, ..., r-1, q.
But wait: In the case where we use p < l, we need to ensure that we can pick l+1, l+2, ..., r after p. That's fine because l+1 > p. Also need to ensure that the resulting subsequence has length L: p plus the L-1 indices from l+1 to r. That's exactly L. So it's valid.
One nuance: What if L=2? Then using p < l, we take p and r (since l+1 = r). That yields indices p, r. That's good if p and r are not consecutive. As argued, p <= l-1, r = l+1, so p and r have at least difference 2? Not necessarily if p = l-1 and r = l+1, diff = 2. So it's non-consecutive. If p is even earlier, diff larger. So good. Using q > r, we take l and q, similarly non-consecutive because q >= r+1, and l = r-1? Actually when L=2, l and r are consecutive: r = l+1. Then l and q with q >= r+1 = l+2, difference >=2, so non-consecutive. So condition works.
Thus the condition seems both necessary and sufficient for all lengths >=2.
But we must double-check edge cases: Could there be a scenario where we have p < l but using p and then l+1,...,r might not be a valid subsequence because maybe the character at l+1 is not equal to t[2]? But t[2] is s[l+1] by definition. So it's fine. The only requirement is that s[p] = s[l] which we have. So works.
Similarly for q > r.
But is there any subtlety about "good subsequence" definition? It says: "A subsequence is said to be good if it is not contiguous and has length ≥ 2." "Not contiguous" means the subsequence is not a contiguous subsequence? Usually it means the indices are not consecutive. But could it also mean that the subsequence is not a substring? That is the same: a substring is a contiguous subsequence. So yes.
Thus, condition is simple.
But wait: In the constructive proof, we used the indices p, l+1, l+2, ..., r. However, we must ensure that the subsequence we construct is indeed "good", i.e., not contiguous. Is it possible that this subsequence becomes contiguous inadvertently? That would require that p and l+1 are consecutive, i.e., p = l? But p < l, so p <= l-1, and l+1 is at least l+1. So the gap between p and l+1 is at least 2 (since l+1 - (l-1) = 2). So they are not consecutive. Also within the tail part from l+1 to r, those are consecutive indices. But the definition of contiguous for the whole subsequence: "contiguous" means that the selected indices, in order, are consecutive integers. If we have p, l+1, l+2, ..., r, are these indices consecutive? For them to be consecutive, we would need p = l (to connect to l+1) but p<l, so no. So indeed they are not all consecutive. So it's good.
Similarly, for q > r, we have l, l+1, ..., r-1, q. The segment from l to r-1 is consecutive, but then jump to q >= r+1, so not all consecutive.
Thus conditions sufficient.
Now, are there any other possibilities? Suppose we have both pr. Then either works.
Now, is there any scenario where neither condition holds but we could still have a good subsequence by using a combination: e.g., using some index i < l for first character, and also some index j > r for last character, but also adjusting middle characters? That would be covered by p<l condition already (since p<l works alone). But maybe using pr together might be necessary if using only p<l fails because we need to skip something? Our construction for p<l used the tail from l+1 to r. That works regardless of q. So p<l alone gives a valid subsequence. So condition is simply OR.
Thus answer per query: YES if s[l] appears in s[1..l-1] OR s[r] appears in s[r+1..n]; else NO.
But wait: Check example: first test case: s="001000", n=6. Queries:
2 4: substring "010". s[l]=s[2]='0'. Is there '0' before index2? s[1]='0', yes -> YES. Output YES. matches.
1 3: substring "001". s[l]=s[1]='0', before index1? none. s[r]=s[3]='1', after index3? s[4]='0', s[5]='0', s[6]='0' -> no '1' after. So NO. Output NO. matches.
3 5: substring "100". s[l]=s[3]='1', before index3? s[1]='0',s[2]='0' -> no '1'. s[r]=s[5]='0', after index5? s[6]='0', yes '0' after. So YES. Output YES. matches.
Second test case: s="1111", n=4. Queries:
1 4: substring "1111". s[l]='1', before index1? none. s[r]='1', after index4? none. So NO. Output NO. matches.
2 3: substring "11". s[l]=s[2]='1', before index2? s[1]='1', yes -> YES. Output YES. matches.
So condition works for given examples.
Thus solution: For each test case, read n,q,s (string of length n). Preprocess maybe prefix/suffix occurrences? For each position i, we can compute whether character '0' or '1' appears before i or after i. Since n small, we can just check per query by scanning.
But careful: The condition is existence of s[l] in prefix [1, l-1] and s[r] in suffix [r+1, n]. That is trivial to check by scanning O(n) per query, total O(nq) ≤ 100100=10k per test, t up to 100 => 1e6, fine. Or we can precompute for each index the earliest occurrence of 0/1 to left and right. But given constraints, simple scanning is okay.
Double-check any corner cases: l and r such that length =2 and condition uses s[l] before l? If s[l] appears exactly at l-1? That's before l, so ok. s[r] after r at r+1 also ok.
But could there be a scenario where the constructed subsequence, while not contiguous, is actually the same as the substring? No, because we changed indices.
One nuance: The subsequence we construct using p < l and l+1..r: Is it guaranteed that the subsequence is not equal to the substring? The substring uses l..r; our uses p, l+1..r. Since p ≠ l, they differ. So it's a different subsequence. Good.
Now, is there any scenario where p < l exists but the constructed subsequence might be invalid because the character at l+1 is not available because we need to pick it? But l+1 exists since r >= l+1? Actually length >=2, so r > l, so l+1 <= r. So it exists.
Edge case: If length =2, then using p < l, we take p and r. That's fine. Using q > r, we take l and q.
So condition appears sufficient.
Now, is there any possibility that the condition fails (no p, no q) but there exists a good subsequence that uses first character from l (since cannot from before) and last character from r (since cannot from after) but with some interior character taken from outside? But as argued, if we use first from l and last from r, all indices must be between l and r inclusive. But we need L indices; the only set of L distinct indices in [l,r] is the whole interval, so the subsequence would be exactly l..r, which is contiguous. Could we use first from l and last from r but skip some interior index and replace it with a duplicate from within [l,r]? But then we would have L indices but one index from within is omitted and another duplicate from within is used instead. However, we still need L distinct indices from [l,r]. Since the interval contains exactly L positions, to have L distinct indices we must include every position. Because if we omit some index k and include some other index j that is already included? But j must be distinct. But there are only L positions total. To select L distinct numbers from a set of L numbers, we must select all of them. So it's impossible to omit one and include a different one because that would mean we are selecting L numbers from a set of L numbers, but omitting one and including another that is not in the set? Actually all numbers we can choose are from [l,r]. If we omit some index, say we don't pick l+1, then we need to pick another index from [l,r] that is not already in the selection to keep count L. But all indices in [l,r] except l+1 are the other L-1 indices. To have L selections, we must include l+1 as well, because there are only L-1 other indices. So we cannot replace an interior index with a duplicate because there is no extra index; the interval size is exactly L. So the only way to have L distinct indices in [l,r] is to take all of them. So any subsequence with i1>=l and iL<=r must be exactly the contiguous one. Therefore, condition is necessary.
What if the subsequence uses some indices outside [l,r] but still i1>=l and iL<=r? That's impossible because if an index outside is used, it would be either < l or > r, contradicting i1>=l and iL<=r. So okay.
Thus condition is both necessary and sufficient.
Hence problem reduces to: For each query (l, r), check if s[l] appears in s[1..l-1] OR s[r] appears in s[r+1..n].
Implementation: For each test case, we can precompute arrays: left0[i] = true if there's a '0' in positions 1..i-1, similarly left1[i] for '1'. Or simply compute prefix presence of '0' and '1'. Similarly suffix presence.
Simplify: For each position i, we can compute has_left[0][i] and has_left[1][i] indicating whether character '0'/'1' exists to the left of i (excluding i). Similarly has_right.
Then for query (l,r): if (s[l] == '0' and has_left[0][l]) or (s[l]=='1' and has_left[1][l]) or (s[r]=='0' and has_right[0][r]) or (s[r]=='1' and has_right[1][r]) -> YES else NO.
We can implement by scanning once.
Because constraints small, we can even just for each query scan left part and right part. That's simpler.
Let's test some more cases:
Case: n=5, s="10101", query l=2,r=5? l=2 r=5 gives t="0101". s[l]='0', before index2? s[1]='1' -> no. s[r]='1', after index5? none. So condition says NO. Is there a good subsequence? Let's test manually: t="0101". Could we find? s:1:1,2:0,3:1,4:0,5:1. Subsequence possibilities: using p<l? none. using q>r? none. But maybe using mixture? Since all indices must be within [2,5] if we cannot use outside? But maybe we could use index1 for something? If we use index1 for first char? But first char must be '0', index1 is '1'. So not. Could we use index? For first char we need '0', only indices with '0' are 2 and 4. To avoid using 2 as first, we could use 4 as first? But then we need '1' after 4: index5 is '1', then need '0' after 5: none. So not. If we use 2 as first, then we need '1','0','1' after. Options: after 2, indices 3,4,5 give "101" which is correct: 2,3,4,5 gives "0101"? Actually 2:0,3:1,4:0,5:1 => "0101". That's contiguous (2-5 are consecutive? Check: 2,3,4,5 are consecutive, yes they are consecutive (difference 1). So that's not good. Could we make non-contiguous by using 2,3,4,? need fourth char? Actually we need four chars. Could we do 2,3,5? That's only three. 2,4,5 gives 0,0,1="001". 2,3,4,? need one more after4? but 5 is 1, so 2,3,4,5 contiguous. 2,3,5,? need four; not possible. 2,4,? need two after4? only 5, insufficient. 3,4,5? starts with 1, not 0. So no good. So answer NO matches.
Case: s="000", n=3, query l=1,r=3: t="000". s[l]='0', before index1? none. s[r]='0', after index3? none. So NO. Is there good subsequence? Need three '0's non-contiguous. Could we use indices 1,2,3 contiguous only. Indices 1,3 gives two zeros, length 2, but we need three. So NO.
Case: s="010", n=3, query l=2,r=3: t="10". s[l]='1', before index2? index1='0', no. s[r]='0', after index3? none. So NO. Check: need non-consecutive "10". Subsequence "10" from indices 2,3 is consecutive. Could we get from indices? 2,? after 2 only 3. So no. So NO.
Case: s="1001", n=4, query l=1,r=4: t="1001". s[l]='1', before? none. s[r]='1', after? none. So NO. Check possible good subsequence? Could we use indices 1,2,4? That gives 1,0,1="101". Need four chars. 1,3,4 gives 1,0,1? Actually s[1]=1,s[3]=0,s[4]=1 =>"101". 2,3,4 => "001". So no four non-contiguous. So NO.
Case: s="1010", n=4, query l=2,r=4: t="010". s[l]='0', before index2? index1='1' no. s[r]='0', after index4? none. So NO. Check: possible? We need "010". Options: 2,3,4 contiguous. 1,2,4 =>1,0,0="100". 1,3,4 =>1,0,0="100". 2,3,? no. So NO.
Case: s="0100", n=4, query l=1,r=3: t="010". s[l]='0', before? none. s[r]='1', after index3? index4='0' not 1. So NO. Check: possible? Need "010". 1,2,3 contiguous. 1,2,4 =>0,1,0="010"? Actually 1:0,2:1,4:0 => indices 1,2,4: are they consecutive? 1,2 are consecutive, but 2 and 4 have gap of 1 (index3). The whole set 1,2,4: are they all consecutive? No, because 1 and 2 are consecutive, but 2 and 4 are not, so the subsequence is not contiguous because for a subsequence to be contiguous, all selected indices must be consecutive integers. Here we have a gap after 2 (3 is missing), so it is non-contiguous. So "010" is formed by indices 1,2,4? Check s[1]=0, s[2]=1, s[4]=0 -> yields "010". So this is a good subsequence! But our condition said NO? Let's verify: l=1,r=3, s[l]=s[1]='0', before index1? none. s[r]=s[3]='0', after index3? index4='0', so s[r] after r: s[3]='0', and index4='0' exists, so condition: s[r] appears after r? Yes! Because s[r] = '0', and there is a '0' at index4 >3. So condition should be YES. I mis-evaluated: I thought s[r]='1', but actually s[3]='0'? Wait s = "0100": indices:1:'0',2:'1',3:'0',4:'0'. So r=3, s[3]='0'. After index3, index4='0' -> yes! So condition gives YES. So it's consistent.
Now let's try to find a counterexample where condition fails but a good subsequence exists via more complex rearrangement. Based on the interval cardinality argument, it seemed necessary that either i1r. Let's rigorously prove.
Claim: If there exists a good subsequence (i1,...,iL) equal to t = s[l..r], then either i1 < l or iL > r.
Proof: Suppose not, i.e., l <= i1 and iL <= r. Since indices are increasing and all within [l, r]. The number of indices L = r-l+1. The interval [l, r] contains exactly L integers. A set of L distinct integers in [l, r] must be exactly all of them. Therefore, {i1,...,iL} = {l, l+1, ..., r}. Moreover, because they are in increasing order, i_k = l + k - 1. So the subsequence is exactly the contiguous substring, which is not good. Contradiction. Hence either i1 < l or iL > r.
Now, i1 < l implies s[i1] = t[1] = s[l], so there exists an occurrence of s[l] before l. iL > r implies s[iL] = t[L] = s[r], so there exists an occurrence of s[r] after r.
Thus condition is necessary.
Now, is it possible that i1 < l but there is no occurrence of s[l] before l? That's contradictory. So condition necessary.
Therefore condition is both necessary and sufficient. So answer is simply as described.
Edge Cases: What if L=1? But constraints say l_i < r_i, so length at least 2. So fine.
Thus solution is trivial.
But wait: There might be a nuance: The substring s[l..r] could have length 2, and the constructed subsequence using p < l and r might be p and r. But is it guaranteed that p and r are not consecutive? As argued, p <= l-1, r = l+1, so difference at least 2. But what if l=2, p=1, r=3 => diff=2, not consecutive. So good. Similarly for l=2, p=0? Not possible. So fine.
What if we use q > r with l and q: l = r-1, q >= r+1 => diff >=2.
Thus all good.
Now, check if there is any scenario where using p < l yields a subsequence that is contiguous? Only if p = l and l+1,...,r, but p<l prevents that.
Thus solution.
Implementation details: Input t up to 100. For each test case, read n,q, s (1-indexed or 0-indexed). For each query (l,r), convert to 0-index if needed. Check:
- For i in range(0, l-1) (if using 1-indexed positions) if s[i] == s[l-1] (since string indices 0..n-1). Or easier: keep s as string with 0-index, l and r as 1-indexed from input. Then positions are l-1 and r-1.
Let left_char = s[l-1], right_char = s[r-1].
Check if left_char appears in s[0:l-1] (i.e., positions before l-1) or right_char appears in s[r: n] (positions after r-1). If yes print YES else NO.
We can compute prefix arrays: left0[i] = True if '0' in s[0:i] (i exclusive). Actually for position i (0-index), we want to know if character c appears in s[0:i]. So we can maintain counts.
Simplify: For each query, just loop from 0 to l-2 to check left, and from r to n-1 to check right. Since n <= 100, q <=100, it's fine.
But note: Example output: For first test case, they output "YES", "NO", "YES". For second: "NO", "YES". So just print per query.
Double-check with provided examples.
Now, I'll produce a solution in Python.
But wait: There is also a possibility: What if the substring is of length >=2 but the constructed subsequence using p < l might accidentally be contiguous if the skipped part is exactly one index and the indices become consecutive? But p < l, and we take l+1,...,r. The smallest p is l-1. Then indices: l-1, l+1, l+2, ..., r. Are these consecutive as a whole? For them to be consecutive integers, we would need that after l-1, the next is l, but we have l+1, so there's a gap. So not consecutive. So fine.
Thus solution.
Edge Cases: n=2, q=1, s="01", query l=1,r=2. s[l]='0', before l? none. s[r]='1', after r? none. So NO. Check: only possible subsequence of length 2 equal to "01" is indices 1,2 contiguous. So NO.
What about s="00", l=1,r=2: s[l]='0', before? none. s[r]='0', after? none. So NO. But is there a non-contiguous subsequence "00"? Could use indices? Only two positions, both needed to get two characters, but they are consecutive. So NO.
Now, the problem statement mentions "A subsequence is said to be good if it is not contiguous and has length ≥ 2." The example: In first test case, they gave s_1s_3s_5 ("1100110") and s_2s_3s_6 as good. Actually they said s_1s_3s_5 and s_2s_3s_6 are good, while s_1s_2s_3 is not good. So they consider any subsequence that is not contiguous as good. So our interpretation matches.
Thus final solution is straightforward.
But before finalizing, let's confirm with another custom test where condition fails but maybe using both sides? Example: s = "101", query l=2,r=3 => t="01". s[l]='0' before? index1='1' no. s[r]='1' after? index3 is r, after none. So NO. Indeed no good.
Now, try to craft potential counterexample: Suppose s[l] appears before l but we cannot use that because after taking p, we need l+1,...,r but maybe l+1 does not exist because length maybe 2? Actually for length 2, l+1 = r, so it exists. So fine.
Thus final solution.
Implementation:
for each test case:
read n, q
read s (string length n)
for each query:
read l, r
l0 = l-1; r0 = r-1
left_char = s[l0]; right_char = s[r0]
left_ok = any(s[i] == left_char for i in range(0, l0))
right_ok = any(s[i] == right_char for i in range(r0+1, n))
if left_ok or right_ok: print("YES")
else: print("NO")
Complexity O(t * q * n) ≤ 100 * 100 * 100 = 1,000,000, fine.
Alternatively, precompute for each index the closest left occurrence of each character, but not needed.
Now, I'll produce answer.
But wait, the problem statement might have additional nuance: The substring s[l..r] might be equal to a subsequence that is not contiguous but uses indices that are not necessarily using the entire tail from l+1 to r. For instance, maybe using p<l and then some other indices after p that are not exactly l+1..r but still produce t. But our construction is sufficient.
Thus final.
One more check: In the constructive proof, we assumed we can take all indices from l+1 to r. This requires that after p, we have enough characters to match t[2..L]. That's true because s[l+1..r] = t[2..L] by definition. So it's fine.
Edge case: What if p is such that p > l? No, p < l.
Now, we must ensure that the subsequence is indeed a subsequence, i.e., we are allowed to pick characters in that order. Yes.
Thus solution is correct.
Now, I will write final answer.
But before finishing, double-check if any trick: The substring itself could be considered a good subsequence if it is not contiguous? But by definition substring is contiguous, so no.
Now, final code.
Potential Pitfall: Input reading: t test cases, for each, read n and q, then s, then q lines of l r.
Output: per query on its own line.
I will use Python.
Also ensure to handle 1-indexed vs 0-indexed.
That's it.
I'll now produce final answer. ```python
import sys
def solve():
it = iter(sys.stdin.read().strip().split())
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
q = int(next(it))
s = next(it).strip()
# 0-indexed string
for _ in range(q):
l = int(next(it)) - 1
r = int(next(it)) - 1
left_char = s[l]
right_char = s[r]
# check left side
left_ok = False
for i in range(l):
if s[i] == left_char:
left_ok = True
break
# check right side
right_ok = False
if not left_ok:
for i in range(r+1, n):
if s[i] == right_char:
right_ok = True
break
if left_ok or right_ok:
out_lines.append("YES")
else:
out_lines.append("NO")
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()