#include <bits/stdc++.h>
#define ll long long int
#define ld long double
using namespace std;
int problem_solve(int n, int t, int d)
{
n -= (t * d);
n = max(0, n);
return n;
}
void mainSolve()
{
int x, y;
cin >> x >> y;
int ans1 = problem_solve(500, x, 2) + problem_solve(1000, x + y, 4);
int ans2 = problem_solve(1000, y, 4) + problem_solve(500, x + y, 2);
int ans = max(ans1, ans2);
cout << ans << endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--)
{
mainSolve();
}
return 0;
}