def generate_nine(): s = [] for i in range(99, 0, -1): s.append(f"{i} bottle{'s' if i != 1 else ''} of beer on the wall, {i} bottle{'s' if i != 1 else ''} of beer.") s.append(f"Take one down and pass it around, {i-1 if i-1 != 0 else 'no more'} bottle{'s' if (i-1) != 1 else ''} of beer on the wall.\n") s.append("No more bottles of beer on the wall, no more bottles of beer.") s.append("Go to the store and buy some more, 99 bottles of beer on the wall.") return '\n'.join(s) nine_output = generate_nine() L_song = len(nine_output) L_hello = len("Hello, World!") def check_hq9_combination(s): current = 0 code = [] n = len(s) while current < n: if s.startswith("Hello, World!", current): code.append('H') current += L_hello elif s.startswith(nine_output, current): code.append('9') current += L_song else: return None return ''.join(code) def main(): import sys input = sys.stdin.read().split() N = int(input[0]) S = input[1] # Case 1: S is the 99 song if S == nine_output: print("9") return # Case 2: S is "Hello, World!" if S == "Hello, World!": print("H") return # Case 3: S is "Q" if S == "Q": print("Q") return # Case 4: S is all Q's and length is a square if all(c == 'Q' for c in S): m = int(len(S) ** 0.5) if m * m == len(S): print('Q' * m) return # Case 5: Check if S can be split into H and 9 parts code_h9 = check_hq9_combination(S) if code_h9 is not None: print(code_h9) return # Case 6: Check if S itself is a valid code (consists of H, Q, 9, +) valid_chars = {'H', 'Q', '9', '+'} if all(c in valid_chars for c in S): # Generate the output of code S output = [] for c in S: if c == 'H': output.append("Hello, World!") elif c == 'Q': output.append(S) elif c == '9': output.append(nine_output) # '+' does nothing generated = ''.join(output) if generated == S: print(S) return # If none of the above print(-1) if __name__ == "__main__": main()