fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ub upper_bound // first element > val(itr)
  5. #define lb lower_bound // first element >= val(itr)
  6.  
  7. #define sz(a) int((a).size())
  8. #define pb push_back
  9. #define all(c) (c).begin(),(c).end()
  10. #define tr(c,i) for(typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
  11. #define present(c,x) ((c).find(x) != (c).end())
  12. #define cpresent(c,x) (find(all(c),x) != (c).end())
  13. #define mem(a,b) memset( a, b, sizeof(a) )
  14. #define x first
  15. #define y second
  16.  
  17. #define bitcount(a) __builtin_popcount(a) // count set bits
  18. #define lzcount(x) __builtin_clz(x) // count leading zeros in binary representation of number
  19. #define tzcount(x) __builtin_ctz(x) // count trailing zeros in binary representation of number
  20.  
  21. #define FAST ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  22.  
  23. typedef long long ll;
  24. typedef long double lld;
  25. const ll MOD = 1e9+7;
  26. const lld PI = acos(-1.0);
  27.  
  28. template<typename T> T gcd(T a,T b){ if(b>a) return gcd(b,a);return b==0?a:gcd(b,a%b); }
  29. template<typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }
  30. template<typename T> T fast_power(T x,T y,ll m=MOD){T ans=1;while(y>0){if(y&1LL) ans=(ans*x)%m;y>>=1LL;x=(x*x)%m;}return ans%m;}
  31. template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
  32. template<typename X> inline X sqr(const X& a) { return a * a; }
  33.  
  34. inline ll mod(ll x,ll n) {if (x < n) return x; if (x >= n) return x - n;}
  35. string IntToString(ll a){ ostringstream temp; temp << a; return temp.str();}
  36. ll findMMI_fermat(ll n,ll M) {ll ans= fast_power(n,M-2,M);return ans;}//i.e In (a/b)%M..it calculates MMI of b wrt M,only if M is prime
  37. ll add(ll a,ll b,ll M) { return mod(( mod(a,M ) + mod(b,M )),M); }
  38. ll sub(ll a, ll b,ll M) { return mod((mod(a,M ) + M - mod(b,M )),M); }
  39. ll mult(ll a,ll b,ll M) { return mod(( mod(a,M)*mod(b,M) ),M) ; }
  40. bool isprime(ll a){ if(a==2) {return 1;}if(!(a&1) ) {return 0;}for(ll i=3;i*i<=a;i+=2){if(a%i==0) {return 0;} } return 1;}
  41.  
  42. ll gcdExtended(ll a, ll b, ll *x, ll *y){
  43. if (a == 0){
  44. *x = 0, *y = 1;
  45. return b;
  46. }
  47. ll x1, y1;
  48. ll gcd1= gcdExtended(b%a, a, &x1, &y1);
  49. *x = y1 - (b/a) * x1;
  50. *y = x1;
  51. return gcd1;
  52. }
  53.  
  54. ll modInv(ll a, ll m){
  55. ll x, y;
  56. ll g = gcdExtended(a, m, &x, &y);
  57. ll res = (x%m + m) % m;
  58. return res;
  59. }
  60.  
  61. /* -------------------------------Main Code------------------------------- */
  62.  
  63.  
  64. int main(){
  65. FAST
  66. int t;cin>>t;
  67. while(t--){
  68. //while(t--){
  69. string s,r;cin>>s>>r;
  70. string temp;
  71. int cnt1[26]={0},cnt2[26]={0};
  72. for(int i=0;i<sz(s);i++){
  73. cnt1[s[i]-'a']++;
  74. }
  75. for(int i=0;i<sz(r);i++){
  76. cnt2[r[i]-'a']++;
  77. }
  78. int poss = 1;
  79. for(int i=0;i<26;i++){
  80. if(cnt2[i]<cnt1[i]){
  81. poss=0;
  82. }
  83. cnt2[i]-=cnt1[i];
  84. }
  85. //cout<<"A"<<endl;
  86. temp.reserve(sz(r));
  87. if(poss){
  88. int stop = s[0]-'a';
  89. //int x = 0;
  90. for(int i=0;i!=stop;i++){
  91. temp.append(cnt2[i], (i+'a'));
  92. //x+=cnt2[i];
  93. }
  94. temp += s;
  95. //x+=sz(s);
  96. for(int i=stop;i<26;i++){
  97. temp.append(cnt2[i], (i+'a'));
  98. //x+=cnt2[i];
  99. }
  100. cout<<temp<<endl;
  101. }
  102. else{
  103. cout<<"Impossible"<<endl;
  104. }
  105. }
  106. return 0;
  107. }
  108.  
  109.  
Success #stdin #stdout 0s 4436KB
stdin
4
aa
ababab
aaa
ramialsadaka
said
sryhieni
code
codeisfun
stdout
aaabbb
aaaaadiklmrs
Impossible
codefinsu