#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_multiset tree<long long , null_type,less_equal<long long >, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_set tree<long long , null_type,less<long long >, rb_tree_tag,tree_order_statistics_node_update>
#define fill(arr,c) memset(arr,c,sizeof(arr))
#define perm(r) next_permutation(all(r))
//it gives bool value ans permute the string lexoggraphically
#define sortdesc greater<int>()
#define ll long long int 
//std::setfill('0') << std::setw(5) << 25; it adds leaing zeroes;
#define f(i,a) for(int i=0;i<a;i++)
#define fo(i,a) for(int i=1;i<=a;i++)
#define foo(i,x,n,c) for(ll i=x;i<n;i+=c)
#define foi(i,x,n,c) for(ll i=x;i>=n;i+=c)
#define fauto(i,m) for(auto i:m)
#define forall(a) for( auto itr=a.begin();itr!=a.end();itr++)
#define ld long double
#define in push_back
#define mll map<ll,ll>
#define mcl map<char,ll>
#define msl map<string,ll>
#define vi vector<int >
#define vl vector<ll >
#define vp vector< pair<int,int> >
#define vs vector <string> 
#define vc vector <char> 
#define o(a) cout<<a
#define os(a) cout<<a<<" "
#define en cout<<"\n";
#define testcase ll t ;cin>>t; while(t--)
#define ff first
#define ss second
#define pl pair<ll,ll>
#define pi pair<int,int>
#define all(a) (a).begin(), (a).end()

#define sz(a)  (a).size()
#define prec(n) fixed<<setprecision(n)
#define mp make_pair
#define bitcount  __builtin_popcountll
#define imin o("imin");en;
using namespace std;
bool comp(const pair<int,int> &a , const pair<int,int> &b){
    if(a.first == b.first)  return a.second < b.second;
     return a.first < b.first;
}
bool comps(const pair<int,int> &a , const pair<int,int> &b){
    if(a.second == b.second)  return a.first< b.first;
     return a.second < b.second;
}
//ll pi=acos(0.0)*2;
void fastio()
{
    	ios::sync_with_stdio(0);
cin.tie(0);

}
void inout()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);

     freopen("output.txt", "w", stdout);
     #endif
}

int main()
{
fastio();
vl l;
string s;
cin>>s;
ll sum=0;
ll a[3];
f(i,3)
a[i]=0;
f(i,sz(s))
{
sum+=s[i]-'0';
a[(s[i]-'0')%3]++;
}
ll ans=-1;
if(sum%3==0)
ans=0;
else if(sum%3==1)
{
	if(a[1]>=1)
	ans=1;
	else if(a[2]>=2)
	ans=2;
	
}
else if(sum%3==2)
{
	if(a[2]>=1)
	ans=1;
	else if(a[1]>=2)
	ans=2;
	
}
if(ans<sz(s))
o(ans);
else
o(-1);

return 0;
}

#include<bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll,ll>
#define mod 998244353
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fb(i,a,b) for(ll i=a;i>b;i--)
#define ff first
#define ss second
#define srt(v)  if(!v.empty())sort(v.begin(),v.end())
#define rev(v)  if(!v.empty())reverse(v.begin(),v.end())
#define PI 3.141592653589793238
#define pqr priority_queue<ll,vector<ll>,greater<ll>()>
using namespace std;
ll pow_mod(ll a,ll b)
{
	ll res=1;
	while(b!=0)
	{
		if(b&1)
		{
			res=(res*a)%mod;
		}
		a=(a*a)%mod;
		b/=2;
	}
	return res;
}

ll inv_mod(ll x){
    return pow_mod(x,mod-2);
}

const ll N=(ll)3e5;
ll inv[N];
ll fac[N];

ll ncr(ll n,ll r){
    if(n<r)
    return 0;
    
    if(n==r||r==0)
    return 1LL;
    
    ll x=fac[n];
    ll y=inv[n-r];
    ll z=inv[r];
    
    return ((x*y)%mod*z)%mod;
}

void solve()
{
    ll n;
    cin>>n;
    
    string s[3];
    
    for(ll i=0;i<3;i++)
    cin>>s[i];
    
    for(ll i=0;i<3;i++)
    s[i]+=s[i];
    
    string ans;
    
    /* 
       1
       0101
       0101
       1010
       => ans=010
       
       01010101
       00110011
       11001100
       => ans=11011
       
       10011001
       01100110
       => ans=10000
       
       A binary string of length 2*N can be easily constructed easily
       by taking all the 1's or 0's.
       
       
    */
    
   for(ll i=0;i<n;i++)
   cout<<"0";
   for(ll i=0;i<n;i++)
   cout<<"1";
   cout<<"0";
   cout<<endl;
}
int main()
{    ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);
     
//Start from Here.

  ll t;
  t=1;
  cin>>t;
  while(t--)
  solve();
  
  
  
  
  
  fac[0]=1;
  
  for(ll i=1;i<N;i++)
  fac[i]=(i*fac[i-1])%mod;
  
  for(ll i=0;i<N;i++)
  inv[i]=inv_mod(fac[i]);
//Good Bye!
    return 0;
}
