#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <map>
using namespace std;
map<int,int> arr;
int min3(int a,int b,int c)
{
return a>b?((b<c)?b:c):((a<c)?a:c);
}
int min2(int a,int b)
{
return a>b?b:a;
}
int solve(int n)
{
if(n==1) return 0;
if(n==2 || n==3 ) return 1;
if(arr.find(n)!=arr.end()) return arr[n];
int ans;
if(n%2==0 && n%3==0 )
ans=(1+min3(solve(n-1),solve(n/2),solve(n/3)));
else if(n%2==0)
ans=(1+min2(solve(n-1),solve(n/2)));
else if(n%3==0)
ans=(1+min2(solve(n-1),solve(n/3)));
else
ans=(1+solve(n-1));
arr[n]=ans;
return ans;
}
int main()
{
//printf("Hello World!\n");
int t;
scanf("%d",&t);
arr[1]=0;
arr[2]=1;
arr[3]=1;
while(t--)
{
int n;
scanf("%d",&n);
printf("%d\n",solve(n));
}
}
CSNpbmNsdWRlIDxzdGRpby5oPgoJI2luY2x1ZGUgPHN0ZGxpYi5oPgoJI2luY2x1ZGUgPHV0aWxpdHk+CgkjaW5jbHVkZSA8bWFwPgoJdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCgltYXA8aW50LGludD4gYXJyOwoKCWludCBtaW4zKGludCBhLGludCBiLGludCBjKQoJewoJCXJldHVybiBhPmI/KChiPGMpP2I6Yyk6KChhPGMpP2E6Yyk7Cgl9CgoJaW50IG1pbjIoaW50IGEsaW50IGIpCgl7CgkJcmV0dXJuIGE+Yj9iOmE7Cgl9CgoJaW50IHNvbHZlKGludCBuKQoJewoJCWlmKG49PTEpIHJldHVybiAwOwoJCWlmKG49PTIgfHwgbj09MyApIHJldHVybiAxOwoJCWlmKGFyci5maW5kKG4pIT1hcnIuZW5kKCkpIHJldHVybiBhcnJbbl07CgkJaW50IGFuczsKCQlpZihuJTI9PTAgJiYgbiUzPT0wICkKCQkgIGFucz0oMSttaW4zKHNvbHZlKG4tMSksc29sdmUobi8yKSxzb2x2ZShuLzMpKSk7CgkJZWxzZSBpZihuJTI9PTApCgkJCWFuc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