Help me in solving CAESAR problem

My issue

My code

why this is not getting accepted what’s wrong in this code?

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int g;
	cin>>g;
	for(int i=0;i<g;i++){
	    int n;
	    cin>>n;
	   string a,b,c;
	   cin>>a;
	   cin>>b;
	   cin>>c;
	   int a1=a[0];
	   int b1=b[0];
	   int k;
	  // if(a1<=b1)
	   k=(b[n-1]-a[n-1])%26;
	  // else k='z'-a[0]+b[0];
	  // cout<<k;
	  char p;
	 for(int j=0;j<n;j++){
	       int f=c[j];
	       int r=f+k;
	     if(r>122){
	           r=r-122;
	           p=96+r;
	           // cout<<char(f);
	       }
	       else p=r;
	      

  //  cout<<p;
	      // cout<<char(r);
	      /* char p=r;*/
	       cout<<p;
	   }
	    cout<<endl;
	}
	return 0;
}

Problem Link: https://www.codechef.com/problems/CAESAR

@kpsrivastava23
the value of k u r generating can also be negative like when b[n-1]=‘a’ and a[n-1]=‘z’ so to handle this just add 26 to the diff and then take modulo 26 .
This is my code i have written in much simpler way hope u will get it.
include
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
string s,t,u;
cin>>s>>t>>u;
int k=(t[0]-s[0]+26)%26;

    for(int i=0;i<u.size();i++)
    {
        int val=(u[i]-'a'+1+k)%26;

        if(val==0)
        val=26;
        u[i]='a'+val-1;
    }
    cout<<u<<endl;
}
return 0;

}