A number is called faithful if you can write it as the sum of distinct powers of 7. 
e.g.,  2457 = 7 + 7^{2} + 7^{4 . }If we order all the faithful numbers, we get the sequence 1 = 7^{0}, 7 = 7^{1}, 8 = 7^{0} + 7^{1}, 49 = 7^{2}, 50 = 7^{0} + 7^{2} . . . and so on.
Given some value of N, you have to find the N'th faithful number.
 
Example 1:
Input:
N = 3
Output:
8
Explanation:
8 is the 3rd Faithful number.
Example 2:
Input:
N = 7
Output:
57
Explanation:
57 is the 7th Faithful number.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function nthFaithfulNum() which takes an Integer N as input and returns the answer.
 
Expected Time Complexity: O(log(N))
Expected Auxiliary Space: O(log(N))
 
Constraints:
1 <= N <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3

class Solution:
    def nthFaithfulNum(self, N):
        # code here 

Solutions:
class Solution:

	def nthFaithfulNum(self, N):
		ans = 0
		power = 0
		while N:
			if N & 1:
				ans = ans + pow(7, power)
			power = power + 1
			N = N // 2
		return ans


class Solution:

	def nthFaithfulNum(self, N):
		d = 1
		i = 1
		res = 0
		while i <= N:
			if N & i:
				res += d
			d *= 7
			i <<= 1
		return res


class Solution:

	def nthFaithfulNum(self, N):
		exp = 0
		res = 0
		pow = 0
		while N:
			pow = 7 ** exp
			if N & 1:
				res += pow
			N >>= 1
			exp += 1
		return res


class Solution:

	def nthFaithfulNum(self, N):
		(res, x) = (0, 1)
		while N != 0:
			if N % 2 == 1:
				res += x
			x = x * 7
			N = N >> 1
		return res


class Solution:

	def nthFaithfulNum(self, N):
		x = str(bin(N)[2:])
		x1 = x[::-1]
		j = 0
		s = 0
		for i in x1:
			if int(i) == 1:
				s += pow(7, j)
			j += 1
		return s


class Solution:

	def nthFaithfulNum(self, N):
		N_in_binary = '{0:b}'.format(N)
		N_in_binary_reversed = N_in_binary[::-1]
		elements = [7 ** i for (i, e) in enumerate([*N_in_binary_reversed]) if e == '1']
		return sum(elements)


class Solution:

	def nthFaithfulNum(self, N):
		if N == 1:
			return 1
		temp = bin(N).replace('0b', '')
		temp = temp[::-1]
		sum = 0
		for i in range(len(temp)):
			sum += 7 ** i * int(temp[i])
		return sum


class Solution:

	def nthFaithfulNum(self, N):
		res = 0
		exp = 1
		while N > 0:
			currbit = N & 1
			res = res + currbit * exp
			exp *= 7
			N = N // 2
		return res
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.nthFaithfulNum(N))


class Solution:

	def nthFaithfulNum(self, N):
		p = 1
		answer = 0
		while N:
			l = N & 1
			N = N >> 1
			answer += l * p
			p = p * 7
		return answer
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.nthFaithfulNum(N))


class Solution:

	def nthFaithfulNum(self, N):
		bits = []
		while N // 2:
			r = N % 2
			bits.append(r)
			N = N // 2
		if N == 1:
			bits.append(1)
		n = 0
		sum = 0
		for i in bits:
			sum += i * 7 ** n
			n += 1
		return sum
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.nthFaithfulNum(N))


class Solution:

	def nthFaithfulNum(self, n):
		ans = 0
		for i in range(25):
			if n & 1 << i:
				ans += 7 ** i
		return ans


class Solution:

	def nthFaithfulNum(self, N):
		lst = []
		i = 0
		while True:
			lst.append(pow(2, i))
			if lst[-1] >= N:
				break
			i += 1
		count = 0
		for i in range(len(lst) - 1, -1, -1):
			if lst[i] <= N:
				N = N - lst[i]
				count += pow(7, i)
			if N == 0:
				break
		return count


import math

class Solution:

	def nthFaithfulNum(self, N):
		j = 0
		sum1 = 0
		while N != 0:
			k = N & 1
			sum1 += math.pow(7, j) * k
			j += 1
			N = N >> 1
		return int(sum1)


import math

