// Update the code below to solve the problem
#include <iostream>
#include <string>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int N, M;
cin>>N>>M;
int pairNeeded = 2*N;
int extra = max(0, pairNeeded-M);
cout<<extra<<endl;
}
return 0;
}
// Update the code below to solve the problem
#include <iostream>
#include <string>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int N, M;
cin>>N>>M;
if(N<M)
cout<<N;
else
cout<<M+(N-M)*2;
cout<<endl;
}
return 0;
}