#include <bits/stdc++.h>

#define int long long
#define endl '\n'
[[maybe_unused]]const int INF = 1e16 + 50, MOD = 10007;
[[maybe_unused]] typedef std::pair<int, int> pii;

void solve() {
    int h, m;
    std::cin >> h >> m;
    if (h == 0)
        std::cout << 12 << ' ' << m << ' ' << "am" << endl;
    else if (h == 12) {
        std::cout << h << " " << m << ' ' << "pm" << endl;
    } else if (h > 12) {
        std::cout << h - 12 << ' ' << m << ' ' << "pm" << endl;
    }else {
        std::cout << h << ' ' << m << ' ' << "am" << endl;
    }
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr), std::cout.tie(nullptr);
    int Lazy_boy_ = 1;
    std::cin >> Lazy_boy_;
    while (Lazy_boy_--)
        solve();
    return 0;
}