#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
const int N = 1e6 + 5;
using namespace std;

int cas, n;
char s[3][N], T[N];

template < typename T >
inline T read()
{
	 T x = 0, w = 1; char c = getchar();
	 while(c < '0' || c > '9') { if(c == '-') w = -1; c = getchar(); }
	 while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	 return x * w; 
}

bool check()
{
	 bool flag = 1; int pos = 1;
	 for(int op = 0; op < 3; op++)
	 {
		  pos = 1;
		  for(int i = 1; i <= 4 * n; i++)
				if(pos <= 2 * n + 1 && T[pos] == s[op][i > 2 * n ? i - 2 * n : i]) pos++;
		  flag &= pos > 2 * n;
	 }
	 return flag;
}

int main()
{
#ifndef ONLINE_JUDGE
	 freopen("cpp.in", "r", stdin);
#endif
	 cas = read <int> ();
	 while(cas--)
	 {
		  n = read <int> (), scanf("%s", s[0] + 1), scanf("%s", s[1] + 1), scanf("%s", s[2] + 1);
		  for(int i = 1; i <= n; i++) T[i] = '1', T[n + i] = '0'; T[2 * n + 1] = '1';
		  if(check()) { for(int i = 1; i <= 2 * n + 1; i++) printf("%c", T[i]); puts(""); continue; }
		  for(int i = 1; i <= n; i++) T[i] = '1', T[n + i] = '0'; T[2 * n + 1] = '0';
		  if(check()) { for(int i = 1; i <= 2 * n + 1; i++) printf("%c", T[i]); puts(""); continue; }
		  for(int i = 1; i <= n; i++) T[i] = '0', T[n + i] = '1'; T[2 * n + 1] = '0';
		  if(check()) { for(int i = 1; i <= 2 * n + 1; i++) printf("%c", T[i]); puts(""); continue; }
		  for(int i = 1; i <= n; i++) T[i] = '0', T[n + i] = 1; T[2 * n + 1] = '1';
		  if(check()) { for(int i = 1; i <= 2 * n + 1; i++) printf("%c", T[i]); puts(""); continue; }
		  T[1] = '0'; for(int i = 1; i <= n; i++) T[i + 1] = '1', T[n + i + 1] = '0';
		  if(check()) { for(int i = 1; i <= 2 * n + 1; i++) printf("%c", T[i]); puts(""); continue; }
		  T[1] = '1'; for(int i = 1; i <= n; i++) T[i + 1] = '1', T[n + i + 1] = '0';
		  if(check()) { for(int i = 1; i <= 2 * n + 1; i++) printf("%c", T[i]); puts(""); continue; }		  
		  T[1] = '1'; for(int i = 1; i <= n; i++) T[i + 1] = '0', T[n + i + 1] = '1';
		  if(check()) { for(int i = 1; i <= 2 * n + 1; i++) printf("%c", T[i]); puts(""); continue; }
		  T[1] = '0'; for(int i = 1; i <= n; i++) T[i + 1] = '0', T[n + i + 1] = '1';
		  if(check()) { for(int i = 1; i <= 2 * n + 1; i++) printf("%c", T[i]); puts(""); continue; }
	 }
	 return 0; 
}

#include<bits/stdc++.h>
#include<iostream>
#include<map>
#include<math.h>
#include<string>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<stack>
#define _GLIBCXX_DEBUG
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
using namespace std;
using ll=long long;
const ll INF=5000000000000;
const ll dx[8]={1,0,-1,0,1,1,-1,-1};
const ll dy[8]={0,1,0,-1,1,-1,1,-1};
ll mod(ll x,ll m){return x & m;}
ll modinv(ll a,ll m){ll b=m,u=1,v=0;while(b){ll t=a/b;a-=t*b;swap(a,b);u-=t*v;swap(u,v);}u%=m;if(u<0){u+=m;}return u;}
ll modpow(ll a,ll n,ll m){ll res=1;while(n>0){if(n&1){res=res*a%m;}a=a*a%m;n>>=1;}return res;}
ll npow(ll a,ll n){ll res=1;while(n>0){if(n&1){res=res*a;}a=a*a;n>>=1;}return res;}
ll yaku(ll x){ll cnt=0;for(int i=1;i*i<=x;i++){if(x%i==0){if(i==x/i){cnt++;}else{cnt+=2;}}}return cnt;}
ll modwaru(ll a, ll b, ll m){a%=m;return a*modinv(b,m)%m;}
ll gcd(ll x, ll y){if(x%y==0){return y;}else{return gcd(y, x % y);}}
ll lcm(ll a, ll b){return a/gcd(a, b)*b;}
struct UnionFind{vector<ll>par;UnionFind(ll N):par(N){for(ll i=0;i<N;i++)par[i]=i;}ll root(ll x){if(par[x]==x){return x;}return par[x]=root(par[x]);}void unite(ll x,ll y){ ll rx=root(x);ll ry=root(y);if(rx==ry){return;}par[rx]=ry;}bool same(ll x,ll y){ll rx=root(x);ll ry=root(y);return rx==ry;}};

int main(){
    ll N, ans = 0, now = 0;
    cin >> N;
    vector<ll> tma, tmi;
    map<ll,ll> ma, mi;
    for(int i = 0; i < N; i++){
        ll X, C;
        cin >> X >> C;
        if(!ma.count(C)) ma[C] = X;
        else ma[C] = max(ma[C], X);
        if(!mi.count(C)) mi[C] = X;
        else mi[C] = min(mi[C], X);
    }
    tma.push_back(0);
    tmi.push_back(0);
    for(pair<ll,ll> a : ma) tma.push_back(a.second);
    for(pair<ll,ll> a : mi) tmi.push_back(a.second);
    vector<pair<ll,ll>> dp(tma.size() + 1, {INF,INF});
    dp[0].first = 0;
    dp[0].second = 0;
    for(int i = 1; i < tma.size(); i++){
        ll kyo = tma[i] - tmi[i];
        dp[i].first = min(dp[i - 1].first + abs(tma[i - 1] - tmi[i]) + kyo, dp[i - 1].second + abs(tmi[i - 1] - tmi[i]) + kyo);
        dp[i].second = min(dp[i - 1].first + abs(tma[i - 1] - tma[i]) + kyo, dp[i - 1].second + abs(tmi[i - 1] - tma[i]) + kyo);
    }
    dp[tma.size() - 1].first += abs(tma[tma.size() - 1]);
    dp[tma.size() - 1].second += abs(tmi[tma.size() - 1]);
    cout << min(dp[tma.size() - 1].first, dp[tmi.size() - 1].second) << endl;
}