A2. Yukari Yakumo and the Boundary Stones (Hard Version)
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem.

There is a significant difference between the easy and hard versions of this problem. Please read the statement carefully.

Yukari Yakumo has placed $$$n$$$ boundary stones on the ground, each with power $$$1$$$ or $$$2$$$. You do not know the powers of the stones; you only know $$$n$$$.

In each test case, you may write a spell $$$s$$$ consisting of the characters + and - ($$$1 \le |s| \le 999$$$) and give it to Yukari. Yukari chooses a prefix of $$$s$$$ (possibly empty) and then performs every character of this prefix in order.

When performing the $$$i$$$-th character of the prefix, Yukari chooses a stone that is currently on the ground. The chosen stone changes as follows:

  • if $$$s_i = \mathtt{+}$$$, its power increases by $$$1$$$; however, if its power is $$$2$$$, then its power does not change, and a new stone of power $$$2$$$ appears on the ground instead (the original stone stays on the ground);
  • if $$$s_i = \mathtt{-}$$$, its power decreases by $$$1$$$; however, if its power is $$$1$$$, then the stone disappears.

If there is no stone on the ground, the remaining characters cannot be performed, and the number of stones on the ground at that moment is $$$0$$$.

Yukari wants the number of stones on the ground after performing the chosen prefix to be as large as possible. If several prefixes achieve this maximum, she chooses the shortest of them and tells you its length $$$k$$$.

Knowing $$$k$$$, you have to output the sum of the initial powers of the $$$n$$$ stones.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the number of stones.

For each test case, after reading $$$n$$$, you should begin the interaction and find the answer before proceeding to the next test case.

Interaction

The interaction for each test case is as follows:

  1. your program reads a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of stones;
  2. your program prints a single line containing a string $$$s$$$ of length from $$$1$$$ to $$$999$$$ consisting of the characters + and - — the spell;
  3. your program reads a single integer $$$k$$$ ($$$0 \le k \le |s|$$$) — the length of the prefix chosen by Yukari;
  4. your program prints a single integer — the sum of the initial powers of the $$$n$$$ stones (it is always between $$$n$$$ and $$$2n$$$).

After printing each query do not forget to output the end of line and flush$$$^{\text{∗}}$$$ the output. Otherwise, you will get Idleness limit exceeded verdict.

If, at any interaction step, you read $$$-1$$$ instead of valid data, your solution must exit immediately. This means that your solution will receive Wrong answer because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream.

The powers of the stones are fixed before the interaction starts and do not depend on the spell.

Hacks are disabled in this problem.

$$$^{\text{∗}}$$$To flush, use:

  • fflush(stdout) or cout.flush() in C++;
  • sys.stdout.flush() in Python;
  • see the documentation for other languages.
Example
Interaction log
< 2
< 2
> +-
< 0
> 2
< 3
> +-
< 1
> 5
Note

In the first test case, $$$n = 2$$$ and the powers of the stones are $$$1$$$ and $$$1$$$ (your program does not know this, but the interaction depends on it). You print +-.

  • The empty prefix leaves $$$2$$$ stones on the ground.
  • The prefix + turns a stone of power $$$1$$$ into a stone of power $$$2$$$, so there are still $$$2$$$ stones on the ground.
  • The prefix +- turns a stone of power $$$2$$$ back into a stone of power $$$1$$$, so there are still $$$2$$$ stones on the ground.
The maximum number of stones on the ground is $$$2$$$, and the prefixes of lengths $$$0$$$, $$$1$$$, and $$$2$$$ all achieve it; the shortest of them has length $$$0$$$, hence $$$k = 0$$$. The sum of the initial powers is $$$2$$$.

In the second test case, $$$n = 3$$$ and the powers of the stones are $$$1$$$, $$$2$$$, $$$2$$$. You print +-.

  • The empty prefix leaves $$$3$$$ stones on the ground.
  • The prefix + copies a stone of power $$$2$$$, so there are $$$4$$$ stones on the ground.
  • The prefix +- turns a stone of power $$$2$$$ into a stone of power $$$1$$$, so there are still $$$4$$$ stones on the ground.
The maximum number of stones on the ground is $$$4$$$, and the shortest prefix achieving it has length $$$1$$$, hence $$$k = 1$$$. The sum of the initial powers is $$$1 + 2 + 2 = 5$$$.