Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

namespace __gnu_pbds{
          typedef tree<long long,
                       null_type,
                       less<long long>,
                       rb_tree_tag,
                       tree_order_statistics_node_update> ordered_set;
}
//multiset:-
//greater_equal<long long>
//less_equal<long long>

//set:-
//greater<long long>
//less<long long>
using namespace __gnu_pbds;
//A.order_of_key number of elements strictly less than k
//*A.find_by_order

// Miscellanous
#define ld long double
#define ll long long
#define ull unsigned long long
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(),v.end()

// Loops
#define loop(i,s,e) for (int (i)=(s);(i)<(e);++(i))
#define loope(i,s,e) for (int (i)=(s);(i)<=(e);++(i))

// Constants
#define PI 3.1415926535897932384626
const int mod = 1000000007;

// Containers
typedef pair<int, int>	    pii;
typedef pair<ll, ll>	    pll;
typedef vector<int>		    vi;
typedef vector<ll>		    vl;
typedef vector<pii>		    vpii;
typedef vector<pll>		    vpll;
typedef vector<vi>		    vvi;
typedef vector<vl>		    vvl;
#define in(a) for(int i = 0; i<a.size(); i++) cin>>a[i];
#define out(a) for(int i = 0; i<a.size(); i++) cout<<a[i]<<" ";

// Debug
void _print(int t) {cerr << t;}
void _print(ll t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);

template <class T> void _print(unordered_set <T> v);
template <class T> void _print(unordered_multiset <T> v);
template <class T> void _print(set <T> v);
template <class T> void _print(multiset <T> v);

template <class T, class V> void _print(unordered_map <T, V> v);
template <class T, class V> void _print(unordered_multimap <T, V> v);
template <class T, class V> void _print(map <T, V> v);
template <class T, class V> void _print(multimap <T, V> v);

