import java.io.BufferedReader;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Scanner;
import java.util.StringTokenizer;


public class Main {
	
//	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	
	static int getCN(char ch) {
		if (ch >= '0' && ch <= '9') return ch - '0';
		return ch - 'A' + 10;
	}
	
	static boolean valid(String s) {
		int cnt = 2;
		for (int i = s.length() - 1; i >= 0; i--) {
			if (i == 0 && s.charAt(i) == '-') continue;
			char ch = s.charAt(i);
			if (!(ch >= '0' && ch <= '9') && !(ch >= 'A' && ch <= 'Z')) {
				return false;
			}
			int num = getCN(ch);
			if (num >= cnt) {
				return false;
			}
			cnt++;
		}
		return true;
	}
	
	static BigInteger getInt(String s) {
		BigInteger ret = BigInteger.ZERO;
		int n = s.length();
		int rad = s.length() + 2;
		int sgn = 1;
		int i = 0;
		if (s.charAt(0) == '-') {
			sgn = -1;
			i++;
			rad--;
		}
		for ( ; i < n; i++, rad--) {
			char c = s.charAt(i);
			ret = ret.multiply(BigInteger.valueOf(rad));
			ret = ret.add(BigInteger.valueOf(getCN(c)));
		}
		if (sgn < 0) {
			ret = ret.multiply(BigInteger.valueOf(-1));
		}
		return ret;
	}
	
	static char toCh(BigInteger x) {
		String s = x.toString();
		int y = Integer.parseInt(s);
		if (y < 10) return (char)(y + '0');
		return (char) (y - 10 + 'A');
	}
	
	static String turnBack(BigInteger x) {
		int sgn = 1;
		if (x.compareTo(BigInteger.valueOf(0)) < 0) {
			sgn = -1;
			x.multiply(BigInteger.valueOf(-1));
		}
		BigInteger tmp = x;
		String ret = new String();
		for (int i = 2; i < 36; i++) {
			ret = toCh(tmp.mod(BigInteger.valueOf(i))) + ret;
			tmp = tmp.divide(BigInteger.valueOf(i));
			if (tmp.equals(BigInteger.valueOf(0))) break;
		}
		if (!tmp.equals(BigInteger.valueOf(0))) {
			return "Invalid";
		} else {
			if (sgn < 0) ret = "-" + ret;
			return ret;
		}
	}
		
	public static void main(String[] args) throws NumberFormatException, IOException {
		Scanner in = new Scanner(System.in);
		int Tc = in.nextInt();
		for (int ri = 0; ri < Tc; ri++) {
			String sa = in.next();
			String op = in.next();
			String sb = in.next();
			if (valid(sa) && valid(sb)) {	
				BigInteger a = getInt(sa);
				BigInteger b = getInt(sb);
				BigInteger ret;
				if (op.charAt(0) == '+') {
					ret = a.add(b);
				} else {
					ret = a.subtract(b);
				}
				System.out.println(turnBack(ret));
			} else {
				System.out.println("Invalid");
			}
		}
	}
}

