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

#include <iostream>
#include <string>

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

using std::string;

class CrazyInt {
 public:
  CrazyInt() : valid_(false) { }

  CrazyInt(const string& value) : valid_(true), value_(value) {
    if (!Validate(value))
      valid_ = false;
  }

  CrazyInt operator-() const {
    if (!valid_) return CrazyInt("");
    switch (Sign()) {
      case -1: return CrazyInt(value_.substr(1));
      case 0: return *this;
      case 1: return CrazyInt("-" + value_);
    }
    assert(false);
  }

  CrazyInt operator+(CrazyInt other) const {
    if (!valid_ || !other.valid_) return CrazyInt("");
    if (Sign() < 0) return -(-*this + -other);
    if (other.Sign() < 0) return *this - -other;
    return AddSubInternal(other, true);
  }

  CrazyInt operator-(CrazyInt other) const {
    if (!valid_ || !other.valid_) return CrazyInt("");
    if (Sign() < 0) return -(-*this - -other);
    if (other.Sign() < 0) return *this + -other;
    if (other > *this) return -(other - *this);
    return AddSubInternal(other, false);
  }

  CrazyInt operator--() {
    *this = *this - CrazyInt("1");
    return *this;
  }

  bool valid() const { return valid_; }

  const string ToString() const {
    if (!valid_) return "Invalid";
    return value_;
  }

  bool operator>(CrazyInt other) const { return Compare(other) > 0; }

 private:
  CrazyInt AddSubInternal(CrazyInt other, bool add) const {
    int buf[buf_size_];
    char char_buf[buf_size_];
    memset(buf, 0, sizeof(buf));
    memset(char_buf, 0, sizeof(char_buf));
    int offset = buf_size_ - value_.size();
    for (int i = 0; i < value_.size(); i++) {
      buf[offset + i] += Index(value_[i]);
    }
    offset = buf_size_ - other.value_.size();
    for (int i = 0; i < other.value_.size(); i++) {
      if (add)
        buf[offset + i] += Index(other.value_[i]);
      else
        buf[offset + i] -= Index(other.value_[i]);
    }

    int last_non_zero = buf_size_ - 1;
    int base = 2;
    for (int 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] = charset_[buf[i]];
      if (buf[i] != 0)
        last_non_zero = i;
      base++;
    }

    return CrazyInt(string(char_buf, buf_size_).substr(last_non_zero));
  }

  int Sign() const {
    assert(valid_);
    assert(!value_.empty());
    if (value_[0] == '-')
      return -1;
    if (value_ == "0")
      return 0;
    return 1;
  }

  int Compare(CrazyInt other) const {
    assert(valid_ && other.valid_);
    if (Sign() < other.Sign()) return -1;
    if (Sign() > other.Sign()) return 1;
    if (Sign() < 0) return -((-*this).Compare(-other));
    if (value_.size() < other.value_.size()) return -1;
    if (value_.size() > other.value_.size()) return 1;
    if (value_ < other.value_) return -1;
    if (value_ > other.value_) return 1;
    return 0;
  }

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

  static bool Validate(const string& value) {
    if (!value.empty() && value[0] == '-') {
      return Validate(value.substr(1));
    }
    if (value.empty() || value.size() >= 36) {
      return false;
    }
    int base = value.size() + 1;
    for (int i = 0; i < value.size(); i++) {
      int digit = Index(value[i]);
      if (digit < 0 || digit >= base) {
        return false;
      }
      base--;
    }
    return true;
  }

  static const char* charset_;
  static const int buf_size_ = 40;
  bool valid_;
  string value_;
};

const char* CrazyInt::charset_ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

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);
  for (int 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(str_a);
    CrazyInt b(str_b);
    CrazyInt result;
    if (op == '+') result = a + b;
    else if (op == '-') result = a - b;
    std::cout << result.ToString() << std::endl;
  }
  return 0;
}
