#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
struct Node {
  vector<Node*> son;
  Node* parent = NULL;
  int parall = 1;
  char operate = 'S';
  int num;
};
class solve {
 public:
  solve(int n) {
    while (n--) {
      cin >> resister;
      setzero();
      create_tree();
      if (length == 1) {
        cout << "REVOLTING " << resister << '\n';
        continue;
      }
      root->parall = dfs1(root);
      dfs2(root);
      long long single = root->parall * resister;
      cout << "REVOLTING ";
      for (int i = 0; i < q; i++) {
        if (ans[i]->operate == 'Y')
          cout << single << " ";
        else
          cout << "0"
               << " ";
      }
      cout << '\n';
      clear(root);
    }
  }
  void setzero() { q = now = pre = length = 0; }
  void create_tree() {
    Node *parent = root, *son = NULL;
    int num = 0;
    root->num = 0;
    while ((ch = getchar()) != '\n') {
      ++length;
      if (ch == ' ') continue;
      if (ch == '(') {
        now++;
        if (now == pre + 1) {
          son = new Node;
          son->parent = parent;
          son->num = ++num;
          parent->son.push_back(son);
          if (now == 1) {
            parent = son;
          }
        }
        if (now == pre + 2) {
          pre++;
          son = new Node;
          son->parent = parent;
          parent->son.push_back(son);
          parent = son;
          son->num = ++num;
        }
        continue;
      }
      if (ch == ')') {
        now--;
        if (now <= pre) {
          parent = parent->parent;
          pre--;
        }
        continue;
      }
      if (ch == '*') {
        if (length == 1) return;
        son = new Node;
        son->parent = parent;
        parent->son.push_back(son);
        ans[q++] = son;
        son->operate = 'N';
        son->num = ++num;
        continue;
      }
      if (ch == 'S') continue;
      if (ch == 'P') {
        parent->operate = 'P';
        continue;
      }
    }
  }
  int dfs1(Node* now_Node) {
    if (now_Node->operate == 'N') return 1;
    int ssize = now_Node->son.size();
    if (now_Node->operate == 'S') {
      now_Node->parall = dfs1(now_Node->son[0]);
      int temp;
      for (int i = 1; i < ssize; i++) {
        temp = dfs1(now_Node->son[i]);
        if (now_Node->parall > temp) now_Node->parall = temp;
      }
    }
    if (now_Node->operate == 'P') {
      int temp;
      for (int i = 0; i < now_Node->son.size(); i++) {
        temp = dfs1(now_Node->son[i]);
        now_Node->parall += temp;
      }
      now_Node->parall--;
    }
    return now_Node->parall;
  }
  void dfs2(Node* now_Node) {
    if (now_Node->operate == 'N') {
      now_Node->operate = 'Y';
      return;
    }
    int ssize = now_Node->son.size();
    if (now_Node->operate == 'S') {
      Node* temp = now_Node->son[0];
      for (int i = 0; i < ssize; i++)
        if (now_Node->son[i]->parall < temp->parall) temp = now_Node->son[i];
      dfs2(temp);
    }
    if (now_Node->operate == 'P') {
      for (int i = 0; i < ssize; i++) dfs2(now_Node->son[i]);
    }
  }
  void clear(Node* now_node) {
    int ssize = now_node->son.size();
    if (ssize == 0) {
      delete now_node;
      return;
    }
    for (int i = 0; i < ssize; i++) clear(now_node->son[i]);
    if (now_node->parent != NULL)
      delete now_node;
    else
      now_node->son.clear();
  }

 private:
  char ch;
  int now, pre, length;
  Node* const root = new Node;
  Node** ans = new Node*[(unsigned int)1e6];
  long long q;
  long long resister;
};
int main() {
  int n;
  cin >> n;
  solve solution(n);
  return 0;
}
