/* This solution performs all operations on strings with the mixed radix.
 *
 * Author: Matt Eastman
 */

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define FALSE 0
#define TRUE 1

#define BUF_SIZE 100

typedef struct {
  int valid;
  char value[BUF_SIZE];
} CrazyInt;

static const char* kCrazyInt_Charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

void CrazyInt_Parse(char* value, CrazyInt* out);
void CrazyInt_Copy(CrazyInt* a, CrazyInt* out);

int CrazyInt_Validate(char* value);
int CrazyInt_Sign(CrazyInt* a);
int CrazyInt_Compare(CrazyInt* a, CrazyInt* b);
int CrazyInt_Index(char c);

void CrazyInt_Neg(CrazyInt* a, CrazyInt* out);
void CrazyInt_Add(CrazyInt* a, CrazyInt* b, CrazyInt* out);
void CrazyInt_Sub(CrazyInt* a, CrazyInt* b, CrazyInt* out);
void CrazyInt_Dec(CrazyInt* a);
void CrazyInt_AddSubInternal(CrazyInt* a, CrazyInt* b, int add, CrazyInt* out);

void CrazyInt_Parse(char* value, CrazyInt* out) {
  out->valid = CrazyInt_Validate(value);
  if (out->valid) {
    strcpy(out->value, value);
  }
}

void CrazyInt_Copy(CrazyInt* a, CrazyInt* out) {
  out->valid = a->valid;
  if (out->valid) {
    strcpy(out->value, a->value);
  }
}

int CrazyInt_Validate(char* value) {
  if (value[0] == '-')
    return CrazyInt_Validate(&value[1]);
  int len = strlen(value);
  if (len == 0 || len >= 36)
    return FALSE;

  int i;
  int base = len + 1;
  for (i = 0; i < len; i++) {
    int digit = CrazyInt_Index(value[i]);
    if (digit < 0 || digit >= base)
      return FALSE;
    base--;
  }
  return TRUE;
}

int CrazyInt_Sign(CrazyInt* a) {
  assert(a->valid);
  if (a->value[0] == '-')
    return -1;
  if (strcmp(a->value, "0") == 0)
    return 0;
  return 1;
}

int CrazyInt_Compare(CrazyInt* a, CrazyInt* b) {
  assert(a->valid && b->valid);
  if (CrazyInt_Sign(a) < CrazyInt_Sign(b)) return -1;
  if (CrazyInt_Sign(a) > CrazyInt_Sign(b)) return 1;
  if (CrazyInt_Sign(a) < 0) {
    CrazyInt temp1; CrazyInt_Neg(a, &temp1);
    CrazyInt temp2; CrazyInt_Neg(b, &temp1);
    return CrazyInt_Compare(&temp2, &temp1);
  }
  if (strlen(a->value) < strlen(b->value)) return -1;
  if (strlen(a->value) > strlen(b->value)) return 1;
  return strcmp(a->value, b->value);
}

int CrazyInt_Index(char c) {
  int i;
  for (i = 0; kCrazyInt_Charset[i] != '\0'; i++) {
    if (kCrazyInt_Charset[i] == c)
      return i;
  }
  return -1;
}

void CrazyInt_Neg(CrazyInt* a, CrazyInt* out) {
  out->valid = a->valid;
  if (a->valid) {
    char buf[BUF_SIZE];
    switch (CrazyInt_Sign(a)) {
      case -1:
        CrazyInt_Parse(&a->value[1], out);
        break;
      case 0:
        CrazyInt_Copy(a, out);
        break;
      case 1:
        buf[0] = '-';
        strcpy(&buf[1], a->value);
        CrazyInt_Parse(buf, out);
        break;
      default:
        assert(FALSE);
    }
  }
}

void CrazyInt_Add(CrazyInt* a, CrazyInt* b, CrazyInt* out) {
  if (!a->valid || !b->valid) {
    out->valid = FALSE;
  } else if (CrazyInt_Sign(a) < 0) {
    /* if (a < 0) out = -(-a + -b) */
    CrazyInt temp1; CrazyInt_Neg(a, &temp1);
    CrazyInt temp2; CrazyInt_Neg(b, &temp2);
    CrazyInt temp3; CrazyInt_Add(&temp1, &temp2, &temp3);
    CrazyInt_Neg(&temp3, out);
  } else if (CrazyInt_Sign(b) < 0) {
    /* if (b < 0) out = a - -b */
    CrazyInt temp1; CrazyInt_Neg(b, &temp1);
    CrazyInt_Sub(a, &temp1, out);
  } else {
    CrazyInt_AddSubInternal(a, b, TRUE, out);
  }
}

