Stack Problem - Book Exercises

#include <bits/stdc++.h>
using namespace std;

int findSmallest(const stack<pair<int, string>>& inputStack) {
    if (inputStack.empty()) {
        return INT_MAX;
    }

    int smallest = INT_MAX;
    stack<pair<int, string>> tempStack = inputStack; // Make a copy

    while (!tempStack.empty()) {
        int element = tempStack.top().first;
        if (element < smallest) {
            smallest = element;
        }
        tempStack.pop();
    }

    return smallest;
}

int main() {
    int t;
    cin >> t;

    stack<pair<int, string>> myStack;
    int smallest = INT_MAX;

    while (t--) {
        int a;
        cin >> a;
        if (a == -1) {
            int count = 0;
            while (!myStack.empty() && myStack.top().first != smallest) {
                int ans = myStack.top().first;
                if (ans != 0) {
                    count++;
                }
                myStack.pop();
            }
            if (!myStack.empty()) {
                cout << count << " " << myStack.top().second << endl;
                myStack.pop();
            }
            smallest = findSmallest(myStack);
        } else {
            string s;
            cin >> s;
            myStack.push({a, s});
            if (a < smallest && a != 0) {
                smallest = a;
            }
        }
    }
    return 0;
}

My code works for special test cases but is failing on a large test case. The thing that bothers me if it fails on one case then all answers after it should also be wrong because the stack was wrong but this does not happen.

try it with long int