# -*- coding: utf-8 -*-
"""
Created on Mon Jul 1 18:40:30 2019
@author: zangz
"""
class Node(object):
"""节点"""
def __init__(self, item):
self.item = item #这个链表节点的值
self.next = None #这个链表节点的下一个节点,每一个节点看成是一个对象
class SinCycLinkedlist(object):
"""单向循环链表"""
def __init__(self):
self._head = None #初始化头结点是空的
def is_empty(self):
"""判断链表是否为空"""
return self._head == None
def yuesefu(self,N):
"""约瑟夫环"""
# if self.is_empty():
# return
cur = self._head #游标指向开始的头结点
list1=[]
while True:
for i in range(N-2): #走一步
cur = cur.next #让游标指向第二个节点
# print(cur.next.item) #打印第三个节点的值
list1.append(cur.next.item)
cur.next = cur.next.next #第二个节点连接第四个节点
cur =cur.next #游标指向第四个节点
if cur.next == cur: #自己指向自己的时候说明只有一个了
# print(cur.item)
list1.append(cur.next.item)
return list1
def append(self, item):
"""尾部添加节点"""
node = Node(item)
if self.is_empty():
self._head = node
node.next = self._head
else:
# 移到链表尾部
cur = self._head
while cur.next != self._head:
cur = cur.next
# 将尾节点指向node
cur.next = node
# 将node指向头节点_head
node.next = self._head
if __name__ == "__main__":
m=int(input()) #输入有几组样例
list2=[] #用来接收输入的多组样例的值
for i in range(m):
n=int(input())
list2.append(n)
#对多组样例求约瑟夫环并输出
for j in list2:
ll = SinCycLinkedlist() #初始化一个空的链表
for a in range(j):
ll.append(a+1)
list1=[]
list1=ll.yuesefu(3) #3是数到3的自杀
str1=""
for b in list1: #按照输出的要求进行字符串的拼接
str1+=str(b)+" "
print(str1)