template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T> void _print(unordered_set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(unordered_multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T, class V> void _print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(unordered_multimap <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(multimap <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

#ifndef ONLINE_JUDGE
#define deb(x) cerr << #x << " = ";  _print(x); cerr << endl;
#else
#define deb(x)
#endif
//stoi to_string
#define py	    cout<<"YES\n";
#define pn	    cout<<"NO\n";
#define pnl	    cout<<"\n";




ll  ith_set_bit_ct_0_r(ll r,ll i){
    ll period = (1LL<<(i+1));
    ll freq = (1LL<<i);
    ll ct1 = ((r+1)/period)*freq+max(0LL,(r+1)%period-freq)  ;
    return ct1;
}
ll  ith_set_bit_ct_l_r(ll l  ,ll r,ll i){
  if(l==0)return ith_set_bit_ct_0_r(r,i);  
  return ith_set_bit_ct_0_r(r,i)-ith_set_bit_ct_0_r(l-1,i);
}

ll xor1_to_n(ll n ){
  if(n%4==0)return n;
  else if(n%4==1)return 1;
  else if(n%4==2)return n+1;
  else return 0;
}

class DSU_by_rank{
  public:
  vector<int>rank;
  vector<int>par;
  DSU_by_rank(int n){
    rank.resize(n+1,0);
    par.resize(n+1,0);
    for(int i = 0;i<=n ;i++)par[i] =i;
  }
  int find(int node){
    if(par[node] == node)return node;
    else return par[node]  = find(par[node]);
  }
  void unionr(int x, int y){
    int rx = find(x);
    int ry = find(y);

    if(rank[rx]<rank[ry])swap(rx,ry);
    par[ry]  = rx;
    if(rank[rx]==rank[ry])rank[rx]++;
  }
};

class DSU_by_size{
  public:
  vector<int>size;
  vector<int>par;
  DSU_by_size(int n){
      size.resize(n+1,1);
      par.resize(n+1,0);
      for (int  i = 0; i < n+1; i++)
      {
        /* code */
        par[i]=i;
      }
  }
  int find(int x){
    if(par[x] == x)return x;
    else return par[x] = find(par[x]);
  }
  void unions(int x , int y){
    int rx = find(x);
    int ry = find(y);
    if(size[rx]<size[ry])swap(rx,ry);
    par[ry] = rx;
    size[rx]+= size[ry];
  }
};
void permute(string str)
{
    sort(str.begin(), str.end());

    do {
       cout << str << endl;
    } while (next_permutation(str.begin(), str.end()));
}




ll func(int  i ,int j ,int n ,int m , string & a,string & b , string & c , vvl &dp){
    if(j==m && i==n){
        return 0;
    }
    if(m-1-j < n-1-i){
        return INT_MAX;
    }
    if(j==m && i)
    if(dp[i][j]!=-1)return dp[i][j];
    ll ans = INT_MAX;
    if(i!=n)ans = min(func(i+1,j+1,n,m,a,b,c,dp)+1,ans);
    if(j!=m)ans = min(ans,func(i,j+1,n,m,a,b,c,dp)+1);
    if(i!=n && j!=m && c[j]==a[i])ans = min(ans , func(i+1,j+1,n,m,a,b,c,dp));
    if(j!=m && j-i<b.size() && b[j-i]==c[j])ans = min(ans ,func(i,j+1,n,m,a,b,c,dp));
    return dp[i][j]  = ans;
}


void solve()
{
    string a,b;
    cin>>a>>b;
    string c;
    cin>>c;

    int n = a.size();
    int m = c.size();
    vector<vector<ll>>dp(n+1,vector<ll>(m+1,-1));
    cout<<func(0,0,n,m,a,b,c,dp)<<endl;
}


#define N 300005
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout.precision(10); cout.setf(ios::fixed);
  int t;
  cin>>t;    
  while(t--){
    solve();
  }
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


/*
*/

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

namespace __gnu_pbds{
          typedef tree<long long,
                       null_type,
                       less<long long>,
                       rb_tree_tag,
                       tree_order_statistics_node_update> ordered_set;
}
//multiset:-
//greater_equal<long long>
//less_equal<long long>

//set:-
//greater<long long>
//less<long long>
using namespace __gnu_pbds;
//A.order_of_key number of elements strictly less than k
//*A.find_by_order

// Miscellanous
#define ld long double
#define ll long long
#define ull unsigned long long
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(),v.end()

// Loops
#define loop(i,s,e) for (int (i)=(s);(i)<(e);++(i))
#define loope(i,s,e) for (int (i)=(s);(i)<=(e);++(i))

// Constants
#define PI 3.1415926535897932384626
const int mod = 1000000007;

// Containers
typedef pair<int, int>	    pii;
typedef pair<ll, ll>	    pll;
typedef vector<int>		    vi;
typedef vector<ll>		    vl;
typedef vector<pii>		    vpii;
typedef vector<pll>		    vpll;
typedef vector<vi>		    vvi;
typedef vector<vl>		    vvl;
#define in(a) for(int i = 0; i<a.size(); i++) cin>>a[i];
#define out(a) for(int i = 0; i<a.size(); i++) cout<<a[i]<<" ";

// Debug
void _print(int t) {cerr << t;}
void _print(ll t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);

template <class T> void _print(unordered_set <T> v);
template <class T> void _print(unordered_multiset <T> v);
template <class T> void _print(set <T> v);
template <class T> void _print(multiset <T> v);

template <class T, class V> void _print(unordered_map <T, V> v);
template <class T, class V> void _print(unordered_multimap <T, V> v);
template <class T, class V> void _print(map <T, V> v);
template <class T, class V> void _print(multimap <T, V> v);

template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T> void _print(unordered_set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(unordered_multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T, class V> void _print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(unordered_multimap <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(multimap <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

#ifndef ONLINE_JUDGE
#define deb(x) cerr << #x << " = ";  _print(x); cerr << endl;
#else
#define deb(x)
#endif
//stoi to_string
#define py	    cout<<"YES\n";
#define pn	    cout<<"NO\n";
#define pnl	    cout<<"\n";




ll  ith_set_bit_ct_0_r(ll r,ll i){
    ll period = (1LL<<(i+1));
    ll freq = (1LL<<i);
    ll ct1 = ((r+1)/period)*freq+max(0LL,(r+1)%period-freq)  ;
    return ct1;
}
ll  ith_set_bit_ct_l_r(ll l  ,ll r,ll i){
  if(l==0)return ith_set_bit_ct_0_r(r,i);  
  return ith_set_bit_ct_0_r(r,i)-ith_set_bit_ct_0_r(l-1,i);
}

ll xor1_to_n(ll n ){
  if(n%4==0)return n;
  else if(n%4==1)return 1;
  else if(n%4==2)return n+1;
  else return 0;
}

class DSU_by_rank{
  public:
  vector<int>rank;
  vector<int>par;
  DSU_by_rank(int n){
    rank.resize(n+1,0);
    par.resize(n+1,0);
    for(int i = 0;i<=n ;i++)par[i] =i;
  }
  int find(int node){
    if(par[node] == node)return node;
    else return par[node]  = find(par[node]);
  }
  void unionr(int x, int y){
    int rx = find(x);
    int ry = find(y);

    if(rank[rx]<rank[ry])swap(rx,ry);
    par[ry]  = rx;
    if(rank[rx]==rank[ry])rank[rx]++;
  }
};

class DSU_by_size{
  public:
  vector<int>size;
  vector<int>par;
  DSU_by_size(int n){
      size.resize(n+1,1);
      par.resize(n+1,0);
      for (int  i = 0; i < n+1; i++)
      {
        /* code */
        par[i]=i;
      }
  }
  int find(int x){
    if(par[x] == x)return x;
    else return par[x] = find(par[x]);
  }
  void unions(int x , int y){
    int rx = find(x);
    int ry = find(y);
    if(size[rx]<size[ry])swap(rx,ry);
    par[ry] = rx;
    size[rx]+= size[ry];
  }
};
void permute(string str)
{
    sort(str.begin(), str.end());

    do {
       cout << str << endl;
    } while (next_permutation(str.begin(), str.end()));
}




ll func(int  i ,int j ,int n ,int m , string & a,string & b , string & c , vvl &dp){
    if(j==m && i==n){
        return 0;
    }
    if(m-1-j < n-1-i){
        return INT_MAX;
    }
    if(dp[i][j]!=-1)return dp[i][j];
    ll ans = INT_MAX;
    if(i!=n)ans = min(func(i+1,j+1,n,m,a,b,c,dp)+1,ans);
    if(j!=m)ans = min(ans,func(i,j+1,n,m,a,b,c,dp)+1);
    if(i!=n && j!=m && c[j]==a[i])ans = min(ans , func(i+1,j+1,n,m,a,b,c,dp));
    if(j!=m && j-i<b.size() && b[j-i]==c[j])ans = min(ans ,func(i,j+1,n,m,a,b,c,dp));
    return dp[i][j]  = ans;
}


void solve()
{
    string a,b;
    cin>>a>>b;
    string c;
    cin>>c;

    int n = a.size();
    int m = c.size();
    vector<vector<ll>>dp(n+1,vector<ll>(m+1,-1));
    cout<<func(0,0,n,m,a,b,c,dp)<<endl;
}


#define N 300005
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout.precision(10); cout.setf(ios::fixed);
  int t;
  cin>>t;    
  while(t--){
    solve();
  }
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


/*
*/