#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,m;
inline int read()
{
int s=0;
char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s;
}
int he,ta,s[1010],t[1010000],e[1010100];
typedef pair<int,int> pa;
pa q[1010000];
map<pa,int>ma;
int main()
{
int T=read();
a = 99;//0 1 5 6
b = 78;//1 2 3 6
m = 120;//3 4 5 6
he=0;ta=1;
q[ta]=make_pair(a,b);t[1]=1;
ma[make_pair(a,b)]=1;
while(he<ta) {
he++;
int x=q[he].first,y=q[he].second;
if(!ma[make_pair(x&y,y)]) ma[make_pair(x&y,y)]=t[ta+1]=t[he]+1,q[++ta]=make_pair(x&y,y);
if(!ma[make_pair(x|y,y)]) ma[make_pair(x|y,y)]=t[ta+1]=t[he]+1,q[++ta]=make_pair(x|y,y);
if(!ma[make_pair(x,y^x)]) ma[make_pair(x,y^x)]=t[ta+1]=t[he]+1,q[++ta]=make_pair(x,y^x);
if(!ma[make_pair(x,y^m)]) ma[make_pair(x,y^m)]=t[ta+1]=t[he]+1,q[++ta]=make_pair(x,y^m);
}
// cout<<ta<<'\n';
while(T--) {
a=read(),b=read(),c=read(),d=read();m=read();
ma.clear();
s[6] = a & b & m;
s[1] = (a & b) ^ s[6];
s[3] = (b & m) ^ s[6];
s[5] = (a & m) ^ s[6];
s[0] = a ^ s[1] ^ s[5] ^ s[6];
s[2] = b ^ s[1] ^ s[3] ^ s[6];
s[4] = m ^ s[3] ^ s[5] ^ s[6];
for(int i=0;i<128;i++) {
int na=0;
for(int j=0;j<7;j++) if(i&(1<<j)) na|=s[j];
e[i]=na;
}
int flag=0;
for(int i=1;i<=ta;i++) {
int x = q[i].first,y=q[i].second;
int na=e[x],nb=e[y];
if(na==c&&nb==d) {flag=1;cout<<t[i]-1<<'\n';break;}
}
if(!flag) cout<<-1<<'\n';
// cerr<<ta<<'\n';
}
}
345. Reverse Vowels of a String | 628. Maximum Product of Three Numbers |
1526A - Mean Inequality | 1526B - I Hate 1111 |
1881. Maximum Value after Insertion | 237. Delete Node in a Linked List |
27. Remove Element | 39. Combination Sum |
378. Kth Smallest Element in a Sorted Matrix | 162. Find Peak Element |
1529A - Eshag Loves Big Arrays | 19. Remove Nth Node From End of List |
925. Long Pressed Name | 1051. Height Checker |
695. Max Area of Island | 402. Remove K Digits |
97. Interleaving String | 543. Diameter of Binary Tree |
124. Binary Tree Maximum Path Sum | 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts |
501A - Contest | 160A- Twins |
752. Open the Lock | 1535A - Fair Playoff |
1538F - Interesting Function | 1920. Build Array from Permutation |
494. Target Sum | 797. All Paths From Source to Target |
1547B - Alphabetical Strings | 1550A - Find The Array |