Time Limit Exceeding in Problem :COINFLIP (Math)

Please tell logic to minimize Time complexity in this problem. Refer and check my code below in cpp:
#include
#include <bits/stdc++.h>

using namespace std;

int main()
{
** int T;**
** cin>>T;**
** int G,ans;**
** int I,N,Q;**

** while(T–) {**
** cin>>G;**
** while(G–) {**
** cin>>I>>N>>Q;**
** int tail_no=0;**
** int head_no=0;**
** int ans=0;**
** for(int i=N;i>0;i–) {**
** if(I==1 && Q==1){**
** if(i%2==0) {**
** ans= ++head_no;**
** }**
** }**
** else if(I==1 && Q==2){**
** if(i%2!=0) {**
** ans = ++tail_no;**
** }**
** }**
** else if(I==2 && Q==1){**
** if(i%2!=0) {**
** ans= ++head_no;**
** }**
** }**
** else {**
** if(i%2==0) {**
** ans= ++tail_no;**

** }**
** }**

** }**
** cout<<ans<<endl;**
** }**
** }**

** return 0;**
}