//PrePost
//pgmr: T Feil

#include <iostream.h>
#include <string.h>

int Des[26][26], Kids[26][26], NumKids[26];
char Pre[27], Post[27];
int m,n,root;

// calculate m choose n
long Choose(int m,int n){
  int i;
  long c = m;

  for(i=1;i<n;i++){
    c *= m-i;
    c /= i+1;
  }
  return c;
}

//does b precede a in Post?
int Before(char a, char b) {
  int i=0;
  while(Post[i]!=a && Post[i]!=b) i++;
  if(Post[i]==a)
    return 0;
  else
    return 1;
}

//use Des[][] to find the children of each node
void FindChildren(){
  int i,j,k, child;
 
  for(i=0;i<n;i++){
    Kids[i][i]=0;
    for(j=0;j<n;j++){
      child = 0;  
      if(Des[i][j]){
        child = 1;   //j is child until otherwise
        for(k=0;k<n;k++)
             //j descendant of a descendant of i
          if(k!=j && Des[i][k] && Des[k][j]) 
             child = 0;
      }
      if (child)
        Kids[i][j]=1;
      else
        Kids[i][j]=0;
   }
}}

// calculate # of trees - mult (m choose # children) by CountEm
//    for each kid
long CountEm(int x){
  int i;
  long count;

  if(NumKids[x]==0)
    return 1;
  count = Choose(m,NumKids[x]);
  for(i=0;i<n;i++)
    if(Kids[x][i])
      count *= CountEm(i);

  return count;
}
          
void main(){

  int i,j,count;

  cin>>m;
  while(m>0) {
    cin>>Pre>>Post;
    n = strlen(Pre);

    //find descendants of each letter
    for(i=0;i<n;i++)
      for(j=0;j<n;j++)
        Des[i][j]=0;

    for(i=0;i<n;i++)
      for(j=i+1;j<n;j++)
        if(Before(Pre[i],Pre[j]))
          Des[Pre[i]-'a'][Pre[j]-'a']=1; 

    FindChildren(); //Use Des[][] to get children

    root = Pre[0]-'a';

    for(i=0;i<n;i++){
      count=0;
      for(j=0;j<n;j++)
        if(Kids[i][j]) count++;
      NumKids[i] = count;
    }

    count = CountEm(root);  //calc # of trees 
    cout<<count<<endl;

    cin>>m;
  }
  return 0;
}
