def main(): from math import sqrt,sin,cos,tan,ceil,radians,floor,gcd,exp,log,log10,log2,factorial,fsum from heapq import heapify, heappop, heappush from bisect import bisect_left, bisect_right from copy import deepcopy import copy import random from collections import deque,Counter,defaultdict from itertools import permutations,combinations from decimal import Decimal,ROUND_HALF_UP #tmp = Decimal(mid).quantize(Decimal('0'), rounding=ROUND_HALF_UP) from functools import lru_cache, reduce #@lru_cache(maxsize=None) from operator import add,sub,mul,xor,and_,or_,itemgetter INF = 10**18 mod1 = 10**9+7 mod2 = 998244353 #DecimalならPython ''' 一周回ってくるってことだよな 2^xの逆元がx? 2^xが 1 (mod p)のとき x = 1,p+1,2p+1, 2^x = 1,p+1,2p+1 pは2以外の時は奇数 p = 2 x = 2 2^x = 4 p = 3 x = 4 2^x = 16 p = 5 x = 3 2^x = 8 p = 7 x = 11 2^x = 2048 2^xを試していく。余りがxならよい 同じマスに来たらやめてよい なるほど 2^(p-1) = 1 (mod p) 2^(p-1)n = 1 (mod p) ''' def modinv(a, b): p = b x, y, u, v = 1, 0, 0, 1 while b: k = a // b x -= k * u y -= k * v x, u = u, x y, v = v, y a, b = b, a % b x %= p if x < 0: x += p return x N = int(input()) for _ in range(N): p = int(input()) if p == 2: print(2) continue ans = (p-1)*modinv(p-1,p) print(ans) if __name__ == '__main__': main()