void CrazyInt_Sub(CrazyInt* a, CrazyInt* b, CrazyInt* out) {
  if (!a->valid || !b->valid) {
    out->valid = FALSE;
  } else if (CrazyInt_Sign(a) < 0) {
    /* if (a < 0) out = -(-a - -b) */
    CrazyInt temp1; CrazyInt_Neg(a, &temp1);
    CrazyInt temp2; CrazyInt_Neg(b, &temp2);
    CrazyInt temp3; CrazyInt_Sub(&temp1, &temp2, &temp3);
    CrazyInt_Neg(&temp3, out);
  } else if (CrazyInt_Sign(b) < 0) {
    /* if (b < 0) out = a + -b */
    CrazyInt temp1; CrazyInt_Neg(b, &temp1);
    CrazyInt_Add(a, &temp1, out);
  } else if (CrazyInt_Compare(a, b) < 0) {
    /* if (a < b) out = -(b - a) */
    CrazyInt temp1; CrazyInt_Sub(b, a, &temp1);
    CrazyInt_Neg(&temp1, out);
  } else {
    CrazyInt_AddSubInternal(a, b, FALSE, out);
  }
}

void CrazyInt_Dec(CrazyInt* a) {
  CrazyInt temp1; CrazyInt_Parse("1", &temp1);
  CrazyInt temp2; CrazyInt_Sub(a, &temp1, &temp2);
  CrazyInt_Copy(&temp2, a);
}

void CrazyInt_AddSubInternal(CrazyInt* a, CrazyInt* b, int add, CrazyInt* out) {
  int buf[BUF_SIZE];
  char char_buf[BUF_SIZE + 1];
  memset(buf, 0, sizeof(buf));
  memset(char_buf, 0, sizeof(char_buf));
  int len = strlen(a->value);
  int offset = BUF_SIZE - len;
  int i;
  for (i = 0; i < len; i++) {
    buf[offset + i] += CrazyInt_Index(a->value[i]);
  }
  len = strlen(b->value);
  offset = BUF_SIZE - len;
  for (i = 0; i < len; i++) {
    if (add)
      buf[offset + i] += CrazyInt_Index(b->value[i]);
    else
      buf[offset + i] -= CrazyInt_Index(b->value[i]);
  }

  int last_non_zero = BUF_SIZE - 1;
  int base = 2;
  for (i = BUF_SIZE - 1; i >= 0; i--) {
    if (buf[i] >= base) {
      buf[i - 1] += buf[i] / base;
      buf[i] %= base;
    }
    while (buf[i] < 0) {
      buf[i] += base;
      buf[i - 1]--;
    }
    char_buf[i] = kCrazyInt_Charset[buf[i]];
    if (buf[i] != 0)
      last_non_zero = i;
    base++;
  }

  CrazyInt_Parse(&char_buf[last_non_zero], out);
}

int main(int argc, char** argv) {
#ifdef DEBUG
  FILE* fp = fopen("input", "r");
  assert(fp != NULL);
  dup2(fileno(fp), fileno(stdin));
#endif

  int num_lines;
  assert(scanf("%d", &num_lines) == 1);
  int i;
  for (i = 0; i < num_lines; i++) {
    char str_a[1024];
    char str_b[1024];
    char op;
    assert(scanf(" %s %c %s", str_a, &op, str_b) == 3);
    CrazyInt a; CrazyInt_Parse(str_a, &a);
    CrazyInt b; CrazyInt_Parse(str_b, &b);
    CrazyInt result; CrazyInt_Parse("", &result);
    if (op == '+') CrazyInt_Add(&a, &b, &result);
    else if (op == '-') CrazyInt_Sub(&a, &b, &result);

    if (result.valid)
      printf("%s\n", result.value);
    else
      printf("Invalid\n");
  }
  return 0;
}
