/*
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
return 0;
}
*/
/*
#include <stdio.h>
#define size 50000
int top;
char stack[size];
int isfull()
{
if(top==size-1)
{
return 1;
}
else
{
return 0;
}
}
int isempty()
{
if(top==-1)
{
return 1;
}
else
{
return 0;
}
}
void init()
{
top=-1;
}
void push(char data)
{
if(isfull())
{
return 0;
}
top++;
stack[top]=data;
}
char pop()
{
char k;
if(isempty())
{
return 0;
}
k=stack[top];
stack[top]=0;
top--;
return k;
}
int main()
{
char str[50000];
int i;
init();
scanf("%s",str);
for(i=0; i<strlen(str); i++)
{
if(str[i]=='(')
{
push(str[i]);
}
else
{
pop();
}
}
if(str[0]==')'){
printf("bad");
}
else if(isempty())
{
printf("good");
}
else
{
printf("bad");
}
}
*/
/*
#include <stdio.h>
#define size 100
int top;
int stack[size];
int isfull()
{
if(top==size-1){
return 1;
}
else{
return 0;
}
}
int isempty()
{
if(top==-1){
return 1;
}
else{
return 0;
}
}
void init()
{
top=-1;
}
void push(int data)
{
if(isfull()){
return 0;
}
top++;
stack[top]=data;
}
int pop()
{
int k;
if(isempty()){
return 0;
}
k=stack[top];
stack[top]=k;
top--;
return k;
}
void view()
{
for(int i = 0; i<=top; i++)
{
printf("%d ",stack[i]);
}
printf("\n");
}
int main()
{
char str[50];
int i;
init();
scanf("%s",str);
for(i=0;i<strlen(str);i++){
if(str[i]=='(' || str[i]=='[')
{
push(str[i]);
}
else if(str[i]==']')
{
if(str[i-1]=='[')
{
pop();
push(3);
}
}
else if(str[i]==')' && str[i-1]=='(')
{
pop();
push(2);
}
if(str[i]==')'){
pop();
pop();
push(2);
}
else if(str[i]==']'){
pop();
pop();
push(3);
}
if(str[i-1]=='['&&str[i+1]==']'){
push(str[i]*3);
}
if(str[i-1]=='('&&str[i+1]==')'){
push(str[i]*2);
}
if(str[i] str[i+1])
}
}
*/
#include <Stdio.h>
struct n
{
int student;
int apple;
} ;
int main()
{
int n,i,sum;
struct n st[100];
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&st[i].student);
scanf("%d",&st[i].apple);
sum+=st[i].apple-(st[i].apple/st[i].student*st[i].student);
printf("%d\n",sum);
}
}



