#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-5;
typedef unsigned long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=1000005;
unsigned int n,tt,cnt,k;
ll m;
int a[MAXN];
int prime[MAXN+1];
void getPrime()
{
memset(prime,0,sizeof(prime));
for(int i = 2;i <= MAXN;i++)
{
if(!prime[i])prime[++prime[0]] = i;
for(int j = 1;j <= prime[0] && prime[j] <= MAXN/i;j++)
{
prime[prime[j]*i] = 1;
if(i % prime[j] == 0)break;
}
}
}
long long factor[100][2];
int fatCnt;
int getFactors(long long x)
{
fatCnt = 0;
long long tmp = x;
for(int i = 1; prime[i] <= tmp/prime[i];i++)
{
factor[fatCnt][1] = 0;
if(tmp % prime[i] == 0 )
{
factor[fatCnt][0] = prime[i];
while(tmp % prime[i] == 0)
{
factor[fatCnt][1] ++;
tmp /= prime[i];
}
fatCnt++;
}
}
if(tmp != 1)
{
factor[fatCnt][0] = tmp;
factor[fatCnt++][1] = 1;
}
return fatCnt;
}
long long factor2[100][2];
int fatCnt2;
int getFactors2(long long x)
{
fatCnt2 = 0;
long long tmp = x;
for(int i = 1; prime[i] <= tmp/prime[i];i++)
{
factor2[fatCnt2][1] = 0;
if(tmp % prime[i] == 0 )
{
factor2[fatCnt2][0] = prime[i];
while(tmp % prime[i] == 0)
{
factor2[fatCnt2][1] ++;
tmp /= prime[i];
}
fatCnt2++;
}
}
if(tmp != 1)
{
factor2[fatCnt2][0] = tmp;
factor2[fatCnt2++][1] = 1;
}
return fatCnt2;
}
int main()
{
int i;
getPrime();
scanf("%d",&tt);
while(tt--)
{
scanf("%d %I64d",&n,&m);
getFactors(n);
bool flag=1;
if(m<n)
{
printf("-1\n");
continue;
}
for(i=0;i<fatCnt;i++)
{
int tot=1;
while(m%factor[i][0]==0)
{
m/=factor[i][0];
factor2[i][0]=factor[i][0];
factor2[i][1]=tot++;
}
}
if(m!=1)
{
flag=0;
}
int Max=0;
for(i=0;i<fatCnt;i++)
{
int temp=factor[i][1];
int tot=0;
while(temp<factor2[i][1])
{
temp<<=1;
tot++;
}
Max=max(Max,tot);
}
if(!flag)
{
printf("-1\n");
}
else
{
printf("%d\n",Max);
}
}
}