RT,WA on #5。
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
typedef long long ll;
using namespace std;
int qwq;
const int N = 5e5 + 5;
int n,a[N],b[N],cnt;
map<int,int> mp;
vector<int> g[N][2];
int get(int x){return mp.count(x) ? mp[x] : mp[x] = ++cnt;}
vector<pair<int,int> > v;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> qwq;
while(qwq--){
cin >> n;
v.clear();
int pre = 0,lst = 0;
for(int i = 1;i <= n;i++){
cin >> a[i];
if(~a[i])v.push_back({i,a[i]}),pre = (!pre ? i : pre),lst = i,b[i] = a[i];
}
bool flag = true;
for(int i = 1;i < v.size();i++){
pair<int,int> x = v[i - 1],y = v[i];
bool appear = false;
int dis = y.first - x.first;
if(dis == 1){
if(x.second / 2 != y.second && y.second / 2 != x.second)flag = false;
continue;
}
mp.clear();
for(int i = 1;i <= cnt;i++)g[i][0].clear(),g[i][1].clear();
cnt = 0;
int t = x.second;
for(int j = 0;j <= dis;j++){
g[get(t)][j & 1].push_back(j);
// cout << j << " " << t << endl;
t /= 2;
}
// cout << "-----------------------------\n";
t = y.second;
for(int j = dis;j >= 1;j--){
// cout << j << " " << t << endl;
if(g[get(t)][j & 1].size() && t){
int pos = g[get(t)][j & 1].back();
int tmp = x.second;
for(int k = 1;k <= pos;k++)tmp /= 2,b[k + x.first] = tmp;
tmp = y.second;
for(int k = dis - 1;k >= j;k--)tmp /= 2,b[k + x.first] = tmp;
for(int k = pos + 1;k < j;k++)b[k + x.first] = ((k - pos) & 1 ? t * 2 : t);
appear = true;
break;
}
t /= 2;
}
if(!appear){
if(y.second < x.second){
int tmp = x.second;
for(int j = 1;j <= dis;j++)tmp /= 2;
if(tmp == y.second){
tmp = x.second;
for(int j = 1;j < dis;j++)b[j + x.first] = (tmp /= 2);
appear = true;
}
}else{
int tmp = y.second;
for(int j = 1;j <= dis;j++)tmp /= 2;
if(tmp == x.second){
tmp = y.second;
for(int j = dis - 1;j >= 1;j--)b[j + x.first] = (tmp /= 2);
appear = true;
}
}
}
// cout << "-----------------------------\n";
if(!appear)flag = false;
}
if(!flag)cout << "-1\n";
else{
if(!pre){
for(int i = 1;i <= n;i++)cout << 2 - (i & 1) << " ";
cout << endl;
continue;
}
for(int i = 1;i < pre;i++)b[i] = ((pre - i) & 1 ? a[pre] * 2 : a[pre]);
for(int i = lst + 1;i <= n;i++)b[i] = ((i - lst) & 1 ? a[lst] * 2 : a[lst]);
for(int i = 1;i <= n;i++)cout << b[i] << " ";cout << endl;
}
}
}