#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const long long OO = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2000100;
char s[maxn + 5];
int N;
vector<int> chd[maxn + 5];
int ty[maxn + 5];
int id[maxn + 5];
long long f[maxn + 5];
long long ans[maxn + 5];
void calc(int first) {
  if (ty[first] == 2) {
    f[first] = 1;
    return;
  }
  if (ty[first] == 0)
    f[first] = OO;
  else
    f[first] = 0;
  for (auto second : chd[first]) {
    calc(second);
    if (ty[first] == 0)
      chkmin(f[first], f[second]);
    else
      f[first] += f[second];
  }
}
void dfs(int first, long long w) {
  if (ty[first] == 2) {
    ans[id[first]] = w;
    return;
  }
  if (ty[first] == 0) {
    int Miny = -1;
    for (auto second : chd[first])
      if (!~Miny || f[second] < f[Miny]) Miny = second;
    dfs(Miny, w);
  } else {
    for (auto second : chd[first]) dfs(second, w);
  }
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int r;
    scanf("%d", &r);
    char c;
    int n = 0;
    while (isprint(c = getchar()))
      if (c != ' ') s[n++] = c;
    N = 0;
    vector<int> stk;
    ty[N] = 0;
    chd[N].clear();
    stk.push_back(N++);
    int m = 0;
    for (int i = (0), i_end_ = (n); i < i_end_; ++i) {
      if (s[i] == '(') {
        ty[N] = 0;
        chd[N].clear();
        chd[stk.back()].push_back(N);
        stk.push_back(N++);
      } else if (s[i] == '*') {
        ty[N] = 2;
        id[N] = m++;
        chd[N].clear();
        chd[stk.back()].push_back(N);
        N++;
      } else if (s[i] == ')')
        stk.pop_back();
      else if (s[i] == 'S')
        ty[stk.back()] = 0;
      else if (s[i] == 'P')
        ty[stk.back()] = 1;
    }
    calc(0);
    for (int i = (0), i_end_ = (m); i < i_end_; ++i) ans[i] = 0;
    dfs(0, r * f[0]);
    printf("REVOLTING ");
    for (int i = (0), i_end_ = (m); i < i_end_; ++i) printf("%lld ", ans[i]);
    printf("\n");
  }
  return 0;
}
