
import java.io.*;
import java.util.LinkedList;
import java.util.List;

public class Samples {

    private static BufferedReader b;
    private static final Samples samples = new Samples();
    private static int diff = 60;

    public static void main(String[] args) throws Exception {
        b = new BufferedReader(new InputStreamReader(System.in));
        samples.nonstatic();
    }

    private void nonstatic() throws Exception {
        int sampleCount;

        while ((sampleCount = readNumber()) != Integer.MIN_VALUE) {
            Sample[] samplesArray = new Sample[sampleCount];
            for (int i = 0; i < samplesArray.length; i++) {
                String line = b.readLine();
                int time = Integer.parseInt(line.substring(0, line.indexOf(" ")));
                int sampleValue = Integer.parseInt(line.substring(line.indexOf(" ") + 1));
                samplesArray[i] = new Sample(time, sampleValue);
            }
            
            int operationsCount = readNumber();
            OperationLine[] allOperationLines = new OperationLine[operationsCount];
            for (int i = 0; i < allOperationLines.length; i++) {   
                String line = b.readLine();
                String op = line.substring(0, line.indexOf(" "));
                String type = line.substring(line.indexOf(" ") + 1, line.lastIndexOf(" "));
                int time = Integer.parseInt(line.substring(line.lastIndexOf(" ") + 1));
                allOperationLines[i] = new OperationLine(op, type, time);
            }
            
            
            for (OperationLine opLine : allOperationLines) {
                int count = evalOperation(opLine, samplesArray);
                System.out.println(count);
            }
            
//            for (OperationLine opLine : allOperationLines) {
//                System.out.println(opLine);
//            }
            
//            Sample[] test = getSamplesInTime(samplesArray, 120, 2);
//            
//            for (Sample sample : test) {
//                System.out.println(sample);
//            }
        }
    }

   // private int evalOperation(Operation op, Type type, int time, Sample[] samples) {
     private int evalOperation(OperationLine opLine, Sample[] samples) {
        int count = 0;
        for (int i = 1; i < samples.length; i++) {
            Sample[] evalatingSamples = getSamplesInTime(samples, opLine.getTime(), i);
            int value = getValue(opLine.getType(), evalatingSamples);
            switch(opLine.getOperation()){
                case gt:
                    if(samples[i].getSampleValue() > value)
                        count++;
                    break;
                case lt:                    
                    if(samples[i].getSampleValue() < value)
                        count++;
                    break;
            }
        }
        return count;
    }

    private int getValue(Type type, Sample[] samples) {
        switch (type) {
            case min:
                if(samples.length == 0){
                    return Integer.MAX_VALUE;
                }
                int min = samples[0].getSampleValue();
                for (Sample sample : samples) {
                    if (sample.getSampleValue() < min) {
                        min = sample.getSampleValue();
                    }
                }
                return min;
            case max:
                if(samples.length == 0){
                    return Integer.MIN_VALUE;
                }
                int max = samples[0].getSampleValue();
                for (Sample sample : samples) {
                    if (sample.getSampleValue() > max) {
                        max = sample.getSampleValue();
                    }
                }
                return max;
            case avg:
            default:
                int all = 0;
                for (Sample sample : samples) {
                    all += sample.getSampleValue();
                }
                return samples.length == 0 ? Integer.MIN_VALUE : all / samples.length;
        }
    }

    private Sample[] getSamplesInTime(Sample[] samples, int time, int actualIndex) {
//        int newCountSamples = time / diff;
//        Sample[] shortSamples = new Sample[newCountSamples];
//        for (int i = 0; i < newCountSamples; i++) {
//            shortSamples[i] = samples[actualIndex - newCountSamples + i];
//        }
//        return shortSamples;
        
        List<Sample> samplesList = new LinkedList<>();
        int i = actualIndex-1;
        while(i >= 0){
            if(samples[i].getTime() < samples[actualIndex].getTime() - time)
                break;
            samplesList.add(samples[i]);
            i--;
        }
        Sample[] array = new Sample[samplesList.size()];
        samplesList.toArray(array);
        return array;
    }

    private static int readNumber() throws Exception {
        String val = b.readLine();
        return (val == null || val.equals("")) ? Integer.MIN_VALUE : Integer.parseInt(val);
    }

    private enum Operation {

        lt("lt"), gt("gt");
        
        private String text;
        
        private Operation(String t){
            text = t;
        }        
    }

    private enum Type {
        min("min"), max("max"), avg("avg");        
                
        private String text;
        
        private Type(String t){
            text = t;
        }
    }

    private class Sample {

        private final int time, sampleValue;

        public int getTime() {
            return time;
        }

        public int getSampleValue() {
            return sampleValue;
        }

        public Sample(int time, int sampleValue) {
            this.time = time;
            this.sampleValue = sampleValue;
        }

        @Override
        public String toString() {
            return "Sample{" + "time=" + time + ", sampleValue=" + sampleValue + '}';
        }
    }
    
    private class OperationLine {
        private Operation op;
        private Type tp;
        private int time;

        public OperationLine (String op, String tp, int time) {
            for (Operation operation: Operation.values()) {
                if(operation.text.equals(op))
                    this.op = operation;
            }
            for (Type type : Type.values()) {
                if(type.text.equals(tp))
                    this.tp = type;
            }
            this.time = time;
        }

        public Operation getOperation() {
            return op;
        }

        public Type getType() {
            return tp;
        }

        public int getTime() {
            return time;
        }

        @Override
        public String toString() {
            return "Operations{" + "op=" + op + ", tp=" + tp + ", time=" + time + '}';
        }                
    }
}