// Justin Almazan
// 7/23/2025
// Solution to 2025 CP SI @ UCF Contest 5 Problem: Seating

#include <bits/stdc++.h>
using namespace std;

int tt, r, g;

int main() {

    // Number of test cases
    cin >> tt;

    // Process test cases
    while (tt--) {

        // Get num people and rows
        cin >> r >> g;

        // The remainder of g/r is how many seats in the last row are taken.
        // Subtract that from r and you get the number of seats remaining, 
        // unless the remainder was 0. The math below accounts for that
        int res = (r - (g % r)) % r;

        // Answer
        cout << res << endl;
    }

    return 0;
}