#P13961. [ICPC 2023 Nanjing R] 华丽收场

[ICPC 2023 Nanjing R] 华丽收场

Description

In Pigeland, Slay the Pig\textit{Slay the Pig} is the most popular roguelike game where players use card decks to challenge a boss called Evil Potato Lord.

The general game rules are as follows:

  • Players start with a set of initial cards in their hand and a draw pile arranged from top to bottom.
  • At any given time, the player's hand and the draw pile are the only places where cards are present.
  • Players can play cards from their hands, and each played card will lead to this card being discarded first, and then triggering certain effects.
  • The player can only use the next card after all the effects of the previously played card have been triggered.

In this problem, for simplicity, we will only consider the effect of drawing cards. The rules for drawing cards are as follows:

  • When using cards that can draw some other cards, players will draw a certain number of cards from the top of the draw pile into their hands in sequential order.
  • Players have a hand size limit of kk, and at any given moment, the number of cards in a player's hand cannot exceed the limitation kk.
  • When a player attempts to draw a card from the top of the draw pile and their hand already contains kk cards, the drawn card will be discarded from the draw pile without triggering any effects and not added to the hand.
  • When a player attempts to draw a card but the draw pile is empty, nothing will happen.

Decks based on the key card Grand Finale\textit{Grand Finale} are the most powerful deck in the game. Once this card is played, it will cause massive damage to all enemies and often leads to victory in the game. However, to play the Grand Finale card, there are strict conditions. That is, the player's draw pile must be empty, which means that all cards must either be played, discarded, or in the player's hand.

Bie-Bot is the smartest pig in Pigeland only after the Mysterious Oscar, and he is also playing a Grand Finale\textit{Grand Finale}-based deck, which can contain the following four types of cards:

:::align{center} :::

  • Grand Finale\textit{Grand Finale}: The most powerful card in the game, playing it will lead to an easy victory, and there is exactly one Grand Finale\textit{Grand Finale} in Bie-Bot's deck. This card can only be played if there are no cards in the draw pile.
  • Quick Slash\textit{Quick Slash}: Using this card, you will draw one card from the draw pile.
  • Backflip\textit{Backflip}: Using this card, you will draw two cards from the draw pile.
  • Wound\textit{Wound}: A status card that, once it is in your hand, can not be played.

At the beginning of the game, Bie-Bot was lucky to have drawn the only one Grand Finale\textit{Grand Finale} in his starting hand, and he also knows that each card in the draw pile from top to bottom. Now, his goal is to successfully play the Grand Finale\textit{Grand Finale} card. Bie-Bot wants to know, under his optimal strategy, what is the minimum hand size limit kk required for him to achieve his goal. As the third smartest player in Pigeland, can you help him out?

More formally, you are given a string SHS_{H} of length nn representing Bie-Bot's starting hand and a string SPS_{P} of length mm representing the draw pile from top to bottom. Both strings consist of uppercase letters G, Q, B and W, indicating that the card at the corresponding position in the starting hand or draw pile is Grand Finale\textit{Grand Finale}, Quick Slash\textit{Quick Slash}, Backflip\textit{Backflip}, or Wound\textit{Wound}, respectively. Bie-Bot can use the cards in his hand according to the rules mentioned above. Please output the minimum hand size limit kk (knk \geq n) such that Bie-Bot can finally play the Grand Finale\textit{Grand Finale} or state that there is no such kk.

Input Format

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers nn and mm (1n,m25001 \leq n, m \leq 2500) indicating the number of cards in Bie-Bot's starting hand and draw pile.

The second line contains a string SHS_{H} of length nn to represent Bie-Bot's starting hand, consisting of uppercase letters G, Q, B, and W. It's guaranteed that there is exactly one G in SHS_{H}.

The third line of the input contains a string SPS_{P} of length mm to represent Bie-Bot's draw pile from top to bottom, consisting of uppercase letters Q, B, and W.

It is guaranteed that the sum of (n+m)(n + m) of all test cases will not exceed 5×1045 \times 10^4.

Output Format

For each test case output one line containing one integer indicating the minimum hand size kk (knk \geq n) that can lead to successfully playing Grand Finale\textit{Grand Finale}, or IMPOSSIBLE\texttt{IMPOSSIBLE} if it can't be played.

2
2 6
BG
BQWBWW
4 6
GQBW
WWWWQB
3
IMPOSSIBLE

Hint

We express the situation with "hand/deck" string. For the first sample test case, one optimal strategy is:

  • BG/BQWBWW B\overset{B}{\longrightarrow} BQG/WBWW
  • BQG/WBWW Q\overset{Q}{\longrightarrow} BWG/BWW
  • BWG/BWW B\overset{B}{\longrightarrow} BWG/W (Discard one "W" here due to hand size limit)
  • BWG/W B\overset{B}{\longrightarrow} WWG/\emptyset (Only draw one card here because there is no more card in the draw pile)
  • WWG/\emptyset G\overset{G}{\longrightarrow} Successfully playing Grand Finale\textit{Grand Finale}!