#include "testlib.h"
#include <string>

using namespace std;

const string YES = "YES";
const string NO = "NO";

void swap(std::string &s, int q, int w, int ii)
{
    int c = 0;
	for (int i = q-1; i < w; i++)
	{
	    c += s[i] - '0';
	}
	if (c % 2 != 0)
        quitf(_wa, "swap #%d has odd number of ones (%d)", ii+1, c);
	std::reverse(s.begin() + q - 1, s.begin() + w);
}

int main(int argc, char * argv[])
{
    registerTestlibCmd(argc, argv);

    std::string ja = upperCase(ans.readWord());
    std::string pa = upperCase(ouf.readWord());

    if (ja != YES && ja != NO)
        quitf(_fail, "%s or %s expected in answer, but %s found", YES.c_str(), NO.c_str(), compress(ja).c_str());

    if (pa != YES && pa != NO)
        quitf(_pe, "%s or %s expected, but %s found", YES.c_str(), NO.c_str(), compress(pa).c_str());

	if (pa == YES)
	{
		std::string s1 = inf.readWord();
		std::string s2 = inf.readWord();
		int l = s1.length();
		int k = ouf.readInt();
		if (k > 16 * l)
	        quitf(_wa, "Number of operations is more than 16*N");
		for (int i = 0; i < k; i++)
		{
			int b = ouf.readInt();
			int e = ouf.readInt();
			if (b < 1 || e < 1)
			{
		        quitf(_pe, "wrong swap-indexes #%d. Should be more than 0", i+1);
			}
			if (b > l || e > l)
			{
		        quitf(_pe, "wrong swap-indexes #%d. Should be less than or equal strings length", i+1);
			}
			if (b >= e)
			{
		        quitf(_pe, "wrong swap-indexes #%d (%d >= %d)", i+1, b, e);
			}
			swap(s1, b, e, i);
		}
		if (s1 != s2)
		{
	        quitf(_wa, "You didn't get the second word from the first, sorry :(");
		}
	}
	else
	{
        if (ja != pa)
            quitf(_wa, "expected %s, found %s", compress(ja).c_str(), compress(pa).c_str());
	}

    quitf(_ok, "ok");
}