import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.List; import java.util.Queue; public class E2 { InputStream is; PrintWriter out; String INPUT = ""; void solve() { outer: for(int T = ni();T >= 1;T--){ int n = ni(); reg = ns().toCharArray(); len = reg.length; pos = 0; Node[] nodes = expr(); if(pos != len)throw new AssertionError(pos); // for(Node a : all){ // tr(a); // } boolean[][] dist = new boolean[501][all.size()]; dist[0][nodes[0].id] = true; Queue<int[]> q = new ArrayDeque<int[]>(); q.add(new int[]{0, nodes[0].id}); while(!q.isEmpty()){ int[] curs = q.poll(); int d = curs[0]; Node cur = all.get(curs[1]); for(Edge e : cur.next){ int dd = e.c == '!' ? 0 : 1; if(d + dd <= 500 && !dist[d+dd][e.to.id]){ dist[d+dd][e.to.id] = true; q.add(new int[]{d+dd, e.to.id}); } } } for(int i = n;i <= 500;i++){ if(dist[i][nodes[1].id]){ out.println(i); continue outer; } } out.println(-1); } } static char[] reg; static int pos, len; static List<Node> all = new ArrayList<Node>(); // (src,dst) static Node[] expr() { List<Node[]> items = new ArrayList<Node[]>(); items.add(item()); while(pos < len){ if(reg[pos] == ')')break; if(reg[pos] != '|')throw new AssertionError(pos); pos++; items.add(item()); } if(items.size() == 1){ return items.get(0); }else{ Node src = new Node(); Node dst = new Node(); for(Node[] item :items){ src.linkEpsilon(item[0]); item[1].linkEpsilon(dst); } return new Node[]{src, dst}; } } static Node[] item() { Node[] ret = clus(); while(pos < len){ if(reg[pos] == ')' || reg[pos] == '|')break; Node[] nex = clus(); ret[1].linkEpsilon(nex[0]); ret[1] = nex[1]; } return ret; } static Node[] clus() { Node[] unit = unit(); if(pos < len && reg[pos] == '*'){ pos++; Node src = new Node(); Node dst = new Node(); src.linkEpsilon(dst); src.linkEpsilon(unit[0]); unit[1].linkEpsilon(unit[0]); unit[1].linkEpsilon(dst); return new Node[]{src, dst}; } return unit; } static Node[] unit() { if(pos == len)throw new AssertionError(pos); if(reg[pos] == '('){ pos++; Node[] expr = expr(); if(!(pos < len && reg[pos] == ')'))throw new AssertionError(pos); pos++; return expr; }else{ if(reg[pos] == ')' || reg[pos] == '|' || reg[pos] == '*')throw new AssertionError(pos); Node src = new Node(); Node dst = new Node(); src.link(dst, reg[pos]); pos++; return new Node[]{src, dst}; } } static class Edge { public char c; public Node to; public Edge(char c, Node to) { this.c = c; this.to = to; } public String toString() { return c + ":" + to.id; } } static int idbase = 0; static class Node { public List<Edge> next; public int id; public Node() { all.add(this); next = new ArrayList<Edge>(); id = idbase++; } public void link(Node to, char c) { next.add(new Edge(c, to)); } public void linkEpsilon(Node to) { next.add(new Edge('!', to)); } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Node [next="); builder.append(next); builder.append(", id="); builder.append(id); builder.append("]"); return builder.toString(); } } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new E2().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }