WA in CFD021. Can't decipher the mistake

Link to the problem: CFD021 Problem - CodeChef
My c++ code is linked below

#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define llf long long float
#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fi(i,a,b) for(int i=a;i<b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define endl '\n'
#define pb push_back
#define mod 1000000007
#define u_m unordered_map
#define limit pow(10,6)
int intlog(double x) {
    return floor((lli)(log(x) / log(3)));
}

int main()
{

    IOS;
    #ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
    freopen("output.out","w",stdout);
    #endif
    set<int>s;double x,save;int t,q=0;cin>>t;
   	while(t--)
   	{
   		cin>>x;long long int p;save=x;
   		while(x>0)
   		{
   			p=intlog(x);x-=pow(3,p);
   			if(p==0) continue;
   			else if(s.find(p)==s.end()) s.insert(p);
   			else s.erase(p);
   		}
   		set<int>::iterator it;
   		for(it=s.begin();it!=s.end();++it)
   			q^=*it;
   		lli ans = q>0 ? pow(3,q):0;
   		cout<<save+ans<<endl;
   	}
return 0;
}

Consider the testcase:

1
7

My output is 7.It is correct right?

Whoops, sorry: I meant:

1
4

Saw some submissions on and figured out they were doing XOR of the power only when the remained achieved in 1.
bool xp(ll n)
{
ll r=0,i=0,x;
while(n)
{
x=(n%3);
if(x&1)
r^=i;
n/=3;
i++;
}
if(r==0)
return 1;
return 0;
}`