class Solution:

	def nthFaithfulNum(self, N):
		ans = 0
		i = 0
		while N != 0:
			lastDigit = N & 1
			ans += math.pow(7, i) * lastDigit
			i += 1
			N = N >> 1
		return int(ans)


class Solution:

	def nthFaithfulNum(self, N):
		b = bin(N).replace('0b', '')
		arr = [int(b[i]) for i in range(len(b))]
		arr.reverse()
		svn = 0
		for i in range(len(arr)):
			if arr[i] == 0:
				continue
			if arr[i] == 1:
				svn += 7 ** i
		return svn


class Solution:

	def nthFaithfulNum(self, N):
		n = bin(N)
		n = n[2:]
		n = n[::-1]
		res = 0
		for i in range(len(n)):
			res += int(n[i]) * 7 ** i
		return res


class Solution:

	def nthFaithfulNum(self, N):
		val = list(bin(N)[2:])
		val.reverse()
		sumi = 0
		seven = 0
		for i in range(len(val)):
			sumi += 7 ** seven * int(val[i])
			seven += 1
		return sumi


class Solution:

	def nthFaithfulNum(self, N):
		b = str(bin(N))[2:]
		J = 0
		t = 0
		for i in range(len(b) - 1, -1, -1):
			if b[i] == '1':
				t = t + 7 ** J
			J = J + 1
		return t


class Solution:

	def nthFaithfulNum(self, N):
		power = 0
		an = 0
		while N > 0:
			if N & 1:
				an += 7 ** power
			power += 1
			N >>= 1
		return an


class Solution:

	def nthFaithfulNum(self, N):
		i = 0
		sum = 0
		while N > 0:
			a = N % 2
			sum = sum + 7 ** i * a
			N = N // 2
			i = i + 1
		return sum


import math

class Solution:

	def nthFaithfulNum(self, N):
		p = 1
		count = 0
		while N > 0:
			b = N & 1
			count += b * p
			p = p * 7
			N = N >> 1
		return count


class Solution:

	def nthFaithfulNum(self, N):
		if N == 1:
			return 1
		else:
			faith = 0
			lst = []
			while N > 0:
				t = N % 2
				lst.append(t)
				N = N // 2
			for i in range(len(lst)):
				faith += lst[i] * 7 ** i
			return faith


class Solution:

	def nthFaithfulNum(self, N):
		s = bin(N)
		s = s[2:]
		l = list(s)
		l.reverse()
		res = 0
		for i in range(0, len(l)):
			if l[i] == '1':
				res += pow(7, i)
		return res


class Solution:

	def nthFaithfulNum(self, n):
		ans = 0
		x = bin(n)[2:]
		c = 0
		for i in range(len(x) - 1, -1, -1):
			ans += 7 ** c * int(x[i])
			c += 1
		return ans


class Solution:

	def nthFaithfulNum(self, n):
		pow = 0
		ans = 0
		c = 0
		while n:
			x = n % 2
			n = n // 2
			if x == 1:
				ans += 7 ** c
			c += 1
		return ans


class Solution:

	def nthFaithfulNum(self, N):
		power = 0
		answer = 0
		while N:
			if power == 0:
				power = 1
			else:
				power = power * 7
			if N & 1:
				answer += power
			N = N >> 1
		return answer


class Solution:

	def nthFaithfulNum(self, N):
		res = 0
		pos = 0
		while N > 0:
			if N & 1 == 1:
				res += 7 ** pos
			pos += 1
			N >>= 1
		return res


class Solution:

	def nthFaithfulNum(self, N):
		binN = bin(N)[2:]
		res = 0
		for c in binN:
			if c == '1':
				res = res * 7 + 1
			else:
				res *= 7
		return res


import math

class Solution:

	def nthFaithfulNum(self, N):
		b = str(bin(N))[2:]
		r = 0
		t = 0
		for i in b[::-1]:
			r = r + int(i) * 7 ** t
			t = t + 1
		return r


class Solution:

	def nthFaithfulNum(self, n):
		power = 0
		sum1 = 0
		while n:
			powerVal = 7 ** power
			if n & 1:
				sum1 += powerVal
			n >>= 1
			power += 1
		return sum1


import math

class Solution:

	def nthFaithfulNum(self, N):
		c = 0
		s = 0
		while N > 0:
			n = N & 1
			s += n * math.pow(7, c)
			c += 1
			N = N >> 1
		return int(s)