#include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define ll long long
void solve(){
int n;cin>>n;
if(n%3==0){cout<<0<<endl;return;}
if(n%5==0){cout<<0<<endl;return;}
int t=n/3;
int i=0;int ans=100;
while(i<=t){
ans=min(ans,n-3*i-5*((n-3*i)/5));i++;
}
cout<<ans<<endl;
}
int main() {
int t;
cin>>t;
while(t--)solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBsbCBsb25nIGxvbmcKCnZvaWQgc29sdmUoKXsKaW50IG47Y2luPj5uOwppZihuJTM9PTApe2NvdXQ8PDA8PGVuZGw7cmV0dXJuO30KaWYobiU1PT0wKXtjb3V0PDwwPDxlbmRsO3JldHVybjt9CmludCB0PW4vMzsKaW50IGk9MDtpbnQgYW5zPTEwMDsKd2hpbGUoaTw9dCl7CiAgICBhbnM9bWluKGFucyxuLTMqaS01Kigobi0zKmkpLzUpKTtpKys7Cn0KICAgY291dDw8YW5zPDxlbmRsOwogICAgCn0KCmludCBtYWluKCkgewoJaW50IHQ7CgljaW4+PnQ7Cgl3aGlsZSh0LS0pc29sdmUoKTsKCXJldHVybiAwOwp9