// Cheese-Cracker: cheese-cracker.github.io

void theCode(){
    int k = scan!int;
    int j = 0;
    for(int i = 1; i <= 3*k; ++i){
        if(i % 3 == 0 || i % 10 == 3) continue;
        ++j;
        if(j == k){
            writeln(i);
        }
    }
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) theCode();
}

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");
