[Question]
  [
Let's define a **grouping** as a flat list, which is either:
* just `0`
* 2 groupings followed by the literal integer `2`
* 3 groupings followed by `3`
* 4 groupings followed by `4`
* etc. (for all integers \$ \ge 2 \$)
Note that `1` is not included in this definition, because otherwise groupings could be infinite.
For example, these are valid groupings:
* `0`
* `0 0 2`
* `0 0 0 3`
* `0 0 0 2 2`
* `0 0 0 0 0 2 3 0 0 0 4 2 0 2`
If it helps, here is a more visual representation of the last two groupings above, using parentheses to show how they're constructed:
```
(0 ((0 0) 2) 2)
((0 ((0 0 (0 0 2) 3) 0 0 0 4) 2) 0 2)
```
## Task
Given a positive integer `n`, output all possible **groupings** which contain exactly `n` `0`s.
For example, for `n = 3`, there are 3 possible groupings:
* `0 0 0 3`
* `0 0 0 2 2`
* `0 0 2 0 2`
## Rules
* Outputs may be in any order, but duplicate outputs are not allowed.
* You may use any [standard I/O method](https://codegolf.meta.stackexchange.com/q/2447)
* [Standard loopholes](https://codegolf.meta.stackexchange.com/q/1061) are forbidden
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes wins
## Test cases
```
input -> outputs
1 -> [0]
2 -> [0 0 2]
3 -> [0 0 0 2 2], [0 0 0 3], [0 0 2 0 2]
4 -> [0 0 0 0 2 2 2], [0 0 0 0 2 3], [0 0 0 0 3 2], [0 0 0 0 4], [0 0 0 2 0 2 2], [0 0 0 2 0 3], [0 0 0 2 2 0 2], [0 0 0 3 0 2], [0 0 2 0 0 2 2], [0 0 2 0 0 3], [0 0 2 0 2 0 2]
5 -> [0 0 0 0 0 2 2 2 2], [0 0 0 0 0 2 2 3], [0 0 0 0 0 2 3 2], [0 0 0 0 0 2 4], [0 0 0 0 0 3 2 2], [0 0 0 0 0 3 3], [0 0 0 0 0 4 2], [0 0 0 0 0 5], [0 0 0 0 2 0 2 2 2], [0 0 0 0 2 0 2 3], [0 0 0 0 2 0 3 2], [0 0 0 0 2 0 4], [0 0 0 0 2 2 0 2 2], [0 0 0 0 2 2 0 3], [0 0 0 0 2 2 2 0 2], [0 0 0 0 2 3 0 2], [0 0 0 0 3 0 2 2], [0 0 0 0 3 0 3], [0 0 0 0 3 2 0 2], [0 0 0 0 4 0 2], [0 0 0 2 0 0 2 2 2], [0 0 0 2 0 0 2 3], [0 0 0 2 0 0 3 2], [0 0 0 2 0 0 4], [0 0 0 2 0 2 0 2 2], [0 0 0 2 0 2 0 3], [0 0 0 2 0 2 2 0 2], [0 0 0 2 0 3 0 2], [0 0 0 2 2 0 0 2 2], [0 0 0 2 2 0 0 3], [0 0 0 2 2 0 2 0 2], [0 0 0 3 0 0 2 2], [0 0 0 3 0 0 3], [0 0 0 3 0 2 0 2], [0 0 2 0 0 0 2 2 2], [0 0 2 0 0 0 2 3], [0 0 2 0 0 0 3 2], [0 0 2 0 0 0 4], [0 0 2 0 0 2 0 2 2], [0 0 2 0 0 2 0 3], [0 0 2 0 0 2 2 0 2], [0 0 2 0 0 3 0 2], [0 0 2 0 2 0 0 2 2], [0 0 2 0 2 0 0 3], [0 0 2 0 2 0 2 0 2]
6 -> [0 0 0 0 0 0 2 2 2 2 2], [0 0 0 0 0 0 2 2 2 3], [0 0 0 0 0 0 2 2 3 2], [0 0 0 0 0 0 2 2 4], [0 0 0 0 0 0 2 3 2 2], [0 0 0 0 0 0 2 3 3], [0 0 0 0 0 0 2 4 2], [0 0 0 0 0 0 2 5], [0 0 0 0 0 0 3 2 2 2], [0 0 0 0 0 0 3 2 3], [0 0 0 0 0 0 3 3 2], [0 0 0 0 0 0 3 4], [0 0 0 0 0 0 4 2 2], [0 0 0 0 0 0 4 3], [0 0 0 0 0 0 5 2], [0 0 0 0 0 0 6], [0 0 0 0 0 2 0 2 2 2 2], [0 0 0 0 0 2 0 2 2 3], [0 0 0 0 0 2 0 2 3 2], [0 0 0 0 0 2 0 2 4], [0 0 0 0 0 2 0 3 2 2], [0 0 0 0 0 2 0 3 3], [0 0 0 0 0 2 0 4 2], [0 0 0 0 0 2 0 5], [0 0 0 0 0 2 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 3], [0 0 2 0 0 2 0 0 3 2], [0 0 2 0 0 2 0 0 4], [0 0 2 0 0 2 0 2 0 2 2], [0 0 2 0 0 2 0 2 0 3], [0 0 2 0 0 2 0 2 2 0 2], [0 0 2 0 0 2 0 3 0 2], [0 0 2 0 0 2 2 0 0 2 2], [0 0 2 0 0 2 2 0 0 3], [0 0 2 0 0 2 2 0 2 0 2], [0 0 2 0 0 3 0 0 2 2], [0 0 2 0 0 3 0 0 3], [0 0 2 0 0 3 0 2 0 2], [0 0 2 0 2 0 0 0 2 2 2], [0 0 2 0 2 0 0 0 2 3], [0 0 2 0 2 0 0 0 3 2], [0 0 2 0 2 0 0 0 4], [0 0 2 0 2 0 0 2 0 2 2], [0 0 2 0 2 0 0 2 0 3], [0 0 2 0 2 0 0 2 2 0 2], [0 0 2 0 2 0 0 3 0 2], [0 0 2 0 2 0 2 0 0 2 2], [0 0 2 0 2 0 2 0 0 3], [0 0 2 0 2 0 2 0 2 0 2]
```
  
[Answer]
# [Haskell](https://www.haskell.org/), 59 bytes
Returns a list of groupings in lexicographic order.
```
(!0)
0!1=[[]]
n!s=[k:g|k<-[0^n..s],k/=1,g<-(n-0^k)!(s+1-k)]
```
[Try it online!](https://tio.run/##BcFBDsIgEADAe18BN4hdhIsHU57gC3BrSKNrs5SQrse@vTjzzcLvUjrFZzfa28HrEFNCHKqWmPhOB0@Q/FydExz5GsNIE5gKfmarjVwCsMW@5bWqqLbcHi9l2r7WnyOrUnDuhv1cPiWTdFha@wM "Haskell ‚Äì Try It Online")
The possible groupings can be constructed from left to right if you keep track of two values: the remaining number of `0`s to place, and the number of currently open groupings. When all `0`s are placed and there is a single open grouping, we have constructed a valid output.
This is implemented by the function `(!)` which takes the number of `0`s as a left argument `n` and the open groupings as `s`. At each step we can insert a `0` if `n>0` and any integer in `[2..s]` to close multiple open groupings.
```
-- main function: (!) with 0 open groupings
(!0)
-- base case: no more 0s to place, a single grouping left
0!1=[[]]
-- k is the number to insert
-- 0^n == if n==0 then 1 else 0
n!s=[k:g|k<-[0^n..s],k/=1,g<-(n-0^k)!(s+1-k)]
```
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), version 12.3.1, 44 bytes
```
Groupings[#,2~Range~#,Length/@#~Level~All&]&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n6Zg@9@9KL@0IDMvvThaWceoLigxLz21TlnHJzUvvSRD30G5zie1LDWnzjEnRy1W7b9LfnRAUWZeSXSmgq6dQlp0ZmysjkJ1po6CkY6CaW3sfwA "Wolfram Language (Mathematica) ‚Äì Try It Online")
Yes, Mathematica has a built-in for groupings.
But the version on TIO (Mathematica 12.0.1) gives the wrong result for input `1`. The version I'm using (Mathematica 12.3.1) works correctly.
[Answer]
# [JavaScript (V8)](https://v8.dev/), ~~¬†77¬†~~ 76 bytes
Prints all groupings as comma-separated strings.
```
f=(n,s=0,g=0,h=k=>k|n>1?f(k?h(k-1)|n:n+--k,s+[,k+1],g-k):!g&&print(s))=>h(g)
```
[Try it online!](https://tio.run/##JYpBCoMwEEX3PcV0YxImgWZTijZ6EHEhhUQ7MJVE3NSePU3r4vM@vPcctzE94rysZrvl7J1kndxFh7LJkWtp59Z2XlI3STJW7VwzGkM6Ya8J7aCDIVWfQ1UtceZVJqVcO8mgsn9FyeDANsBwd3AtRFTwPgEcreh/XgCWAEEMQjXFecl/Hk25n/wF "JavaScript (V8) ‚Äì Try It Online")
### Commented
```
f = (                  // f is a recursive function taking:
  n,                   //   n = input
  s = 0,               //   s = output 'string' (initially the number 0)
  g = 0,               //   g = number of groupings - 1
  h = k =>             //   h = helper recursive function taking a counter k
    k | n > 1 ?        //     if k is not equal to 0 or n is greater than 1:
      f(               //       recursive call to f:
        k ?            //         if k is not equal to 0:
          h(k - 1)     //           do a recursive call to h with k - 1
          | n          //           and pass n unchanged (*)
        :              //         else:
          n + --k,     //           set k = -1 and decrement n
        s + [, k + 1], //         append a comma followed by k + 1 to s
        g - k          //         subtract k from g
                       //         (this actually increments g if k = -1)
      )                //       end of recursive call
    :                  //     else:
      !g && print(s)   //       print s if g = 0
) => h(g)              // initial call to h with k = g
```
*(\*) This relies on the fact that `h()` will always eventually return a zero'ish value. And that's why we use `!g && print(s)` rather than `g || print(s)`.*
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 47 bytes
```
ÔºÆŒ∏‚äûœÖ‚ü¶‚Å∞‚üßÔº¶œÖÔº¶‚Å∫‚Ä¶‚Å∞‚Äπ‚ÑñŒπ‚Å∞Œ∏‚Ä¶¬∑¬≤‚ÅªÔº¨ŒπŒ£Œπ‚äûœÖ‚Å∫Œπ‚ü¶Œ∫‚üßÔº©Œ¶œÖ‚Åº‚Åº‚ÑñŒπ‚Å∞Œ∏‚ÅªÔº¨ŒπŒ£Œπ
```
[Try it online!](https://tio.run/##fY4xC8IwEIV3f8WNF4hQBCfHolCoUnQsHWKJbTBNbZLr349n1cXBG@5x8N17r@2Vb0dlUyrcg@KJhqv2OIndqqLQI0mos4av2@gBScCilaWAZ@U6jZmEUoeA@UguopGQCQmT4FW4ljEz6ze4kXA0jv9K7brYo2HkQgMrD3zDFmd2qe@NeHXwhl1zFSIejI3cjJn9RMoG/MhP8L8Udkxpm9azfQI "Charcoal ‚Äì Try It Online") Link is to verbose version of code. Explanation: A simplification of @ovs's method, where the number of `0`s to place is simply the desired number of `0`s minus the current number of `0`s, and the number of open groupings is the length minus the sum.
```
ÔºÆŒ∏
```
Input the desired number of `0`s.
```
‚äûœÖ‚ü¶‚Å∞‚üßÔº¶œÖ
```
Start a breadth first search with a single `0`. (I can't use an empty list because `Sum([])` isn't `0` in Charcoal.)
```
Ôº¶‚Å∫‚Ä¶‚Å∞‚Äπ‚ÑñŒπ‚Å∞Œ∏‚Ä¶¬∑¬≤‚ÅªÔº¨ŒπŒ£Œπ
```
Loop over the concatenation of two ranges, one from `0` to whether more `0`s are needed (so either an empty range or a range containing just `0`), one from `2` to the difference between the length and the sum inclusive.
```
‚äûœÖ‚Å∫Œπ‚ü¶Œ∫‚üß
```
Append the value to the grouping and push it to the search list.
```
Ôº©Œ¶œÖ‚Åº‚Åº‚ÑñŒπ‚Å∞Œ∏‚ÅªÔº¨ŒπŒ£Œπ
```
Output those groupings with one grouping and the correct number of `0`s.
[Answer]
# [Core Maude](http://maude.cs.illinois.edu/w/index.php/The_Maude_System), 209 + 15 = 224 bytes
```
mod G is inc LIST{Nat}*(op size to{_]). ops({_})([_]): Nat ~> Nat . var A B
N :[Nat]. eq{N}=[N]N . eq 0 1 = 0 . eq[0]= nil . eq[s N]= 0[N]. crl A B N =>
A{A]B s sd(N,{A])if{A]> 1 /\{A]< N /\ A B =[{A B]]. endm
```
To get the list of groupings for \$n\$, run the following Maude command with the \$G\$ module loaded.
```
search{ùëõ}=>* A .
```
The groupings will be the listed assignments to \$A\$. So, for example, for \$n = 3\$ run
```
search{3}=>* A .
```
(The bytes of this command¬†‚Äî minus the bytes to specify \$n\$¬†‚Äî have been included in the total.)
### Example Session
```
Maude> search{1}=>* A .
search in G : {1} =>* A .
Solution 1 (state 0)
states: 1  rewrites: 4 in 0ms cpu (0ms real) (~ rewrites/second)
A --> 0
No more solutions.
states: 1  rewrites: 15 in 0ms cpu (0ms real) (~ rewrites/second)
Maude> search{2}=>* A .
search in G : {2} =>* A .
Solution 1 (state 0)
states: 1  rewrites: 4 in 0ms cpu (0ms real) (~ rewrites/second)
A --> 0 0 2
No more solutions.
states: 1  rewrites: 128 in 0ms cpu (0ms real) (~ rewrites/second)
Maude> search{3}=>* A .
search in G : {3} =>* A .
Solution 1 (state 0)
states: 1  rewrites: 5 in 0ms cpu (0ms real) (~ rewrites/second)
A --> 0 0 0 3
Solution 2 (state 1)
states: 2  rewrites: 181 in 0ms cpu (0ms real) (~ rewrites/second)
A --> 0 0 2 0 2
Solution 3 (state 2)
states: 3  rewrites: 218 in 0ms cpu (0ms real) (~ rewrites/second)
A --> 0 0 0 2 2
No more solutions.
states: 3  rewrites: 1467 in 1ms cpu (1ms real) (1467000 rewrites/second)
Maude> search{4}=>* A .
search in G : {4} =>* A .
Solution 1 (state 0)
states: 1  rewrites: 6 in 0ms cpu (0ms real) (~ rewrites/second)
A --> 0 0 0 0 4
-- SNIP --
Solution 11 (state 10)
states: 11  rewrites: 4520 in 3ms cpu (3ms real) (1506666 rewrites/second)
A --> 0 0 0 0 2 2 2
No more solutions.
states: 11  rewrites: 15508 in 12ms cpu (12ms real) (1292333 rewrites/second)
Maude> search{5}=>* A .
search in G : {5} =>* A .
Solution 1 (state 0)
states: 1  rewrites: 7 in 0ms cpu (0ms real) (~ rewrites/second)
A --> 0 0 0 0 0 5
-- SNIP --
Solution 45 (state 44)
states: 45  rewrites: 72386 in 42ms cpu (41ms real) (1723476 rewrites/second)
A --> 0 0 0 0 0 2 2 2 2
No more solutions.
states: 45  rewrites: 149258 in 59ms cpu (58ms real) (2529796 rewrites/second)
Maude> search{6}=>* A .
search in G : {6} =>* A .
Solution 1 (state 0)
states: 1  rewrites: 8 in 0ms cpu (0ms real) (~ rewrites/second)
A --> 0 0 0 0 0 0 6
-- SNIP --
Solution 197 (state 196)
states: 197  rewrites: 853311 in 216ms cpu (215ms real) (3950513 rewrites/second)
A --> 0 0 0 0 0 0 2 2 2 2 2
No more solutions.
states: 197  rewrites: 1340298 in 329ms cpu (328ms real) (4073854 rewrites/second)
```
### Ungolfed
```
mod G is
    inc LIST{Nat} * (op size to {_]) .
    ops ({_}) ([_]) : Nat ~> Nat .
    var A B N : [Nat] .
    eq {N} = [N] N .
    eq 0 1 = 0 .
    eq [0] = nil .
    eq [s N] = 0 [N] .
    crl A B N => A {A] B s sd(N, {A]) if {A] > 1 /\ {A] < N /\ A B = [{A B]] .
endm
```
This solution uses Maude's rewriting feature, where Maude will search using a rewrite system to find all solutions to an problem of the form \$t\_1 \to\_R^\* t\_2\$, or in this case, \$\{n\} \to\_G^\* A\$, where \$\to\_G^\*\$ means zero or more applications of rules in \$G\$, and \$A\$ is a variable (so essentially *any* term). The single rule picks a run of zeros and groups them, so Maude will explore every way of grouping starting from \$\{n\} \approx\_G 0 \; 0 \, \cdots \, 0 \; n\$.
We use odd function names (`{_}`, `[_]`, and `{_]`) to take advantage of Maude's odd tokenization rules. `{_}` creates the initial grouping; `[_]` produces a list of zeroes; `{_]` is the renamed `size` function for lists.
[Answer]
# [Python](https://www.python.org), 100 bytes
```
f=lambda n:[k+j+R for i in range(1,n)for*j,J in f(i)for k in f(n-i)for R in[[J,2],[J+1]][:i]]or[[0]]
```
[Attempt This Online!](https://ato.pxeger.com/run?1=dZjBbtNAEIbvvnNf-RQTt0p31gUFhQfokavxIZAE3KZOlASkCvEkXHqBG0_CE8DT4MRxdnf-30Kl3W_Wv8fj2Zld__i1fTp83jTPzz-_HFZXr_8uVrP1_PHDYm6aafkwvh-_M6vNztSmbsxu3nxajm7yJmvRy_v87ghXo_o4NA_doLnqhu_aYVne5bbKy7vxTVWV07qqNruynFTV-V5_6sftZncw-6d9sjUzs_w6X4_Sb-m4Bdf7w6JurnfL-WKUtb-26_nH5Sg1V29NmqfTMg1g3v4Lx-ZEAvC-acdVi8Zp9T3NkgQe6VU2TYwx213dHEZ1vl42o-ODZfm-dXC56Aaz2Xm0Lesqy0_sTXdJ1j3S878Xv2-OPrZPmdjuDzMxtkrkMmiHLcj7gfR_2m6iCyaepoaTj0DCocRW5wdW38iGN-uUJ5Ej4dBqRy1xtnO4iBw-uxy71UHRSHCWi4Ggkmgdp2cUcbxoECGQFoNp44BeQqZnqcD2EZjAi9NIUE20lqCSi4H190QoGgnOgqQhiQPJQxLI6iTq54CeSiYfWUhIvVj0haIvs-E7QCgaCc5yMVDx8FA0Io7AoqILa2hxnS5ObtUCuywxnfi9QRiGxdZhh5Asug4TXVh8R1hoJNxbYb4K81TQT8cUHeoVOOtWV53BojVQuAaKFylg1tAi1mGiC_E8wkIjWtS8AXRJcesx-EuLnDcQbVLs-pQjmBS9HrMGwSQcQuERERYPYdEQjIXwSAiLg_AoCIuBY6oONR1TLDQKyv-QARodyd8eO4SQvz0muo7NJA15sCnTxjzQnEmDDlYHNxBt2qwvqYqbBKoO-duvfCIB-Ru0ySEDbDAmLCbQ0KPWymbTzQs0M28gMaFNPjTwzREKkYbvsSAECfF3YFgQCpvpNCLR6zFoksgJi5vwqAmLmfCIOSbg8HKHF0fbpCEDbB6hbnjsEApXFqbr2MxCI1I3QgPoQt3wGPwldSM0EG26ASZ1I9gJcsw26UyCbPxp3QgNsGmHrPeYHgSI16RuhFWdOkliMnA4CMobw0Sf1A2PBSFIRKuBG-CwQuJ4SWaEJI5BijI84CQ9YNE4Bq-bYaUvPAQeC0JhM51G8OgegyY8tlz-118q8JE9ZgfEUKJ_UVD_YgMcFlX9C7FDKFxZmK5jMwuNoP7FBtBV9S_E4C_Uv9hAtNXLClsEwcLVhWkLV3YISf2LDXBIV4kbYnrwJ15D_Yu7E3WSf13rf3MD_8CAQlD_QiwIQYLWv9gAHydIHKH-hbUJHCT1L-5o1En6QYXGkdQ_q3_OX6L_Aw)
### How?
One big recursive list comprehension. Conceptually: Cut off the tree's lefttmost branch (from the root node) and do recursion on both bits. Technicality: The right bit is the original tree with the left bit removed, while the left bit discards the root branch so as not to violate the no nonbranching nodes requirement. IOW, the left bit hangs one level lower than the right bit. Except when the original tree had only two root branches. Then both hang low.
[Answer]
# [Curry (PAKCS)](https://www.informatik.uni-kiel.de/%7Epakcs/), 63 bytes
```
(!0)
0!1=[]
n!s=anyOf[k:n!(s+1-k)|k<-[2..s]]?n>0&>0:(n-1)!(s+1)
```
[Try it online!](https://tio.run/##Sy4tKqrULUjMTi7@/z/NVkPRQJPLQNHQNjqWK0@x2DYxr9I/LTrbKk9Ro1jbUDdbsybbRjfaSE@vODbWPs/OQM3OwEojT9dQEyyv@T83MTNPwVYhTcHkPwA "Curry (PAKCS) ‚Äì Try It Online")
Based on [ovs's Haskell answer](https://codegolf.stackexchange.com/a/242785/9288). There are two differences:
* The `^` operator is not supported by the Curry version on TIO.
* An expression in Curry can be **non-deterministic**, i.e. having multiple values at the same time. Here I define a non-deterministic function `f`, whose return values are all the possible groupings.
---
# [Curry (PAKCS)](https://www.informatik.uni-kiel.de/%7Epakcs/), 68 bytes
```
(([]:iterate g[0])!!)
g a=a++[0,2]
g(a++[b])|b>1=g a++[b]?a++[0,b+1]
```
[Try it online!](https://tio.run/##Sy4tKqrULUjMTi7@/z/NVkMjOtYqsyS1KLEkVSE92iBWU1FRkytdIdE2UVs72kDHKJYrXQPETIrVrEmyM7QFSoF59hD5JG3D2P@5iZl5CrYKaQom/wE "Curry (PAKCS) ‚Äì Try It Online")
Another non-deterministic function that returns all the possible groupings at the same time.
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), ~~86~~ 84 bytes
```
f=(n,x=[0],i=0)=>n?x.flatMap((_,j)=>j>i?[]:f(n-!j,[...u=x,j+=!!j],i-j+1)):i-1?[]:[u]
```
[Try it online!](https://tio.run/##ZctBDsIgEIXhvbdgN6RAWnVVM@0JPAEhhtRimBBobGu4PeLWbr//PbIfu05vv2wypudcikOIIqNujfDYchzimJULdrvbBeAhqBINftSmdxAlI6GVUjtmQQ0yRvUmqek4773sfiu9m3I7TSmuKcwqpBc4qPlPzge5HOTKefkC "JavaScript (Node.js) ‚Äì Try It Online")
[Answer]
# Python3, 205 bytes:
```
f=lambda x:''.join(f(i)if list==type(i)else'0'for i in x)+str(l:=len(x))*(l>1)
def s(n,c=[]):
 if n:
  for i in[c+[0],*[[],[[0,c]]][j:=len(c)>1],*[[],[[c,0]]][j]]:
   yield from s(n-1,i)
 else:yield f(c)
```
[Try it online!](https://tio.run/##PY9BboQwDEXX5RTeETNpBeqiFVJ6EcsLGpLWKBNQkgWcnga1nZVlff3n5@0o32t8fd/SeXoTpvvnPME@tu3LskpUXgmKhyC5GFOOzdXdhezavvVrAgGJsOMtl6TCaIKLakfsVPgYsJmdh6yitoYYxwYqJ9YB/0WyN@pZd0SsiXptmZnU8ouxWBGP0Or@ChfmCwCHuDCDT@v9OvA8aMEGLq3xL6n18@GXpvjl1KDfqsTTliQWRV2ujzCePw)
]