```xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:query_definition xmlns:ns2="http://www.i2b2.org/xsd/cell/crc/psm/1.1/" xmlns:ns4="http://www.i2b2.org/xsd/cell/crc/psm/analysisdefinition/1.1/" xmlns:ns3="http://www.i2b2.org/xsd/cell/crc/psm/querydefinition/1.1/">
    <query_name>(t) Mental DisorderCirculatory sysDigestive syste@14:55:20</query_name>
    <query_timing>ANY</query_timing>
    <specificity_scale>0</specificity_scale>
    <subquery_constraint>
        <first_query>
            <query_id>Event 1</query_id>
            <join_column>STARTDATE</join_column>
            <aggregate_operator>FIRST</aggregate_operator>
        </first_query>
        <operator>LESS</operator>
        <second_query>
            <query_id>Event 2</query_id>
            <join_column>STARTDATE</join_column>
            <aggregate_operator>FIRST</aggregate_operator>
        </second_query>
    </subquery_constraint>
    <subquery_constraint>
        <first_query>
            <query_id>Event 2</query_id>
            <join_column>ENDDATE</join_column>
            <aggregate_operator>ANY</aggregate_operator>
        </first_query>
        <operator>LESS</operator>
        <second_query>
            <query_id>Event 3</query_id>
            <join_column>STARTDATE</join_column>
            <aggregate_operator>FIRST</aggregate_operator>
        </second_query>
        <span>
            <operator>GREATEREQUAL</operator>
            <span_value>1</span_value>
            <units>DAY</units>
        </span>
        <span>
            <operator>LESSEQUAL</operator>
            <span_value>6</span_value>
            <units>YEAR</units>
        </span>
    </subquery_constraint>
    <subquery>
        <query_id>Event 1</query_id>
        <query_type>EVENT</query_type>
        <query_name>Event 1</query_name>
        <query_timing>SAMEINSTANCENUM</query_timing>
        <specificity_scale>0</specificity_scale>
        <panel>
            <panel_number>1</panel_number>
            <panel_timing>SAMEINSTANCENUM</panel_timing>
            <panel_accuracy_scale>100</panel_accuracy_scale>
            <invert>0</invert>
            <total_item_occurrences>1</total_item_occurrences>
            <item>
                <hlevel>2</hlevel>
                <item_name>Mental Disorders</item_name>
                <item_key>\\i2b2_DIAG\i2b2\Diagnoses\Mental Disorders (290-319)\</item_key>
                <item_icon>FA</item_icon>
                <tooltip>Diagnoses \ Mental Disorders</tooltip>
                <class>ENC</class>
                <constrain_by_date>
                    <date_to>2018-01-29T00:00:00.000-05:00</date_to>
                </constrain_by_date>
                <item_is_synonym>false</item_is_synonym>
            </item>
            <item>
                <hlevel>2</hlevel>
                <item_name>Hematologic diseases</item_name>
                <item_key>\\i2b2_DIAG\i2b2\Diagnoses\Hematologic diseases (280-289)\</item_key>
                <item_icon>FA</item_icon>
                <tooltip>Diagnoses \ Hematologic diseases</tooltip>
                <class>ENC</class>
                <constrain_by_date>
                    <date_to>2019-01-29T00:00:00.000-05:00</date_to>
                </constrain_by_date>
                <item_is_synonym>false</item_is_synonym>
            </item>
        </panel>
    </subquery>
    <subquery>
        <query_id>Event 2</query_id>
        <query_type>EVENT</query_type>
        <query_name>Event 2</query_name>
        <query_timing>SAMEINSTANCENUM</query_timing>
        <specificity_scale>0</specificity_scale>
        <panel>
            <panel_number>1</panel_number>
            <panel_timing>SAMEINSTANCENUM</panel_timing>
            <panel_accuracy_scale>100</panel_accuracy_scale>
            <invert>0</invert>
            <total_item_occurrences>1</total_item_occurrences>
            <item>
                <hlevel>2</hlevel>
                <item_name>Circulatory system</item_name>
                <item_key>\\i2b2_DIAG\i2b2\Diagnoses\Circulatory system (390-459)\</item_key>
                <item_icon>FA</item_icon>
                <tooltip>Diagnoses \ Circulatory system</tooltip>
                <class>ENC</class>
                <constrain_by_date>
                    <date_from>1979-12-01T00:00:00.000-05:00</date_from>
                </constrain_by_date>
                <item_is_synonym>false</item_is_synonym>
            </item>
        </panel>
    </subquery>
    <subquery>
        <query_id>Event 3</query_id>
        <query_type>EVENT</query_type>
        <query_name>Event 3</query_name>
        <query_timing>SAMEINSTANCENUM</query_timing>
        <specificity_scale>0</specificity_scale>
        <panel>
            <panel_number>1</panel_number>
            <panel_timing>SAMEINSTANCENUM</panel_timing>
            <panel_accuracy_scale>100</panel_accuracy_scale>
            <invert>0</invert>
            <total_item_occurrences>1</total_item_occurrences>
            <item>
                <hlevel>2</hlevel>
                <item_name>Digestive system</item_name>
                <item_key>\\i2b2_DIAG\i2b2\Diagnoses\Digestive system (520-579)\</item_key>
                <item_icon>FA</item_icon>
                <tooltip>Diagnoses \ Digestive system</tooltip>
                <class>ENC</class>
                <constrain_by_date>
                    <date_from>1979-12-01T00:00:00.000-05:00</date_from>
                    <date_to>2018-01-29T00:00:00.000-05:00</date_to>
                </constrain_by_date>
                <item_is_synonym>false</item_is_synonym>
            </item>
        </panel>
    </subquery>
</ns3:query_definition>

```

I want to find patients who have a diagnosis of mental disorders or hematologic diseases at any time and have a diagnosis of a circulatory system condition between 1979-12-01 and 2018-01-29, but not a digestive system condition between 1979-12-01 and 2019-01-29. The mental disorder and hematologic disease diagnoses should be within the same encounter as the circulatory system diagnosis, but not within the same encounter as the digestive system diagnosis. The time frame for the circulatory and digestive system diagnoses should be within 1-6 years of each other.