#include<iostream>
#include<string>
#include<queue>
#include<vector>
using namespace std;
#define MAX_N 1000
struct Tree { int num, left, right, par; };

Tree V[MAX_N]; bool Range[MAX_N][100];
string A; vector<int>vec;
vector<int>_K[100];

void init() {
	for (int i = 0; i < 100; i++)_K[i].clear(); vec.clear();
	for (int i = 0; i < MAX_N * 100; i++)Range[i / 100][i % 100] = true;
	for (int i = 0; i < 100; i++)_K[i].push_back(i);
	for (int i = 1; i < 10; i++) {
		for (int j = 0; j < 10; j++) _K[i].push_back(i * 10 + j);
	}
	for (int i = 0; i < MAX_N; i++)V[i] = Tree{ 0,0,0,0 };
}
void preorder(int u) {
	if (u == -1) { return; }
	if (V[u].left == -1)vec.push_back(u);
	preorder(V[u].left);
	preorder(V[u].right);
}
bool isnum(string S) {
	for (int i = 0; i < S.size(); i++) {
		if (S[i] < '0' || S[i] > '9')return false;
	}
	return true;
}
pair<string, string>division(string S) {
	S = S.substr(2, S.size() - 3);
	int cnt = 0; bool _OK = false;
	string _S1, _S2;
	for (int i = 0; i < S.size(); i++) {
		if (S[i] == '(')cnt++;
		if (S[i] == ')')cnt--;
		if (S[i] == ',' && cnt == 0)_OK = true;
		else {
			if (_OK == true)_S2 += S[i];
			if (_OK == false)_S1 += S[i];
		}
	}
	return make_pair(_S1, _S2);
}
void Tree_build(string S) {
	queue<pair<string, int>>Q;
	Q.push(make_pair(S, -1)); int L = 0, R = 1;
	while (!Q.empty()) {
		string S1 = Q.front().first; int a1 = Q.front().second; Q.pop();
		if (isnum(S1) == true) {
			V[L] = Tree{ stoi(S1),-1,-1,a1 }; L++;
		}
		else {
			int P1 = -1; if (S1[0] == '^') { P1 = -2; }
			V[L] = Tree{ P1,R,R + 1,a1 };
			pair<string, string>pa1 = division(S1);
			Q.push(make_pair(pa1.first, L)); R++;
			Q.push(make_pair(pa1.second, L)); R++; L++;
		}
	}
}
void update(int t) {
	while (true) {
		t = V[t].par; if (t == -1)break;
		int lmin = 100, rmin = 100, lmax = 0, rmax = 0;
		bool RR[100]; for (int i = 0; i < 100; i++)RR[i] = Range[t][i];
		for (int i = 0; i < 100; i++) {
			if (Range[V[t].left][i] == false)continue;
			lmin = min(lmin, i); lmax = max(lmax, i);
		}
		for (int i = 0; i < 100; i++) {
			if (Range[V[t].right][i] == false)continue;
			rmin = min(rmin, i); rmax = max(rmax, i);
		}
		bool PP[100]; for (int i = 0; i < 100; i++)PP[i] = false;
		if (V[t].num == -2) {
			int MIN = max(lmin, rmin);
			for (int i = MIN; i < 100; i++)if (Range[V[t].left][i] == true)PP[i] = true;
			for (int i = MIN; i < 100; i++)if (Range[V[t].right][i] == true)PP[i] = true;
			for (int i = 0; i < 100; i++) { Range[t][i] = PP[i]; }
		}
		if (V[t].num == -1) {
			int MAX = min(lmax, rmax);
			for (int i = 0; i <= MAX; i++)if (Range[V[t].left][i] == true)PP[i] = true;
			for (int i = 0; i <= MAX; i++)if (Range[V[t].right][i] == true)PP[i] = true;
			for (int i = 0; i < 100; i++) { Range[t][i] = PP[i]; }
		}
		bool Henka = false;
		for (int i = 0; i < 100; i++) { if (RR[i] != Range[t][i])Henka = true; }
		if (Henka == false)return;
	}
}
pair<int, int> search(string S) {
	string T = ""; int counts = 0;
	for (int i = 0; i < S.size(); i++) {
		if (S[i] >= '0' && S[i] <= '9') {
			T += S[i];
			bool PP[100]; for (int j = 0; j < 100; j++)PP[j] = false;
			for (int j = 0; j < _K[stoi(T)].size(); j++)PP[_K[stoi(T)][j]] = true;
			for (int j = 0; j < 100; j++)Range[vec[counts]][j] = PP[j];
			update(vec[counts]);
		}
		else if (T != "") {
			for (int j = 0; j < 100; j++)Range[vec[counts]][j] = false;
			Range[vec[counts]][stoi(T)] = true; update(vec[counts]);
			T = ""; counts++;
		}
		int res = 0, resid = 0;
		for (int j = 0; j < 100; j++) {
			if (Range[0][j] == true) { res++; resid = j; }
		}
		if (res == 1)return make_pair(i + 1, resid);
	}
}
int main() {
	int QQ; cin >> QQ;
	for (int h = 0; h < QQ; h++) {
		cin >> A; init();
		Tree_build(A.substr(0, A.size() - 1));
		preorder(0);
		pair<int, int>ans = search(A);
		cout << ans.second << ' ' << ans.first << endl;
	}
	return 0;
}

Measured memory_bytes: 384