Help me in solving MAKEDIV3 problem

My issue

import random
for _ in range(int(input())):
x=int(input())
while x!=0:
r=random.randint(10**(x-1),10**x)
if r%3==0 and r%9!=0 and r%2!=0:
print(r)
break
can anyone tell why this approach is wrong pls?

My code

import random
for _ in range(int(input())):
    x=int(input())
    while x!=0:
        r=random.randint(10**(x-1),10**x)
        if r%3==0 and r%9!=0 and r%2!=0:
            print(r)
            break
            
    

Learning course: Jump from 2* to 3*
Problem Link: Make it Divisible Practice Problem in Jump from 2* to 3* - CodeChef

@darlevysali
refer my c++ code , its quite simple

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n;
	    cin>>n;
	    for(int i=0;i<n-1;i++)
	    cout<<9;
	    cout<<3;
	    cout<<endl;
	}
	return 0;
}

Python 3

for _ in range(int(input())):

n = int(input())
if n == 1:
    print(3)
    continue
s = "3" + "0" * (n - 2) + "3"
print(s)