#include<bits/stdc++.h>
using namespace std;
int t;
bool ifp(int x)
{
int n = sqrt(x);
for(int i=2;i<=n;i++)
if(x % i == 0) return 0;
return 1;
}
int solve(int a,int b)
{
if(a == 1 || b == 1) return a + b - 1;
int sum = 0;
if(a < b) swap(a, b);
while(!ifp(a))
{
if(__gcd(a, b) == 1) sum++;
a--;
}
if(a <= b) sum--;
sum += a + b - 1;
return sum;
}
int main()
{
scanf("%d",&t);
while(t--)
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",solve(a, b));
}
for(int i=1;i<=100;i++)
if(ifp(i)) cout<<i<<" ";
return 0;
}