fix bug, when q is too large, program would segment falut