#include #include #include #include #include #include #include #include #include #include #include #include #include #include #pragma warning(disable:4996) typedef long long ll; #define MIN(a, b) ((a)>(b)? (b): (a)) #define MAX(a, b) ((a)<(b)? (b): (a)) #define LINF 9223300000000000000 #define INF 2140000000 const long long MOD = 1000000007; //const long long MOD = 998244353; using namespace std; int main(int argc, char* argv[]) { int Q; scanf("%d", &Q); int q; for(q=0; q z; int curr=a; int cnt=0; while(curr) { z.push_back(curr%c); if(curr%c) cnt++; curr/=c; cnt++; } if(z[(int)z.size()-1]==1) cnt--; printf("%lld\n", (ll)(cnt-1)*b); } } return 0; }