/*
* ZOJ_3210.cpp
*
* Created on: 2013年10月30日
* Author: Administrator
*/
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 110;
int main(){
int a[maxn];
int t;
scanf("%d",&t);
while(t--){
bool isstack = true,isqueue = true;
int n;
scanf("%d",&n);
int i;
for(i = 0 ; i < n ; ++i){
scanf("%d",&a[i]);
}
for(i = 0 ; i < n ; ++i){
int b;
scanf("%d",&b);
if( b != a[i]){//判断是否满足先进先出
isqueue = false;
}
if(b != a[n - 1 - i]){//判断是否满足先进后出
isstack = false;
}
}
if(isstack && isqueue){
printf("both\n");
}else if(!isstack && !isqueue){
printf("neither\n");
}else if(isstack){
printf("stack\n");
}else if(isqueue){
printf("queue\n");
}
}
return 0;
}