#include "bits/stdc++.h"

using namespace std;
#define int long long
#define endl "\n"
#define PII pair<int,int>

void slu() {
    int n, m;
    cin >> n >> m;
    m %= n;
    cout << ((m == 0) ? n : m) << endl;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin >> T;
//    T = 1;
    while (T--)slu();

}