import java.io.*;
import java.util.*;
public class Main {
// nextFront(s) = (s+1) + floor((s+1)/2)
static long nextFront(long s) {
long u = s + 1;
return u + (u / 2);
}
// Which cow is at position 0 immediately after time t
static long frontCow(long t) {
if (t <= 1) return 0; // at times 0 and 1, cow 0 is in front
while (t % 3 != 2) {
long r = t % 3;
if (r == 0) {
// t = 3m -> predecessor s = 2m - 1
t = 2 * (t / 3) - 1;
} else { // r == 1
// t = 3m+1 -> predecessor s = 2m
t = 2 * (t / 3);
}
if (t <= 1) return 0;
}
// t = 3c - 1 => c = (t+1)/3
return (t + 1) / 3;
}
// Position of cow c immediately after time t
static long positionOfCow(long c, long t) {
if (c == 0) {
// cow 0 front times start at 0
long s = 0;
while (s < t) {
long ns = nextFront(s);
if (ns > t) return ns - t;
s = ns;
}
return 0; // s == t
}
// before time 2c, cow c never moves
if (t < 2 * c) return c;
long s = 3 * c - 1; // first time cow c reaches front
if (t <= s) return s - t; // moving toward first front
// advance front times until we bracket t
while (s < t) {
long ns = nextFront(s);
if (ns > t) return ns - t;
s = ns;
}
return 0; // s == t
}
// Cow at position x immediately after time t
static long cowAtPosition(long x, long t) {
if (x > (t / 2)) return x; // untouched suffix
return frontCow(t + x); // x steps later it reaches the front
}
FastScanner fs
= new FastScanner
(System.
in); int Q = fs.nextInt();
StringBuilder out = new StringBuilder();
for (int i = 0; i < Q; i++) {
int type = fs.nextInt();
long a = fs.nextLong();
long t = fs.nextLong();
long ans;
if (type == 1) {
long c = a;
ans = positionOfCow(c, t);
} else {
long x = a;
ans = cowAtPosition(x, t);
}
out.append(ans).append('\n');
}
System.
out.
print(out.
toString()); }
// Fast input
static class FastScanner {
private final byte[] buffer = new byte[1 << 16];
private int ptr = 0, len = 0;
in = is;
}
if (ptr >= len) {
len = in.read(buffer);
ptr = 0;
if (len <= 0) return -1;
}
return buffer[ptr++];
}
int c;
do {
c = readByte();
} while (c <= ' ');
long sign = 1;
if (c == '-') {
sign = -1;
c = readByte();
}
long val = 0;
while (c > ' ') {
val = val * 10 + (c - '0');
c = readByte();
}
return val * sign;
}
return (int) nextLong();
}
}
}