#include <cstdio>
#include <cmath>
#include <cstdlib>

#define FOR( i, l, r) for(int i = (l)    ; i <  (r); i++)
#define FOR1(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define REV( i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define REV1(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define INC( i, n) FOR( i, 0, n)
#define INC1(i, n) FOR1(i, 1, n)
#define DEC( i, n) REV( i, 0, n)
#define DEC1(i, n) REV1(i, 1, n)

typedef long long   signed int LL;
typedef long long unsigned int LU;

template<typename T> void swap(T &x, T &y) { T t = x; x = y; y = t; return; }
template<typename T> T abs(T x) { return (0 <= x ? x : -x); }
template<typename T> T max(T a, T b) { return (b <= a ? a : b); }
template<typename T> T min(T a, T b) { return (a <= b ? a : b); }
template<typename T> bool setmin(T &a, T b) { if(a <= b) { return false; } else { a = b; return true; } }
template<typename T> bool setmax(T &a, T b) { if(b <= a) { return false; } else { a = b; return true; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

int q;
char s[1001];

bool is_digit(char c) {
	return ('0' <= c && c <= '9');
}

struct Node {
	bool table[100];
	int remain;
	bool func;
	int c_n;
	int child[2];
	int parent;
	bool enable;
};

void solve() {
	int len = 0;
	while(s[len] != '\0') { len++; }
	
	Node node[1000];
	INC(i, len) {
		INC(j, 100) { node[i].table[j] = true; }
		node[i].remain = 100;
		node[i].func = false;
		node[i].c_n = 0;
		node[i].child[0] = len;
		node[i].child[1] = len;
		node[i].parent = -1;
		node[i].enable = false;
	}
	
	int list[500], lv = 0;
	
	bool was_digit = false;
	INC(i, len) {
		bool func = (s[i] == '_' || s[i] == '^');
		bool numb = ( is_digit(s[i]) && ! was_digit );
		if(func || numb) {
			node[i].enable = true;
			if(lv != 0) {
				node[i].parent = list[lv - 1];
				Node & pa = node[ node[i].parent ];
				pa.child[ pa.c_n++ ] = i;
				if(pa.c_n == 2) { lv--; }
			}
			if(func) {
				node[i].func = true;
				list[lv++] = i;
			}
		}
		was_digit = is_digit(s[i]);
	}
	
	INC(i, len) {
		if(s[i] == '(' || node[i].func) { continue; }
		
		bool remain_flag = false;
		if(is_digit(s[i])) {
			if(node[i].enable) {
				if(s[i] == '0') {
					FOR(j, 1, 100) { node[i].table[j] = false; }
					node[i].remain = 1;
					remain_flag = true;
				} else {
					INC(j, 100) {
						if( ! (j == (s[i] - '0') || j / 10 == (s[i] - '0') ) ) {
							if(node[i].table[j]) { node[i].remain--; remain_flag = true; }
							node[i].table[j] = false;
						}
					}				
				}
			} else {
				INC(j, 100) {
					if( ! (j == (s[i - 1] - '0') * 10 + (s[i] - '0') ) ) {
						if(node[i - 1].table[j]) { node[i - 1].remain--; remain_flag = true; }
						node[i - 1].table[j] = false;
					}
				}
			}
		} else { // ',', ')', '?'
			if(i != 0 && is_digit(s[i - 1]) && node[i - 1].enable) {
				FOR(j, 10, 100) {
					if(node[i - 1].table[j]) { node[i - 1].remain--; remain_flag = true; }
					node[i - 1].table[j] = false;
				}
			}
		}
		
		// parent
		int pos = (node[i].enable ? i : i - 1);
		pos = node[pos].parent;
		while(remain_flag && pos != -1) { //printf("pos: %d\n", pos);
		//while(pos != -1) { printf("pos: %d\n", pos);
			remain_flag = false;
			
			int &c0 = node[pos].child[0];
			int &c1 = node[pos].child[1];
			if(s[pos] == '_') { // min
				int max0, max1;
				DEC(j, 100) { if(node[c0].table[j]) { max0 = j; break; } }
				DEC(j, 100) { if(node[c1].table[j]) { max1 = j; break; } }
				setmin(max0, max1);
				INC(j, 100) {
					if( ! ( j <= max0 && (node[c0].table[j] || node[c1].table[j]) ) ) {
						if(node[pos].table[j]) { node[pos].remain--; remain_flag = true; }
						node[pos].table[j] = false;
					}
				}
			} else { // max
				int min0, min1;
				INC(j, 100) { if(node[c0].table[j]) { min0 = j; break; } }
				INC(j, 100) { if(node[c1].table[j]) { min1 = j; break; } }
				setmax(min0, min1);
				INC(j, 100) {
					if( ! ( min0 <= j && (node[c0].table[j] || node[c1].table[j]) ) ) {
						if(node[pos].table[j]) { node[pos].remain--; remain_flag = true; }
						node[pos].table[j] = false;
					}
				}
			}
			
			pos = node[pos].parent;
		}
		
		if(node[0].remain == 1) {
			int ans;
			INC(j, 100) { if(node[0].table[j]) { ans = j; break; } }
			printf("%d %d\n", ans, i + 1);
			return;
		}
	}
	
	
	
	printf("error: [ %s ] \n", s);
	exit(1);
	
	INC(i, 18) { printf("%c: remain: %d\n", s[i], node[i].remain); }
	// INC(j, 100) { if(node[0].table[j]) { printf("%d ", j); } } printf("\n");
	
}

int main() {
	scanf("%d", &q);
	INC(i, q) { scanf("%s", s); solve(); }
	return 0;
}


Measured memory_bytes: 256