#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e5+5; int t,n,m; int main(){ scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); if(m%n==0) printf("%d\n",n); else printf("%d\n",m%n); } return 0; }