/* Problem URL: https://codeforces.com/contest/2093/problem/D */

#include <bits/stdc++.h>

using namespace std;

#define V vector

#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)

#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)

#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()

using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;

using ll = long long;

using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;

template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
    os << vec[0];
    for (size_t i = 1; i < vec.size(); i++) {
        os << ' ' << vec[i];
    }
    os << '\n';
    return os;
}

template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
    for (auto &i : vec) {
        is >> i;
    }
    return is;
}

template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
    for (auto &i : vec) {
        os << i[0];
        for (size_t j = 1; j < i.size(); j++) {
            os << ' ' << i[j];
        }
        os << '\n';
    }
    return os;
}

template<class v>
auto operator>>(istream  &is, vector<vector<v>> &vec)->istream& {
    for (auto &i : vec) {
        for (auto &j : i) {
            is >> j;
        }
    }
    return is;
}

using pl = pair<ll, ll>;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        ll n, q;
        cin >> n >> q;

        while (q--) {
            string op;
            cin >> op;

            function<ll(int, pl, pl, pl, pl, ll, ll, ll, ll)> find_by_cord = [&](int n, pl p1, pl p2, pl p3, pl p4, ll x, ll y, ll tx, ll ty)
            {
                if (n == 1) {
                    if (x == tx && y == ty) {
                        return p1.first;
                    }
                    if (x == tx && y + 1 == ty) {
                        return p2.first;
                    }
                    if (x + 1 == tx && y == ty) {
                        return p3.first;
                    }
                    return p4.first;
                }

                ll add = 1LL << (n - 1);
                ll mini;
                ll add1 = (1LL << (2 * (n - 1) - 2)) - 1;
                ll xn = 0;
                ll yn = 0;
                if (tx < x + add && ty < y + add) {
                    mini = p1.first;
                    xn = x;
                    yn = y;
                }

                if (tx < x + add && ty >= y + add) {
                    mini = p2.first;
                    xn = x;
                    yn = y + add;
                }

                if (ty < y + add && tx >= x + add) {
                    mini = p3.first;
                    xn = x + add;
                    yn = y;
                }

                if (tx >= x + add && ty >= y + add) {
                    mini = p4.first;
                    xn = x + add;
                    yn = y + add;
                }

                return find_by_cord(n - 1,  make_pair(mini, mini + add1),
                                            make_pair(mini + add1 * 3 + 3, mini + add1 * 4 + 3),
                                            make_pair(mini + add1 * 2 + 2, mini + add1 * 3 + 2),
                                            make_pair(mini + add1 + 1, mini + add1 * 2 + 1),
                                    xn, yn, tx, ty);
            };

            function<pl(int, pl, pl, pl, pl, ll, ll, ll)> find_by_value = [&](int n, pl p1, pl p2, pl p3, pl p4, ll x, ll y, ll v){
                if (n == 1) {
                    if (v == p1.first) {
                        return make_pair(x, y);
                    }
                    if (v == p2.first) {
                        return make_pair(x, y + 1);
                    }
                    if (v == p3.first) {
                        return make_pair(x + 1, y);
                    }
                    return make_pair(x + 1, y + 1);
                }

                ll add = 1LL << (n - 1);
                ll mini;
                ll add1 = (1LL << (2 * (n - 1) - 2)) - 1;
                ll xn = 0;
                ll yn = 0;
                if (v >= p1.first && v <= p1.second) {
                    mini = p1.first;
                    xn = x;
                    yn = y;
                }

                if (v >= p2.first && v <= p2.second) {
                    mini = p2.first;
                    xn = x;
                    yn = y + add;
                }

                if (v >= p3.first && v <= p3.second) {
                    mini = p3.first;
                    xn = x + add;
                    yn = y;
                }

                if (v >= p4.first && v <= p4.second) {
                    mini = p4.first;
                    xn = x + add;
                    yn = y + add;
                }

                return find_by_value(n - 1, make_pair(mini, mini + add1),
                                            make_pair(mini + add1 * 3 + 3, mini + add1 * 4 + 3),
                                            make_pair(mini + add1 * 2 + 2, mini + add1 * 3 + 2),
                                            make_pair(mini + add1 + 1, mini + add1 * 2 + 1),
                                    xn, yn, v);
 
            };

            if (op == "->") {
                ll x, y;
                cin >> x >> y;

                ll add = (1LL << (2 * n - 2));
                cout << find_by_cord(n, make_pair(1, add),
                                        make_pair(add * 3 + 1, add * 4),
                                        make_pair(add * 2 + 1, add * 3),
                                        make_pair(add + 1, add * 2),
                                     1, 1, x, y);
                cout << '\n';
            } else {
                ll v;
                cin >> v;

                ll add = (1LL << (2 * n - 2));
                auto [x, y] = find_by_value(n, make_pair(1, add),
                                        make_pair(add * 3 + 1, add * 4),
                                        make_pair(add * 2 + 1, add * 3),
                                        make_pair(add + 1, add * 2),
                                     1, 1, v);

                cout << x << ' ' << y << '\n';
            }
        }
    }
}
