Mr. Sreeniketan the owner of Madras Super Kings is a very superstitious owner. He believes that certain gestures help the team to perform well. Whenever the team enters the field , they form a huddle to discuss strategies. Sreeni wishes that they always stay in the same cyclic order that he instructs them to. So, if that cyclic order is not intact, he needs to ask them to change the order immediately. You are Mr. Gurukanth Aiyappan his close relative and are incharge of reporting any ambiguity with regard to the order suggested by Sreeni. The players are grouped in four categories Batsman represented as B, bowler represented as L, all rounder represented by A and wicket-keeper by W. If the order reported by Guru is not the same as Sreeni expects then he will ask them to change it immediately.
Case Study:
Assuming 5 people in the huddle the representation expected by Mr. Sreeni is BBLAW and that reported by Mr. Guru is LAWBB . So, visualizing each of the orders in a cyclic fashion both are equivalent. Hence, Mr. Sreeni's superstious wish of team huddle is met. So, you need to report whether it is in the correct order or not.
Input
The first line of the input contains an integer T denoting the number of test cases. Each test case contains two lines and a string in each line. The first line contains a string S depicting Sreeni's order and the second line contains a string G depicting Guru's order.Both the strings, S and G, have the same length.
Output
For each test case, output a single line 'YES' or 'NO' (without quotes, quotes only for clarity).
Constraints
1 <= T <= 500
Length of each string is <= 15 (Team huddles in cricket have a huddle of max. 15 players)
Example
Input:
2
BLAW
AWBL
AAABBL
LBABAA
Output:
YES
NO
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to determine if the two strings S and G are equivalent when considered in a cyclic order. That is, if one string can be rotated to obtain the other. Because they are arranged in a huddle in a cycle, the order matters cyclically but not the starting point. So we need to check if G is a cyclic rotation of S.
However, careful: The problem statement says: "visualizing each of the orders in a cyclic fashion both are equivalent." That means if we arrange the players around a circle, the order (clockwise) should be the same. That is indeed checking if one string is a rotation of the other. Because the circle has no designated start; we can start anywhere. So we need to check if G can be obtained by rotating S.
But also note: Could there be reverse cyclic order? In a circle, sometimes clockwise and anticlockwise might be considered different? Typically if you stand around a circle, the order can be clockwise or anticlockwise depending on direction. But the problem didn't specify direction. They just said "cyclic fashion", and in the example: S = "BBLAW", G = "LAWBB". Rotating S: "BBLAW" -> rotate left by 2: "LAWBB". That matches. If reverse order was considered same, then "BBLAW" reversed is "WALBB", not "LAWBB". So it's just rotation.
Thus solution: For each test case, if length of S != length of G, then NO (but given they have same length). Check if G is a substring of S+S. That is, concatenate S with itself and see if G appears as substring. That yields YES if rotation.
Constraints: T up to 500, length up to 15, so trivial.
Edge cases: empty string? Probably length at least 1. But if both empty, they are trivially rotation. But not likely.
Thus answer: For each test, if G is in S+S then "YES" else "NO".
But caution: The input format: first line T, then for each test case two lines: first S, second G. They are strings of characters B, L, A, W presumably (though no validation needed). Should we also consider they could have other chars? Not needed.
Implementation: read integer T, then loop T times: read S (strip newline), read G (strip). Then if (S+S).find(G) != string::npos and S.length() == G.length() (but they said same length) then YES else NO.
But consider scenario: S = "AAA", G = "AAA". S+S = "AAAAAA", find "AAA" returns true. So YES.
If S = "AAAB", G = "AABA". S+S = "AAABAAAB", find "AABA" returns true? Let's check: "AAABAAAB", substring from index 3? "AAAB" -> index3: 'B' then 'A' 'A' 'A'? Actually better to check: indices:0 A,1 A,2 A,3 B,4 A,5 A,6 A,7 B. "AABA": A A B A? That sequence appears? At index 2: A(2) = A, index3 = B, index4 = A -> that gives A B A, missing one A. At index1: A A A B? No. Let's actually test: S+S = "AAABAAAB". Need "AABA". Check sliding windows: 0-3: "AAAB" no, 1-4: "AABA" -> yes, because positions 1=A,2=A,3=B,4=A => "AABA". So rotation works. So it's correct.
Thus solution straightforward.
But the example: Input: 2, BLAW, AWBL => S="BLAW", G="AWBL". S+S = "BLAWBLAW". find "AWBL"? "BLAWBLAW": "AWBL" appears starting at index? Let's check: "BLAW"+"BLAW" = "BLAWBLAW". Substrings: indices: 0 B L A W, 1 L A W B, 2 A W B L, 3 W B L A, 4 B L A W. At index 2: "AWBL"? Actually index2: A, index3: W, index4: B, index5: L => "AWBL"? That's A,W,B,L so yes "AWBL"? Wait string is "AWBL" means A W B L. That matches index2-5: index2 A, index3 W, index4 B, index5 L => "A W B L". So yes rotation. So output YES. Second test: S="AAABBL", G="LBABAA". S+S = "AAABBLAABBL"? Actually S length 6, S+S = "AAABBLAABBL"? Wait S = "AAABBL". Let's write: A A A B B L. S+S = A A A B B L A A A B B L. G = "LBABAA" = L B A B A A. Does that appear? Check windows: 0: AAA BBL no; 1: AAB BLA? Not match; 2: ABB LAA?; 3: B B L A A A?; 4: B L A A A B?; 5: L A A A B B?; 6: A A A B B L? etc. Probably not. So NO.
Thus matches example.
Thus we just implement.
Potential pitfalls: Input lines may have leading/trailing spaces. Use getline and careful after reading T integer. In C++: after cin>>T, we need cin.ignore() before using getline? Or we can simply use cin>>S>>G since strings are without spaces. Safer: just use cin>>S>>G. Because there are no spaces in strings (they are sequences of letters). So we can read with cin. But after reading integer T, the next tokens are strings. So we can simply loop: for i in range(T): S = input().strip() etc. In Python, input().strip() works.
Thus solution is easy.
But wait: The problem statement might have twist? It says: "He believes that certain gestures help the team to perform well. ... So, if that cyclic order is not intact, he needs to ask them to change the order immediately. ... you need to report whether it is in the correct order or not." It seems to ask: given two strings, check if they are equivalent in a cyclic sense. Yes.
Thus final answer: for each test case, output YES if G is a rotation of S, else NO.
Time complexity: O(n) per test (with string concatenation O(n) and substring search O(n) using naive or KMP? But n <=15 so any method works.
Thus code.
But we need to consider possibility: The strings could have duplicate rotations? However S+S find G works. But note: If S and G are the same length, but G appears in S+S only if it's rotation; however there is a nuance: If S and G have different lengths but they said same length, so ok.
Edge case: S and G empty? If length 0, both empty. S+S is empty, find("") returns something maybe 0? In Python: '' in '' returns True. In C++: string::find returns 0. So YES. But not needed.
Thus code.
Now writing final answer: We'll provide solution explanation and code in Python (or C++). Since they didn't specify language, we can provide both. The instruction says "We need to produce final answer in the assistant's final message." Possibly they want just the solution description? The typical format: Provide problem solution description and code. I'll include explanation and Python code.
Given the context, it's a simple problem. Let's produce final answer.
But the conversation might expect me to output the code? The instruction: "Write a solution...", so I'll produce a complete solution.
Thus I'll write:
Approach:
For each test case, read strings S and G.
If lengths differ, output NO.
Otherwise, check if G is a substring of S+S.
If yes, output YES else NO.
Complexities: O(T * N) where N <= 15.
Now code:
python
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
idx = 1
out = []
for _ in range(t):
if idx >= len(data):
break
s = data[idx]; idx += 1
if idx >= len(data):
break
g = data[idx]; idx += 1
if len(s) != len(g):
out.append("NO")
else:
combined = s + s
if g in combined:
out.append("YES")
else:
out.append("NO")
sys.stdout.write("\n".join(out))
if __name__ == "__main__":
solve()
This works with whitespace separated input (strings have no spaces). Good.
Alternatively we could use input() but this is fine.
Thus answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
idx = 1
out_lines = []
for _ in range(t):
s = data[idx]; idx += 1
g = data[idx]; idx += 1
if len(s) != len(g):
out_lines.append("NO")
else:
if g in (s + s):
out_lines.append("YES")
else:
out_lines.append("NO")
sys.stdout.write("\n".join(out_lines))