#include <bits/stdc++.h>
using namespace std;
inline void filehandling() {}
vector<long long> vector_in(long long n) {
  vector<long long> v(n);
  for (long long i = 0; i < n; i++) cin >> v[i];
  return v;
}
void vector_out(vector<long long> v) {
  for (long long i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << endl;
}
long long vector_sum(vector<long long> v) {
  long long sum;
  for (long long i = 0; i < v.size(); i++) sum += v[i];
  return sum;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  filehandling();
  long long T;
  cin >> T;
  while (T--) {
    long long v1, v2, n, M = 0;
    cin >> v1;
    cin >> v2;
    cin >> n;
    if (n == 0) {
      cout << v1 << endl;
      continue;
    }
    if (n == 1) {
      cout << v2 << endl;
      continue;
    }
    for (long long i = 0; i < 32; i++) {
      bool boolean = v1 & (1 << i), boolean1 = v2 & (1 << i);
      if (boolean == 1 && boolean1 == 1) {
        if (n % 3 != 2) M |= (1 << i);
      } else if (boolean == 1 && boolean1 == 0) {
        if (n % 3 != 1) M |= (1 << i);
      } else if (boolean == 0 && boolean1 == 1) {
        if (n % 3 != 0) M |= (1 << i);
      }
    }
    cout << M << endl;
  }
}
