import java.util.*;

public class Main {
    public static void main(String[] args) {
      
      List<Vegetable> list = new ArrayList<>();
      
      Scanner sc = new Scanner(System.in);
      
      int n = sc.nextInt();
      for(int i=0; i < n ; i++) {
        int vegetableId = sc.nextInt();
        sc.nextLine();
        String vegetableName = sc.nextLine();
        int price = sc.nextInt();
        int rating = sc.nextInt();
        
        list.add(new Vegetable(vegetableId, vegetableName, price, rating ));
      }
      
      
      int rating = sc.nextInt();
      
      getMinPricByRating(list , rating);
    }
    
    static void getMinPricByRating(List<Vegetable> list, int rating){
      int min = Integer.MAX_VALUE;
      int id = 0;
      for(Vegetable i : list) {
        if(i.getRating() > rating) {
          if(i.getPrice() < min) {
            min = i.getPrice();
            id = i.getId();
          }
        }
      }
      
      System.out.println(id);
    }
}

public class Vegetable {
  
  private int vegetableId;
  private String vegetableName ;
  private int price;
  private int rating;
  
  
  public Vegetable(int vegetableId, String vegetableName, int price, int rating) {
    this.vegetableId = vegetableId;
    this.vegetableName = vegetableName;
    this.price = price;
    this.rating = rating;
  }
  
  public int getRating() {
    return rating;
  }
  
  public int getPrice() {
    return price;
  }
  
  public int getId() {
    return vegetableId;
  }
}



//new
import java.util.*;

public class Main {
    public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      List<Song> songs = new ArrayList<>();
      int n = sc.nextInt();
      for(int i = 0; i < n; i++){
        int sid = sc.nextInt();
        sc.nextLine();
        String sname = sc.nextLine();
        String sartist = sc.nextLine();
        double sduration = sc.nextDouble();
        sc.nextLine();
        songs.add(new Song(sid, sname, sartist, sduration));
      }
      String art1 = sc.nextLine();
      String art2 = sc.nextLine();
      System.out.println(getavgduration(songs, art1));
      getsongdetails(songs, art2);
  }
  
  public static double getavgduration(List<Song> songs, String art1){
    double dura = 0;
    for(Song s : songs){
      if(s.getartist().equalsIgnoreCase(art1)){
        dura += s.getduration();
      }
    }
    return dura;
  }
  
  public static void getsongdetails(List<Song> songs, String art2){
    List<Song> artfound = new ArrayList<>();
    for(Song s : songs){
      if(s.getartist().equalsIgnoreCase(art2)){
        //System.out.println(s.getsongid());
        //System.out.println(s.getTitle());
        artfound.add(s);
      }
    }
    Collections.sort(artfound, Comparator.comparingDouble(Song :: getduration));
    for(Song a : artfound){
      System.out.println(a.getsongid());
      System.out.println(a.getTitle());
    }
  }
}

class Song{
  private int songId;
  private String title;
  private String artist;
  private double duration;
  
  public Song(int songId, String title, String artist, double duration){
    this.songId = songId;
    this.title = title;
    this.artist = artist;
    this.duration = duration;
}

public int getsongid(){
  return songId;
}

public String getTitle(){
  return title;
}

public String getartist(){
  return artist;
}

public double getduration(){
  return duration;
}
}


