#include #include #include #include #include #include using namespace std; //resize(vector, v1,v2,...,vn); v[v1][v2]...[vn] template void resize(vector& vec, const H head){vec.resize(head);} template void resize(vector& vec, const H& head, const T ... tail){ vec.resize(head); for(auto& v: vec) resize(v, tail...); } string ltos(long long x){ stringstream ss; ss << x; return ss.str(); } long long cnt_kadomatsu_digit(string s){ vector>> dp; resize(dp, 11,11, 2); dp[10][10][0] = 1; for(int i=0; i>> dp_; resize(dp_, 11,11, 2); int k = s[i] - '0'; for(int a=0; a<=10; a++) for(int b=0; b<=10; b++) for(int s=0; s< 2 ; s++){ if(!dp[a][b][s]) continue; for(int c=0; c<10; c++){ if(s==0 && c>k) break; int next_s = s || cc && a!=c) || (a>b && b> n; n += cnt_kadomatsu_digit("99"); long long lb = 1; long long ub = 500000000000000LL; while(ub-lb>1){ long long mid = (lb+ub)/2; bool ok = cnt_kadomatsu_digit(ltos(mid)) >= n; (ok? ub : lb) = mid; } cout << ub << endl; return 0; } #include int main(){ //auto s = clock(); int t; cin >> t; while(t--){ main_(); } //cerr << clock()-s << endl; return 0; }